npm 启动错误但节点 index.js 有效 [英] npm start error but node index.js works

查看:72
本文介绍了npm 启动错误但节点 index.js 有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习 nodejs 并按照本教程创建了一个简单的 Web 应用程序.

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

https://blog.risingstack.com/node-hero-tutorial-getting-started-with-node-js/

根据教程,我可以使用 'npm start' 或 node index.js' 运行网络应用程序.当我使用 node install.js 时,它可以工作并给出结果.但是当我使用 npm start 时会出现这个错误.

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! 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 的完全初学者,我无法弄清楚出了什么问题.有谁知道如何解决这一问题 ?提前致谢.

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.

推荐答案

您的 package.json 中存在错误.您不能使用注释 //package.json.有关详细信息,请参阅此答案.

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:

当您使用 npm start 时,您将使用 npm 的 CLI 在 package.json 中查找该项目目录并运行 起始值.如果您想使用 npm start 运行您的应用程序,您将在 package.json 中有这样一行:

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 是 JSON,因此它需要像有效的 JSON 一样进行解析.

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

当您使用 node index.js 时,您将完全绕过 npm 并使用 node 来启动 index.js.如果 package.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.

这篇关于npm 启动错误但节点 index.js 有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆