如何解决:npm run build/dev: missing script?

15

我正在尝试运行node,但由于某些原因,本地的npm安装程序未能正常工作。

该软件包已经存在:

$ npm run dev npm ERR! Darwin 15.4.0 
npm ERR! argv "/usr/local/Cellar/node/5.6.0/bin/node" "/usr/local/bin/npm" "run" "jshint" 
npm ERR! node v5.6.0 
npm ERR! npm  v3.6.0
npm ERR! missing script: dev 
npm ERR!  
npm ERR! If you need help, you may report this error at: 
npm ERR!     <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request: 
npm ERR!     /Users/me/workspace/testapp/npm-debug.log

我能使用npm install,但run npm dev是不正确的。

2个回答

29

您看到了这个错误,是因为在您的package.json文件中的"scripts"部分中可能没有名为dev的脚本。

npm installnpm run dev是两个完全不同的概念。

  1. npm install将运行package.json文件中的dependencies部分,并获取/安装该列表中的模块。

  2. npm run dev将检查package.json文件中的scripts部分,并尝试查找名为"dev"的脚本。如果没有名为"dev"的脚本,它将报错,就像您所遇到的那样(顺便说一句,"Dev"绝对不是一个特殊的单词,如果您将来需要在项目中使用scripts部分,可以将脚本命名为任何您想要的名字)。

例如,创建一个新文件夹,并将以下内容复制到名为package.json的文件中:

{
  "name": "testapp",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "dev": "echo This is the DEV script",
    "abc": "echo This is the abc script",
    "xyz": "echo This is the xyz script",
    "start":"echo This is the special start script"
  }
}

从终端进入包含示例 package.json 的目录并尝试以下命令,看看会发生什么:

npm run dev 您应该在屏幕上看到 "This is the dev script"

npm run abc 您应该在屏幕上看到 "This is the abc script"

npm run xyz 您应该在屏幕上看到 "This is the xyz script"

npm run linkxu1989 由于 package.json 中的 scripts 部分中没有名为 "linkxu1989" 的脚本,因此您应该在屏幕上看到与上面类似的错误

npm start 您应该在屏幕上看到 "This is the special start script" (请注意,start 是一个特殊名称。您可以仅使用 npm start 或使用 npm run start 运行它,就像所有其他脚本一样)

底线:检查 package.json 中的 "scripts" 部分,要运行其中任何一个,请输入 npm run SCRIPT_NAME

希望这有所帮助,祝你在 NPM 中好运!

详情请参阅此处


0

这意味着在你运行“npm run build”命令的文件夹中的“package.json”文件中,没有“build”脚本。快速检查一下:在你的终端/命令提示符中运行“npm run lalala”。它会显示“missing script: lalala”。

因此,如果是你自己的包,请添加“build”脚本。只需转到代码编辑器中的 package.json 文件并将键值条目添加为 JSON。

如果是你从 npmjs.org 下载的模块/包,则请参考文档以查看他们支持的命令。

注意:“npm build”是与“npm run build”完全不同的命令。“start”和“test”以外的所有“scripts”命令都需要使用“run”来运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接