[英]NPM start error - file path cannot find JSON package file
First time poster, thanks in advance for the help!第一次发帖,先谢谢大家的帮助!
I'm trying to complete my first project and am running into a few issues.我正在尝试完成我的第一个项目,但遇到了一些问题。 The main one being that when I hit npm start I get hit with this error message -
主要的是,当我点击 npm start 时,我收到此错误消息 -
I've tried hitting npm install before I run it but to no avail.我试过在运行之前点击 npm install 但无济于事。 Does anyone have any ideas?!
有没有人有任何想法?!
Thanks!谢谢!
Without the contents of the package.json
I can only guess... As some of the comments have pointed our as well, make sure your node
or npm
command can navigate to/see your script file如果没有
package.json
的内容,我只能猜测......正如一些评论所指出的那样,请确保您的node
或npm
命令可以导航到/查看您的脚本文件
But there are 2 concepts here.但是这里有两个概念。
node
& npm
node
和npm
1. Node is a runtime: 1. Node 是一个运行时:
In order to run a node script you'll need the node
command (within the context of the project your working on, so make sure your cli is in the root of your project) like this:为了运行 node 脚本,您需要
node
命令(在您正在处理的项目的上下文中,因此请确保您的 cli 位于您的项目的根目录中),如下所示:
node ./folder/to/your-script.js
2. NPM is a package manager. 2. NPM 是一个包管理器。
...and it relies on having a package.json
in the same directory as where you're running the npm
command for most of it's commands other than init
(which creates the package.json
file for you) ...并且它依赖于在您运行
npm
命令的同一目录中拥有一个package.json
来处理除init
之外的大多数命令(它为您创建package.json
文件)
The second screenshot is the clue here.第二个屏幕截图是这里的线索。
You tried running npm start
, where the error message is:您尝试运行
npm start
,其中错误消息是:
missing script: start
缺少脚本:开始
This means that your scripts block does not contain a start
property这意味着您的脚本块不包含
start
属性
Thus it seems to me as if you are mixing the 2 concepts.因此,在我看来,您好像在混合这两个概念。
package.json
you can use the node <filename.js>
commandpackage.json
情况下运行脚本,你可以使用node <filename.js>
命令npm run <script>
command.npm run <script>
命令。 (Side-note: npm start
is a special case that seems to be able to run or without the run
keyword, which I didn't know as well until I just tested it now :P) (旁注:
npm start
是一种特殊情况,它似乎可以在没有run
关键字的情况下run
,直到我现在才测试它时我才知道:P)
Therefor to get closer to what your expecting, we can modify the package.json
as per below:因此,为了更接近您的期望,我们可以按如下方式修改
package.json
:
You can also create custom aliases for commands within the scripts
block (inside the package.json
):您还可以在
scripts
块中(在package.json
)为命令创建自定义别名:
"scripts": {
"start": "node ./some-path/to/your-script.js"
"bar": "npm run foo",
"foo": "node ./folder/to/your-script.js"
}
You'll notice, you can reference sibling script aliases in other script commands as well (you can also add non node commands in here, like git
, etc..)您会注意到,您也可以在其他脚本命令中引用同级脚本别名(您也可以在此处添加非节点命令,例如
git
等。)
Thus with the above in your package.json
you'll have access to the following commands when running npm
in the same directory as the package.json
.因此,在你上面
package.json
运行时,你将有机会获得以下命令npm
在同一目录下package.json
。
npm start
, npm run foo
or npm run bar
npm start
、 npm run foo
或npm run bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.