Reputation: 1056
I'm using a simple Node.js to pull information from a valid jsonfile (checked with JSLint), but the code i'm using doesn't return the expected value:
squadJSON = JSON.parse(fs.readFileSync('./squads/squad' + whichSquad + '.json'));
and it returns:
{ type: 'Buffer', data:
[ 123,
10,
32,
32,
34,
97,
99,
... 548 more items ] }
Any reason as to why this happens?
Upvotes: 88
Views: 105239
Reputation: 51886
The "why" has been answered by Sidney, but a better "what to do" would be to use require()
, which supports parsing valid JSON files synchronously and returning the result as an object:
squadJSON = require('./squads/squad' + whichSquad + '.json');
or even nicer, using an ES6 template literal:
squadJSON = require(`./squads/squad${whichSquad}.json`);
One notable difference using require()
is that it resolves relative paths from __dirname
, which is the directory path of the current module, whereas fs
methods resolve relative paths using process.cwd()
, or "current working directory". It is the directory within a shell session from which the main module of the program is executed by node.
Hopefully the examples below demonstrate how their relative path resolution differs:
To make require(...)
behave like JSON.parse(fs.readFileSync(..., 'utf8'))
:
const { resolve } = require('path');
function readFileSyncJson (path) {
return require(resolve(process.cwd(), path));
}
And to make JSON.parse(fs.readFileSync(..., 'utf8'))
behave like require(...)
:
const { resolve } = require('path');
function requireJson (path) {
return JSON.parse(fs.readFileSync(resolve(__dirname, path), 'utf8'));
}
Upvotes: 12
Reputation: 4775
fs.readFileSync()
returns a Buffer if you don't specify an encoding.
https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options
So, tell fs.readFileSync()
what encoding to use:
squadJSON = JSON.parse(fs.readFileSync('./squads/squad' + whichSquad + '.json', 'utf8'));
Upvotes: 192