【问题标题】:npm package.json scripts not being called未调用 npm package.json 脚本
【发布时间】:2014-06-26 03:46:59
【问题描述】:

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

【问题讨论】:

    标签: javascript json node.js npm


    【解决方案1】:

    来自documentation

    npm 支持 package.json 脚本的“scripts”成员,用于以下脚本:

    • prepublish:在包发布之前运行。 (也可以在本地 npm install 上运行,不带任何参数。)

    • prepare:在打包和发布包之前,在本地 npm install 上运行,不带任何参数,在安装 git 依赖项时运行(见下文)。这是在prepublish 之后运行,但在prepublishOnly 之前运行。

    • prepublishOnly:在准备和打包包之前运行,仅在 npm publish 上运行。

    • prepack:在打包 tarball 之前运行(在 npm packnpm publish 以及安装 git 依赖项时)。

    • postpack:在 tarball 生成并移动到其最终目的地之后运行。

    • publish、postpublish:在包发布后运行。

    • preinstall:在安装包之前运行

    • 安装、安装后:安装包后运行。

    • 预卸载、卸载:在卸载包之前运行。

    • postuninstall:在软件包卸载后运行。

    • preupdate:在使用 update 命令更新包之前运行。

    • update, postupdate:在使用 update 命令更新包后运行。

    • 前测、测试、后测:通过npm test 命令运行。

    • prestop、stop、poststop:通过npm stop 命令运行。

    • prestart、start、poststart:通过npm start 命令运行。

    • prerestart、restart、postrestart:通过npm restart 命令运行。注意:如果没有提供restart 脚本,npm restart 将运行停止和启动脚本。

    另外,通过npm run-script &lt;stage&gt; &lt;pkg&gt;可以运行任意脚本。

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

    【讨论】:

    • 这是非常有用的信息。请查看我对我的问题的编辑,因为仍然有问题。
    • 看来 npm run-script 目前不支持传入参数。所以现在我只是将 -d 值硬编码到脚本部分"scripts": {"seed": "node bin/seed -d localhost/ease"...
    • 假设 npm 将本地环境传递给脚本,您可以让节点脚本读取环境变量以设置参数。
    • 注意npm run-script的别名是npm run
    • 请注意,他们在最新版本中进行了一些更改。值得注意的是,他们删除了 update/shrinkwrap 脚本并添加了一个版本脚本
    【解决方案2】:

    package.json 中声明的自定义脚本可以在您的 shell 中使用 npm run &lt;your-script&gt; 形式运行。

    试试npm run seednpm run test

    【讨论】:

    • 欢迎来到 SO!您的回复看起来像是评论而不是答案。一旦你有足够的reputation,你就可以在任何帖子上comment。还要检查这个what can I do instead。如果您打算回答,请阅读此how-to-answer 以遵循 SO 指南。
    【解决方案3】:

    使用下面的方法在 package.json 中执行自定义脚本

    npm 运行脚本种子

    npm run-script

    或者你可以使用

    npm 运行

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 2018-09-04
      • 2021-10-03
      • 2021-10-01
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      相关资源
      最近更新 更多