npm install cannot read package.json

Ceilingfish

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 <[email protected]>",
 "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?

Myrne Stol

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to fix: npm ERR! Cannot find module 'read-package-json'

From Dev

How to fix: npm ERR! Cannot find module 'read-package-json'

From Java

npm install if package.json was modified

From Dev

npm install error - invalid package.json

From Dev

npm install not reading package.json

From Dev

npm link, not install, package.json dependencies

From Dev

Change the install directory, npm with package.json

From Dev

npm install vs. edit package.json and npm update

From Dev

"npm install [package]" doesn't update package.json

From Dev

npm install package in absolute path (Locally and package.json)

From Java

Why does "npm install" rewrite package-lock.json?

From Java

How do I add comments to package.json for npm install?

From Java

How to update package-lock.json without doing npm install?

From Java

npm install private github repositories by dependency in package.json

From Dev

Why after "npm install express" there is no package.json in my directory?

From Dev

npm install installing extra packages not specified in package.json

From Java

Make `npm install --save` add a strict version to package.json

From Dev

package.json vs. "npm install %s -g"

From Dev

npm install "no such file or directory '.../package.json'" for custom modules

From Dev

npm install doesn't save dependency to package.json

From Dev

npm install packagename --save-dev not updating package.json

From Dev

NPM - Add to package.json but don't install

From Dev

package.json not found during npm install --save on gitlab

From Dev

Why after "npm install express" there is no package.json in my directory?

From Dev

package.json vs. "npm install %s -g"

From Dev

Why does `npm install` hang with this specific `package.json`?

From Dev

Why isn't my package.json updated by npm install?

From Dev

Cannot install "flexdashboard package"

From Dev

Cannot install plotutils package

Related Related

  1. 1

    How to fix: npm ERR! Cannot find module 'read-package-json'

  2. 2

    How to fix: npm ERR! Cannot find module 'read-package-json'

  3. 3

    npm install if package.json was modified

  4. 4

    npm install error - invalid package.json

  5. 5

    npm install not reading package.json

  6. 6

    npm link, not install, package.json dependencies

  7. 7

    Change the install directory, npm with package.json

  8. 8

    npm install vs. edit package.json and npm update

  9. 9

    "npm install [package]" doesn't update package.json

  10. 10

    npm install package in absolute path (Locally and package.json)

  11. 11

    Why does "npm install" rewrite package-lock.json?

  12. 12

    How do I add comments to package.json for npm install?

  13. 13

    How to update package-lock.json without doing npm install?

  14. 14

    npm install private github repositories by dependency in package.json

  15. 15

    Why after "npm install express" there is no package.json in my directory?

  16. 16

    npm install installing extra packages not specified in package.json

  17. 17

    Make `npm install --save` add a strict version to package.json

  18. 18

    package.json vs. "npm install %s -g"

  19. 19

    npm install "no such file or directory '.../package.json'" for custom modules

  20. 20

    npm install doesn't save dependency to package.json

  21. 21

    npm install packagename --save-dev not updating package.json

  22. 22

    NPM - Add to package.json but don't install

  23. 23

    package.json not found during npm install --save on gitlab

  24. 24

    Why after "npm install express" there is no package.json in my directory?

  25. 25

    package.json vs. "npm install %s -g"

  26. 26

    Why does `npm install` hang with this specific `package.json`?

  27. 27

    Why isn't my package.json updated by npm install?

  28. 28

    Cannot install "flexdashboard package"

  29. 29

    Cannot install plotutils package

HotTag

Archive