简体   繁体   English

没有调用npm package.json脚本

[英]npm package.json scripts not being called

I have the following scripts section in my projects package.json: 我的项目package.json中有以下脚本部分:

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

If i run $ npm test I get this: 如果我运行$ 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

If i run $ npm seed , I get this: 如果我运行$ 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

Why does it recognize my test script but not my seed script? 为什么它识别我的test脚本而不是我的seed脚本?

EDIT 编辑

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我得到了这个错误,这是因为我没有传入-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
...

When I try npm run-script seed -d "localhost/ease" I get this error. 当我尝试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'
...

Why is it looking for a package.json in node_modules\\seed? 为什么在node_modules \\ seed中寻找package.json? Seed is not even a dependency. 种子甚至不是依赖。

From the documentation : 文档

npm supports the "scripts" member of the package.json script, for the following scripts: npm支持package.json脚本的“scripts”成员,用于以下脚本:

  • prepublish: Run BEFORE the package is published. prepublish:在包发布之前运行。 (Also run on local npm install without any arguments.) (也可以在没有任何参数的本地npm installnpm install 。)
  • prepare: Run both BEFORE the package is packed and published, on local npm install without any arguments, and when installing git dependencies (See below). 准备:在打包和发布包之前运行,在没有任何参数的本地npm install ,以及安装git依赖项时运行(见下文)。 This is run AFTER prepublish , but BEFORE prepublishOnly . 这是后运行prepublish ,但在此之前prepublishOnly
  • prepublishOnly: Run BEFORE the package is prepared and packed, ONLY on npm publish . prepublishOnly:在准备和打包包之前运行,仅限于npm publish
  • prepack: run BEFORE a tarball is packed (on npm pack , npm publish , and when installing git dependencies). prepack:在打包tarball之前运行(在npm packnpm publish ,以及安装git依赖项时)。
  • postpack: Run AFTER the tarball has been generated and moved to its final destination. postpack:在生成tarball之后运行并移动到其最终目的地。

  • publish, postpublish: Run AFTER the package is published. 发布,postpublish:发布包后运行。

  • preinstall: Run BEFORE the package is installed 预安装:在安装软件包之前运行
  • install, postinstall: Run AFTER the package is installed. install,postinstall:安装软件包后运行。
  • preuninstall, uninstall: Run BEFORE the package is uninstalled. preuninstall,uninstall:在卸载软件包之前运行。
  • postuninstall: Run AFTER the package is uninstalled. postuninstall:在卸载软件包后运行。
  • preupdate: Run BEFORE the package is updated with the update command. preupdate:在使用update命令更新包之前运行。
  • update, postupdate: Run AFTER the package is updated with the update command. update,postupdate:使用update命令更新软件包后运行。
  • pretest, test, posttest: Run by the npm test command. pretest,test,posttest:由npm test命令运行。
  • prestop, stop, poststop: Run by the npm stop command. prestop,stop,poststop:由npm stop命令运行。
  • prestart, start, poststart: Run by the npm start command. prestart,start,poststart:由npm start命令运行。
  • prerestart, restart, postrestart: Run by the npm restart command. prerestart,restart,postrestart:由npm restart命令运行。 Note: npm restart will run the stop and start scripts if no restart script is provided. 注意:如果没有提供restart脚本, npm restart将运行stop和start脚本。

Additionally, arbitrary scripts can be run by doing npm run-script <stage> <pkg> . 此外,可以通过执行npm run-script <stage> <pkg>来运行任意脚本。

You can see the reason why your npm test script works is because npm test is a built-in command. 你可以看到你的npm test脚本工作的原因是因为npm test是一个内置命令。 You must use npm run-script if you want to execute a script that is not executed by a built-in npm command. 如果要执行未由内置npm命令执行的脚本,则必须使用npm run-script

Custom scripts declared in the package.json can be ran with the npm run <your-script> form in your shell. 可以使用shell中的npm run <your-script>表单npm run <your-script> package.json中声明的自定义脚本。

Try npm run seed or npm run test 尝试npm run seednpm run test

To execute the custom scripts in package.json using below 使用下面的方法在package.json中执行自定义脚本

npm run-script seed npm run-script seed

or 要么

npm run-script < custom script > npm run-script <自定义脚本>

or you can use 或者你可以使用

npm run < custom script > npm运行 <自定义脚本>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM