简体   繁体   English

NPM 启动错误 - 文件路径找不到 JSON 包文件

[英]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 时,我收到此错误消息 -

https://i.stack.imgur.com/gZXjV.png

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的内容,我只能猜测......正如一些评论所指出的那样,请确保您的nodenpm命令可以导航到/查看您的脚本文件


Context:语境:

But there are 2 concepts here.但是这里有两个概念。 node & npm nodenpm

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文件)


Problem:问题:

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.因此,在我看来,您好像在混合这两个概念。

  1. If you wanted to run the script without a package.json you can use the node <filename.js> command如果你想在没有package.json情况下运行脚本,你可以使用node <filename.js>命令
  2. If you have dependencies, and want to make your life a little more convenient if you have a lot of other commands and dependencies, you can use the 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)


Solution:解决方案:

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 startnpm run foonpm run bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NPM START ERROR - 这与 npm 找不到文件有关 - NPM START ERROR - This is related to npm not being able to find a file 节点NPM错误:找不到模块&#39;write-file-atomic&#39; - Node NPM error: Cannot find module 'write-file-atomic' ENOLOCAL:发布 npm 包后找不到 package.json 文件 - ENOLOCAL: can't find package.json file after publishing npm package npm 开始抛出错误“这与 npm 无法找到文件有关。” - npm start throwing error 'This is related to npm not being able to find a file.' (NodeJS,大型 JSON 文件)| 流 json npm package 错误:Javascript 堆出 ZCD69B4957F06CDE2918D7BF3D619 - (NodeJS, large JSON file) | stream-json npm package error: Javascript heap out of memory npm install 说找不到文件 - npm install says cannot find file 使用 npm 安装 java package 时出错 无法打开包含文件:'jni.h' 没有这样的文件或目录 - error installing java package using npm Cannot open include file: 'jni.h' No such fi le or directory 我的 JS npm 包导致“找不到模块的声明文件”错误? - My JS npm package causes 'Could not find a declaration file for module' error? 找不到find.json文件(Node.js NPM游戏编程) - Cannot find.json file (Node.js NPM game programming) npm-bundle 失败并出现错误:ENOENT:没有这样的文件或目录,打开“package.json” - npm-bundle fails with Error: ENOENT: no such file or directory, open 'package.json'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM