Within nodejs if a JSON file is simply imported then it will automatically be parsed into an object:
const keyFile = require('./keyfile.json');
Update:
If you are using TS >=2.9:
Support for well-typed JSON imports
TypeScript is now able to import JSON files as input files when using the node strategy for moduleResolution. This means you can use json files as part of their project, and they’ll be well-typed!
// ./tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"resolveJsonModule": true,
"esModuleInterop": true
"outDir": "lib"
},
"include": ["src"]
}
// ./src/settings.json
{
"dry": false,
"debug": false
}
// ./src/foo.ts
import settings from "./settings.json";
settings.debug === true; // Okay
settings.dry === 2; // Error! Can't compare a `boolean` and `number`
These JSON files will also carry over to your output directory so that things “just work” at runtime.
LOAD_AS_FILE(X).LOAD_AS_FILE(X)is not an actual method but is technical writing describing how the module loading algo works in NodeJS. The point was to illuminate that JSON can be directly loaded in NodeJS without the need to manually read a file and parse the JSON to access the data represented by it.