我开始学习 Nodejs 并按照本教程创建了一个简单的 Web 应用程序。
https://blog.risingstack.com/node-hero-tutorial-getting-started-with-node-js/
根据教程,我可以使用“npm start”或“node index.js”运行网络应用程序。当我使用 node install.js 时,它可以工作并给出结果。但是当我使用 npm start 时会出现此错误。
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! code EJSONPARSE
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
由于我是 Nodejs 和 npm 的初学者,所以我不知道出了什么问题。 有谁知道如何解决这一问题 ?提前致谢。
最佳答案
您的 package.json 中存在错误。您不能使用注释 //package.json
。欲了解更多信息,请参阅this answer .
描述您遇到的两种情况:
当您使用 npm start
时,您将使用 npm 的 CLI 在 package.json 中查找该项目目录并运行 start value 中存在的任何命令。 。如果您想使用 npm start
运行您的应用程序,您将在 package.json
中包含如下一行:
...
"scripts": {
"start": "node index.js"
},
...
package.json
是 JSON,因此需要像有效 JSON 一样进行解析。
当您使用node index.js
时,您完全绕过了npm并使用node来启动index.js。如果 package.json
中存在解析错误,也没关系,因为未使用该文件。
关于json - npm 启动错误,但 node index.js 可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39920909/