node.js 如何使用本地安装在 node_modules 中的包中的可执行文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9679932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:19:37  来源:igfitidea点击:

How to use executables from a package installed locally in node_modules?

node.jscoffeescriptnpmnode-modules

提问by typeoneerror

How do I use a local version of a module in node.js. For example, in my app, I installed coffee-script:

如何在node.js. 例如,在我的应用程序中,我安装了 coffee-script:

npm install coffee-script

This installs it in ./node_modulesand the coffee command is in ./node_modules/.bin/coffee. Is there a way to run this command when I'm in my project's main folder? I guess I'm looking for something similar to bundle execin bundler. Basically, I'd like to specify a version of coffee-script that everyone involved with the project should use.

这会将它安装在./node_modules.coffee 命令中./node_modules/.bin/coffee。当我在项目的主文件夹中时,有没有办法运行此命令?我想我正在寻找类似于bundle exec捆绑器的东西。基本上,我想指定参与该项目的每个人都应该使用的咖啡脚本版本。

I know I can add the -gflag to install it globally so coffee works fine anywhere, but what if I wanted to have different versions of coffee per project?

我知道我可以添加-g标志以在全球范围内安装它,这样咖啡在任何地方都可以正常工作,但是如果我想每个项目有不同版本的咖啡怎么办?

回答by regular

UPDATE: As Seyeong Jeong points out in their answer below, since npm 5.2.0 you can use npx [command], which is more convenient.

更新:正如 Seyeong Jeong 在下面的回答中指出的那样,因为 npm 5.2.0 你可以使用npx [command],这更方便。

OLD ANSWER for versions before 5.2.0:

5.2.0 之前版本的旧答案

The problem with putting

放置的问题

./node_modules/.bin

into your PATH is that it only works when your current working directory is the root of your project directory structure (i.e. the location of node_modules)

进入您的 PATH 是它仅在您当前的工作目录是项目目录结构的根目录时才有效(即 的位置node_modules

Independent of what your working directory is, you can get the path of locally installed binaries with

与您的工作目录无关,您可以使用以下命令获取本地安装的二进制文件的路径

npm bin

To execute a locally installed coffeebinary independent of where you are in the project directory hierarchy you can use this bash construct

要执行本地安装的coffee二进制文件,而与您在项目目录层次结构中的位置无关,您可以使用此 bash 构造

PATH=$(npm bin):$PATH coffee

I aliased this to npm-exec

我将其别名为 npm-exec

alias npm-exec='PATH=$(npm bin):$PATH'

So, now I can

所以,现在我可以

npm-exec coffee

to run the correct copy of coffee no matter of where I am

无论我在哪里,都能运行正确的咖啡副本

$ pwd
/Users/regular/project1

$ npm-exec which coffee
/Users/regular/project1/node_modules/.bin/coffee

$ cd lib/
$ npm-exec which coffee
/Users/regular/project1/node_modules/.bin/coffee

$ cd ~/project2
$ npm-exec which coffee
/Users/regular/project2/node_modules/.bin/coffee

回答by Seyeong Jeong

Nice example

很好的例子

You don't have to manipulate $PATHanymore!

你不必再操纵$PATH了!

From [email protected], npmships with npxpackage which lets you run commands from a local node_modules/.binor from a central cache.

[email protected] 开始npm附带了一个npx包,它允许您从本地node_modules/.bin或中央缓存运行命令。

Simply run:

只需运行:

$ npx [options] <command>[@version] [command-arg]...

By default, npxwill check whether <command>exists in $PATH, or in the local project binaries, and execute that.

默认情况下,npx将检查是否<command>存在于$PATH,或本地项目二进制文件中,并执行它。

Calling npx <command>when <command>isn't already in your $PATHwill automatically install a package with that name from the NPM registry for you, and invoke it. When it's done, the installed package won't be anywhere in your globals, so you won't have to worry about pollution in the long-term. You can prevent this behaviour by providing --no-installoption.

调用npx <command>when <command>is not already in your$PATH将自动从 NPM 注册表中为您安装一个具有该名称的包,并调用它。完成后,安装的包将不会在您的全局变量中的任何位置,因此您不必担心长期污染。您可以通过提供--no-install选项来防止这种行为。

For npm < 5.2.0, you can install npxpackage manually by running the following command:

对于npm < 5.2.0,您可以npx通过运行以下命令手动安装包:

$ npm install -g npx

回答by jassa

Use the npm bincommand to get the node modules /bin directory of your project

使用npm bin命令获取你项目的节点模块/bin目录

$ $(npm bin)/<binary-name> [args]

e.g.

例如

$ $(npm bin)/bower install

回答by jla

Use npm run[-script] <script name>

npm run[-script] <script name>

After using npm to install the bin package to your local ./node_modulesdirectory, modify package.jsonto add <script name>like this:

使用npm将bin包安装到本地./node_modules目录后,修改package.json添加<script name>如下:

$ npm install --save learnyounode
$ edit packages.json
>>> in packages.json
...
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "learnyounode": "learnyounode"
},
...
$ npm run learnyounode

It would be nice if npm install had a --add-script option or something or if npm run would work without adding to the scripts block.

如果 npm install 有一个 --add-script 选项或其他东西,或者 npm run 可以在不添加到脚本块的情况下工作,那就太好了。

回答by mightyiam

Use npm-run.

使用npm-run.

From the readme:

从自述文件:

npm-run

npm 运行

Find & run local executables from node_modules

从 node_modules 中查找并运行本地可执行文件

Any executable available to an npm lifecycle script is available to npm-run.

任何可用于 npm 生命周期脚本的可执行文件均可用于npm-run.

Usage

用法

$ npm install mocha # mocha installed in ./node_modules
$ npm-run mocha test/* # uses locally installed mocha executable 

Installation

安装

$ npm install -g npm-run

回答by Linus Gustav Larsson Thiel

Update:I no longer recommend this method, both for the mentioned security reasons and not the least the newer npm bincommand. Original answer below:

更新:我不再推荐这种方法,无论是出于上述安全原因还是较新的npm bin命令。原答案如下:

As you have found out, any locally installed binaries are in ./node_modules/.bin. In order to always run binaries in this directory rather than globally available binaries, if present, I suggest you put ./node_modules/.binfirst in your path:

正如您所发现的,任何本地安装的二进制文件都在./node_modules/.bin. 为了始终在此目录中运行二进制文件而不是全局可用的二进制文件(如果存在),我建议您将./node_modules/.bin第一个放在您的路径中:

export PATH="./node_modules/.bin:$PATH"

If you put this in your ~/.profile, coffeewill always be ./node_modules/.bin/coffeeif available, otherwise /usr/local/bin/coffee(or whatever prefix you are installing node modules under).

如果你把它放在你的~/.profile, 中,如果可用,coffee将始终存在./node_modules/.bin/coffee,否则/usr/local/bin/coffee(或你正在安装节点模块的任何前缀)。

回答by Bob9630

The PATH solution has the issue that if $(npm bin) is placed in your .profile/.bashrc/etc it is evaluated once and is forever set to whichever directory the path was first evaluated in. If instead you modify the current path then every time you run the script your path will grow.

PATH 解决方案存在的问题是,如果 $(npm bin) 放置在您的 .profile/.bashrc/etc 中,它会被评估一次,并且永远设置为第一次评估路径的目录。如果改为修改当前路径,则每次运行脚本时,您的路径都会增长。

To get around these issues, I create a function and used that. It doesn't modify your environment and is simple to use:

为了解决这些问题,我创建了一个函数并使用了它。它不会修改您的环境并且易于使用:

function npm-exec {
   $(npm bin)/$@  
}

This can then be used like this without making any changes to your environment:

然后可以像这样使用它,而无需对您的环境进行任何更改:

npm-exec r.js <args>

回答by k0pernikus

If you want to keep npm, then npxshould do what you need.

如果你想保留 npm,那么npx应该做你需要的。



If switching to yarn (a npm replacement by facebook) is an option for you, then you can call:

如果切换到 yarn(facebook 的 npm 替代品)是您的一个选择,那么您可以调用:

 yarn yourCmd

scripts inside the package.json will take precedence, if none is found it will look inside the ./node_modules/.bin/folder.

package.json 中的脚本将优先,如果没有找到,它将在./node_modules/.bin/文件夹内查找。

It also outputs what it ran:

它还输出它运行的内容:

$ yarn tsc
yarn tsc v0.27.5
$ "/home/philipp/rate-pipeline/node_modules/.bin/tsc"

So you don't have to setup scripts for each command in your package.json.

因此,您不必为 .git 文件中的每个命令设置脚本package.json



If you had a script defined at .scriptsinside your package.json:

如果你.scripts在你的内部定义了一个脚本package.json

"tsc": "tsc" // each command defined in the scripts will be executed from `./node_modules/.bin/` first

yarn tscwould be equivalent to yarn run tscor npm run tsc:

yarn tsc将相当于yarn run tscnpm run tsc

 yarn tsc
 yarn tsc v0.27.5
 $ tsc

回答by Dheeraj Bhaskar

update: If you're on the recent npm (version >5.2)

更新:如果您使用的是最近的 npm(版本 >5.2)

You can use:

您可以使用:

npx <command>

npxlooks for command in .bindirectory of your node_modules

npx.bin您的目录中查找命令node_modules

old answer:

旧答案:

For Windows

对于 Windows

Store the following in a file called npm-exec.batand add it to your %PATH%

将以下内容存储在一个名为的文件中npm-exec.bat并将其添加到您的%PATH%

@echo off
set cmd="npm bin"
FOR /F "tokens=*" %%i IN (' %cmd% ') DO SET modules=%%i
"%modules%"\%*

Usage

用法

Then you can use it like npm-exec <command> <arg0> <arg1> ...

然后你可以像这样使用它 npm-exec <command> <arg0> <arg1> ...

For example

例如

To execute wdioinstalled in local node_modules directory, do:

wdio在本地 node_modules 目录中执行安装,请执行以下操作:

npm-exec wdio wdio.conf.js

i.e. it will run .\node_modules\.bin\wdio wdio.conf.js

即它会运行 .\node_modules\.bin\wdio wdio.conf.js

回答by guneysus

I prefer to not rely on shell aliases or another package.

我不喜欢依赖 shell 别名或其他包。

Adding a simple line to scriptssection of your package.json, you can run local npm commands like

scripts您的 部分添加一个简单的行package.json,您可以运行本地 npm 命令,例如

npm run webpack

npm run webpack

package.json

包.json

{
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "webpack": "webpack"
  },
  "devDependencies": {
    "webpack": "^4.1.1",
    "webpack-cli": "^2.0.11"
  }
}