没有调用npm package.json脚本 [英] npm package.json scripts not being called
问题描述
我的项目package.json中有以下脚本部分:
I have the following scripts section in my projects package.json:
"scripts": {
"seed": "node bin/seed",
"test": "echo \"Error: no test specified\" && exit 1"
},
如果我运行 $ npm test
我得到这个:
If i run $ npm test
I get this:
>npm test
> node-mongo-seeds@0.0.1 test C:\Users\m089269\WebstormProjects\node-mongo-seeds
> echo "Error: no test specified" && exit 1
"Error: no test specified"
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
如果我运行 $ npm seed
,我明白了:
If i run $ npm seed
, I get this:
npm seed
Usage: npm <command>
where <command> is one of:
add-user, adduser, apihelp, author, bin, bugs, c, cache,
completion, config, ddp, dedupe, deprecate, docs, edit,
explore, faq, find, find-dupes, get, help, help-search,
home, i, info, init, install, isntall, issues, la, link,
list, ll, ln, login, ls, outdated, owner, pack, prefix,
prune, publish, r, rb, rebuild, remove, repo, restart, rm,
root, run-script, s, se, search, set, show, shrinkwrap,
star, stars, start, stop, submodule, tag, test, tst, un,
uninstall, unlink, unpublish, unstar, up, update, v,
version, view, whoami
npm <cmd> -h quick help on <cmd>
npm -l display full usage info
npm faq commonly asked questions
npm help <term> search for help on <term>
npm help npm involved overview
Specify configs in the ini-formatted file:
C:\Users\m089269\.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config
npm@1.4.3 C:\Program Files\nodejs\node_modules\npm
为什么它会识别我的测试
脚本而不是我的种子
脚本?
Why does it recognize my test
script but not my seed
script?
编辑
当我尝试 npm run-script seed
我收到此错误,这是因为我没有传入 -d
param:
When I try npm run-script seed
I get this error which is expected because I don't pass in a -d
param:
$ npm run-script seed
> node-mongo-seeds@0.0.1 seed C:\Users\m089269\WebstormProjects\node-mongo-seeds
> node bin/seed
Populate mongo from a set of .json files.
Usage: $ node seed
Options:
-d The path to your mongo db [required]
Missing required arguments: d
npm ERR! node-mongo-seeds@0.0.1 seed: `node bin/seed`
npm ERR! Exit status 1
...
当我尝试 npm run -script seed -dlocalhost / ease
我收到此错误。
When I try npm run-script seed -d "localhost/ease"
I get this error.
npm run-script seed -d localhost/ease-dev
npm info it worked if it ends with ok
npm info using npm@1.4.3
npm info using node@v0.10.26
npm ERR! Error: ENOENT, open 'C:\Users\m089269\WebstormProjects\node-mongo-seeds\node_modules\seed\package.json'
...
为什么在node_modules \ seed中寻找一个package.json?种子甚至不是依赖。
Why is it looking for a package.json in node_modules\seed? Seed is not even a dependency.
推荐答案
来自文档:
npm支持package.json的scripts成员脚本,用于以下脚本:
npm supports the "scripts" member of the package.json script, for the following scripts:
- prepublish:在发布包之前运行。 (也可以在没有任何参数的本地
npm install
上运行。) - 发布,postpublish:在程序包发布后运行。
- 预安装:在安装软件包之前运行
- 安装,postinstall:安装软件包后运行。
- preuninstall ,卸载:在卸载软件包之前运行。
- postuninstall:在卸载软件包后运行。
- preupdate:在更新软件包之前运行更新命令。
- 更新,postupdate:使用更新命令更新软件包后运行。
- pretest,test,posttest:由
npm test
命令。 - prestop,stop,poststop:由
npm stop $ c $运行c>命令。
- 预启动,启动,poststart:由
npm start
命令运行。 - prerestart,restart,postrestart:由
npm restart
命令运行。注意:如果没有提供restart
脚本,npm restart
将运行停止并启动脚本。
- prepublish: Run BEFORE the package is published. (Also run on local
npm install
without any arguments.) - publish, postpublish: Run AFTER the package is published.
- preinstall: Run BEFORE the package is installed
- install, postinstall: Run AFTER the package is installed.
- preuninstall, uninstall: Run BEFORE the package is uninstalled.
- postuninstall: Run AFTER the package is uninstalled.
- preupdate: Run BEFORE the package is updated with the update command.
- update, postupdate: Run AFTER the package is updated with the update command.
- pretest, test, posttest: Run by the
npm test
command. - prestop, stop, poststop: Run by the
npm stop
command. - prestart, start, poststart: Run by the
npm start
command. - prerestart, restart, postrestart: Run by the
npm restart
command. Note:npm restart
will run the stop and start scripts if norestart
script is provided.
此外,可以通过执行 npm run-script< stage>来运行任意脚本。 < pkg>
。
您可以看到 npm测试的原因
脚本的工作原理是因为 npm test
是一个内置命令。如果要执行未由内置npm命令执行的脚本,则必须使用 npm run-script
。
You can see the reason why your npm test
script works is because npm test
is a built-in command. You must use npm run-script
if you want to execute a script that is not executed by a built-in npm command.
这篇关于没有调用npm package.json脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!