10

I am trying to manage my node package dependencies. I'd like to be able to install all the required dependencies by running a command, and from what I have read, one way to achieve this is using a package.json file and running npm install. So my JSON file looks like this:

{
 "name": "Name-Of-The-Thing",
 "description": "The Thing's Name",
 "author": "The Dude <the.dude@dudethinking.com>",
 "dependencies": {
      "mocha":">= 1.12.0",
      "mocha-phantomjs":">= 3.1.0",
      "chai":">= 1.7.2",
      "phantomjs":">= 1.9.1"
 }
}

However npm install reports the following error:

npm ERR! Failed to parse json
npm ERR! Unexpected token ?
npm ERR! File: C:\Path\To\The\Thing\package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR!
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse

npm ERR! System Windows_NT 6.2.9200
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "test"
npm ERR! cwd C:\Path\To\The\Thing
npm ERR! node -v v0.8.15
npm ERR! npm -v 1.1.66
npm ERR! file C:\Path\To\The\Thing\package.json
npm ERR! code EJSONPARSE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Path\To\The\Thing\npm-debug.log
npm ERR! not ok code 0

Anyone know why?

Ceilingfish
  • 5,131
  • 3
  • 41
  • 68
  • This particular package.json is working fine for me. – Myrne Stol Jul 30 '13 at 12:09
  • @MerynStol that's awesome. Glad to know it really is that simple. Maybe it's a platform issue. I'm running that command on Windows 8, and a 64 bit architecture – Ceilingfish Jul 30 '13 at 13:17
  • 1
    Given the "Unexpected token ?" error, perhaps there's some weird (invisible) character in the json file. Maybe try different text editor, or fiddle with your editor's settings. Then resave the package.json file. I'd use UTF-8 everywhere. – Myrne Stol Jul 30 '13 at 16:18
  • 1
    @MerynStol thanks for the idea, it inspired me! The file was already encoded as UTF-8, but that error message was making me suspicious, so I encoded it as ASCII instead (i.e. removed the BOM), and it worked great! Thanks! – Ceilingfish Jul 31 '13 at 07:53
  • In fact, here's a bug report on the issue https://github.com/isaacs/npm/issues/3358 – Ceilingfish Jul 31 '13 at 07:57

3 Answers3

13

Proper answer:

Your editor adds a byte-order-mark to the JSON file, which makes the octet-stream an invalid JSON text.

JSON RFC says:

JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

Since the first two characters of a JSON text will always be ASCII characters [RFC0020], it is possible to determine whether an octet stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking at the pattern of nulls in the first four octets.

       00 00 00 xx  UTF-32BE
       00 xx 00 xx  UTF-16BE
       xx 00 00 00  UTF-32LE
       xx 00 xx 00  UTF-16LE
       xx xx xx xx  UTF-8

The bug report you mentioned has been closed for this reason.

From my understanding, any valid ASCII encoded text also happens to be valid UTF-8, so together with the absence of the BOM it explains why it now works as expected.

In general, I think you should set up your text editor to save files in UTF-8, without a byte-order-mark. See What's different between UTF-8 and UTF-8 without BOM? for discussion. Per What encoding is expected for Node.js source code? , Node.js would accept non-ASCII characters in JS source files encoded this way. This can be handy when you want to embed a non-ASCII string somewhere in the source code.

Community
  • 1
  • 1
Myrne Stol
  • 10,404
  • 4
  • 37
  • 48
  • ASCII is a subset of UTF-8, omitting the BOM only causes a problem when you start using accented characters or ideographs that appear in asiatic character sets (anything over character code 255). I guess the ideal solution would be to have my editor support the specific encoding that the JSON spec uses to identify files, and activate that detection mechanism based on a .json file extension, sadly the Visual Studio team don't appear to have got around to this (and I somehow doubt they ever will) – Ceilingfish Jul 31 '13 at 14:22
  • 1
    @Ceilingfish Maybe one of the solutions posted here work? http://stackoverflow.com/questions/5406172/utf-8-without-bom – Myrne Stol Jul 31 '13 at 14:49
  • God knows why, But in my case, when I saved with BOM, it worked. Otherwise it failed to parse JSON. I am using sublime text 2. – Sushant Gupta Jan 14 '14 at 04:54
1

npm ERR! Unexpected token ?

In case there is no BOM, also check if you just have a "?" somewhere in the file or other errors, e.g. a missing or additional ",".

Steve K
  • 12,452
  • 10
  • 74
  • 126
0

The only solution is to specify the exact version of the dependencies. NPM sometimes does not recognise > or .x