JSON Question

npm start error but node index.js works

I started learning nodejs and followed this tutorial to create a simple web app.


According to the tutorial I can run the web app using either 'npm start' or node index.js' . When I use node install.js it works and give the result. But when I use npm start this error occurs.

npm ERR! Linux 4.4.0-38-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "start"
npm ERR! node v4.2.6
npm ERR! npm v3.5.2
npm ERR! file /var/www/html/nodejs/package.json

npm ERR! Failed to parse json
npm ERR! Unexpected token '/' at 1:1
npm ERR! // package.json
npm ERR! ^
npm ERR! File: /var/www/html/nodejs/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! Please include the following file with any support request:
npm ERR! /var/www/html/nodejs/npm-debug.log

As I'm a total beginner to nodejs and npm I can't figure out what went wrong.
Does anyone know how to fix this ? Thanks in advance.

Answer Source

There's an error in your package.json. You can't use the comment // package.json. For more info, see this answer.

To describe the two situations you're encountering:

When you use npm start, you're using npm's CLI to look in the package.json for that project directory and run whatever command exists at the start value. If you wanted to run your app using npm start, you'd have a line in package.json like this:

"scripts": {
  "start": "node index.js"

package.json is JSON so it needs to parse like valid JSON.

When you use node index.js, you're bypassing npm entirely and using node to fire up index.js. If there's a parsing error in package.json, it doesn't matter since that file isn't used.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download