没有调用npm package.json脚本

时间:2014-05-08 16:11:15

标签: javascript json node.js npm

我的项目package.json中有以下脚本部分:

"scripts": {
    "seed": "node bin/seed",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

如果我运行$ npm test我得到了这个:

>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,我明白了:

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

为什么它会识别我的test脚本而不识别我的seed脚本?

修改

当我尝试npm run-script seed时,我收到此错误,因为我没有通过-d参数传递

$ 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 -d "localhost/ease"时,我收到此错误。

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?种子甚至不是依赖。

3 个答案:

答案 0 :(得分:60)

来自documentation

  

npm支持&#34;脚本&#34; package.json脚本的成员,用于以下脚本:

     
      
  • prepublish:在发布包之前运行。 (也可以在没有任何参数的本地npm install上运行。)
  •   
  • 发布,发布后:在发布包之后运行。
  •   
  • 预安装:在安装软件包之前运行
  •   
  • install,postinstall:安装软件包后运行。
  •   
  • 预安装,卸载:在卸载软件包之前运行。
  •   
  • postuninstall:在卸载软件包后运行。
  •   
  • preupdate:在使用update命令更新软件包之前运行。
  •   
  • update,postupdate:使用update命令更新软件包后运行。
  •   
  • pretest,test,posttest:由npm test命令运行。
  •   
  • prestop,stop,poststop:由npm stop命令运行。
  •   
  • prestart,start,poststart:由npm start命令运行。
  •   
  • prerestart,restart,postrestart:由npm restart命令运行。注意:如果未提供npm restart脚本,restart将运行停止并启动脚本。
  •   
     

此外,可以通过执行npm run-script <stage> <pkg>来运行任意脚本。

您可以看到npm test脚本工作的原因是因为npm test是内置命令。如果要执行未由内置npm命令执行的脚本,则必须使用npm run-script

答案 1 :(得分:15)

可以在shell中使用npm run <your-script>表单运行package.json中声明的自定义脚本。

尝试npm run seednpm run test

答案 2 :(得分:2)

要使用以下

在package.json中执行自定义脚本

npm运行脚本种子

npm运行脚本 <自定义脚本>

或者您可以使用

npm运行 <自定义脚本>