如何在node.js中使用一个模块的本地版本。例如,在我的应用程序中,我安装了coffee-script:

npm install coffee-script

这会将其安装在。/node_modules中,而coffee命令则安装在。/node_modules/.bin/coffee中。当我在项目的主文件夹中时,是否有一种方法可以运行此命令?我想我在寻找类似于捆绑执行者的东西。基本上,我想指定一个参与项目的每个人都应该使用的coffee-script版本。

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


当前回答

如果你想让你的PATH变量根据你当前的工作目录正确地更新,把它添加到你的.bashrc等价文件的末尾(或者任何定义PATH的东西之后):

__OLD_PATH=$PATH
function updatePATHForNPM() {
  export PATH=$(npm bin):$__OLD_PATH
}

function node-mode() {
  PROMPT_COMMAND=updatePATHForNPM
}

function node-mode-off() {
  unset PROMPT_COMMAND
  PATH=$__OLD_PATH
}

# Uncomment to enable node-mode by default:
# node-mode

这可能会在每次呈现bash提示符时增加一个短延迟(很可能取决于项目的大小),因此默认情况下禁用它。

您可以在终端中通过分别运行node-mode和node-mode-off来启用和禁用它。

其他回答

我遇到了同样的问题,我不特别喜欢使用别名(正如常规的建议),如果你也不喜欢它们,那么这里有另一个我使用的解决方案,你首先必须创建一个小的可执行bash脚本,说setenv.sh:

#!/bin/sh

# Add your local node_modules bin to the path
export PATH="$(npm bin):$PATH"

# execute the rest of the command
exec "$@"

然后,您可以使用以下命令使用本地/bin中的任何可执行文件:

./setenv.sh <command>
./setenv.sh 6to5-node server.js
./setenv.sh grunt

如果你在包中使用脚本。json:

...,
scripts: {
    'start': './setenv.sh <command>'
}

更新:我不再推荐这种方法,既是因为上面提到的安全原因,也是因为更新的npm bin命令。原答案如下:

正如您所发现的,任何本地安装的二进制文件都在./node_modules/.bin中。为了总是在这个目录下运行二进制文件,而不是全局可用的二进制文件,如果存在,我建议你把./node_modules/.bin放在你的路径的前面:

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

如果你把这个放在~/。配置文件,coffee将永远是。/node_modules/.bin/coffee(如果可用的话),否则是/usr/local/bin/coffee(或任何你安装节点模块的前缀)。

如果您正在使用fish shell,并且出于安全原因不想添加到$path。我们可以添加下面的函数来运行本地节点可执行文件。

### run executables in node_module/.bin directory
function n 
  set -l npmbin (npm bin)   
  set -l argvCount (count $argv)
  switch $argvCount
    case 0
      echo please specify the local node executable as 1st argument
    case 1
      # for one argument, we can eval directly 
      eval $npmbin/$argv
    case '*'
      set --local executable $argv[1]
      # for 2 or more arguments we cannot append directly after the $npmbin/ since the fish will apply each array element after the the start string: $npmbin/arg1 $npmbin/arg2... 
      # This is just how fish interoperate array. 
      set --erase argv[1]
      eval $npmbin/$executable $argv 
  end
end

现在你可以这样运行:

n咖啡

或者更多像这样的论点:

N浏览器同步——版本

注意,如果您是bash用户,则可以使用bash的$@来回答@ bob9630,这在fishshell中是不可用的。

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


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

 yarn yourCmd

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

它还输出它运行的内容:

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

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


如果你在package.json中有一个定义在.scripts的脚本:

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

纱线TSC相当于纱线运行TSC或NPM运行TSC:

 yarn tsc
 yarn tsc v0.27.5
 $ tsc

使用npm-run。

自述:

NPM 运行

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

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

使用

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

安装

$ npm install -g npm-run