简体   繁体   English

如何从已经安装的NPM软件包中运行脚本?

[英]How can I run a script from an already installed NPM package?

Example: 例:

npm install my-tools
npm run my-tools tool1

This doesn't work - it looks for a "package.json in my current directory. The documentation for "npm run" says: 这不起作用-它在我的当前目录中查找“ package.json。”“ npm run”的文档说:

If no package name is provided, it will search for a package.json in the current folder 如果没有提供软件包名称,它将在当前文件夹中搜索package.json。

( https://docs.npmjs.com/cli/run-script ) https://docs.npmjs.com/cli/run-script

So how can I provide a package name? 那么我该如何提供软件包名称?

It looks like you're trying to run a script defined in the package.json of my-tools . 似乎您正在尝试运行my-toolspackage.json中定义的脚本。 This functionality is not supported as of npm@2.0.0 . npm@2.0.0不支持此功能。

The reference to npm run-script taking a package name is a documentation error; 引用带有软件包名称的npm run-script是一个文档错误; I have created an issue for that on the npm issue tracker: https://github.com/npm/npm/issues/7440 我已经在npm问题跟踪器上为此创建了一个问题: https : //github.com/npm/npm/issues/7440

Some alternatives might include: 一些替代方案可能包括:

  1. using npm explore to temporarily set current directory (etc.) into the package, eg, npm explore my-tools -- npm run tool1 使用npm explore将当前目录(等)临时设置到软件包中,例如, npm explore my-tools -- npm run tool1

  2. publishing tool1 etc. as bin entries in package.json instead of as scripts ; tool1等发布为package.json bin条目,而不是作为scripts this will make them accessible via the command line 这将使它们可以通过命令行访问

  3. depending on your needs, you may find a more heavyweight task runner such as grunt is a better approach than npm scripts and binaries 根据您的需求,您可能会发现grunt更重的任务npm例如gruntnpm脚本和二进制文件更好。

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

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