json - npm 启动错误,但 node index.js 可以工作

标签 json node.js npm

我开始学习 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/

相关文章:

sql-server - 如何强制 SQL Server 返回空 JSON 数组

json - 将 json 数据插入 POSTGRES 表列的文本数据类型

javascript - 如何从这些 JSON 对象中过滤掉值

javascript - 在sails.js 中停止模式的自动迁移

node.js - 使 npm 包子模块可用

C# JSON 解析

javascript - 在已经完全加载的 Controller 之后加载 AngularJS 指令

node.js - "error": "E_VALIDATION", "status": 400 node. js

ubuntu - yarn安装命令报错No such file or directory : 'install'

javascript - 什么是平均文件:workspaces in package dependencies?