Steve Knoblock
Steve Knoblock

Reputation: 33

JSON undefined on nodejs

I just upgraded node.js to v6.10.3 and tried to run some test code.

var str = '{"foo": "bar"}';
var obj = JSON.parse(str);
console.log(obj.foo);

I saved this to a file test.js and tried running it from the node command prompt.

Windows Script Host says

'JSON' is undefined

I can open a Chrome window, open the debugger, open the console and enter the same code and it runs correctly.

I have searched google and SO for an explanation, and I find a lot of matches on JSON and undefined, but nothing pertaining to this problem in node.

Upvotes: 2

Views: 241

Answers (1)

mscdex
mscdex

Reputation: 106696

You need to make sure you specify the node executable instead of just the javascript file or double clicking on the javascript file. For example: node test.js. Otherwise Windows will try to use WSH (specifically its JScript engine) to execute the code which contains an older, inferior javascript runtime.

Upvotes: 1

Related Questions