问题描述
如何在 node.js
中使用本地版本的模块。例如,在我的应用程序中,我安装了咖啡脚本:
How do I use a local version of a module in node.js
. For example, in my app, I installed coffee-script:
npm install coffee-script
这会安装在 ./ node_modules
中,并且coffee命令位于 ./ node_modules / .bin / coffee
。有一种方法来运行这个命令,当我在我的项目的主文件夹中?我想我在寻找类似于 bundle exec
在bundler。基本上,我想指定一个版本的咖啡脚本,每个人都参与该项目应该使用。
This installs it in ./node_modules
and 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 exec
in bundler. Basically, I'd like to specify a version of coffee-script that everyone involved with the project should use.
我知道我可以添加 -g
标志在全球安装,所以咖啡在任何地方都可以正常工作,但如果我想要每个项目有不同版本的咖啡怎么办?
I know I can add the -g
flag to install it globally so coffee works fine anywhere, but what if I wanted to have different versions of coffee per project?
推荐答案
放置
./node_modules/.bin
到你的PATH中是,只有当你当前的工作目录是根目录,如果你的项目目录结构c $ c> node_modules )
into your PATH is that it only works when your current working directory is the root if your project directory structure (i.e. the location of node_modules
)
不管你的工作目录是什么,你可以得到本地安装的二进制文件的路径
Independent of what your working directory is, you can get the path of locally installed binaries with
npm bin
要执行本地安装的咖啡
独立于您在项目目录层次结构中的位置,您可以使用此bash结构
To execute a locally installed coffee
binary independent of where you are in the project directory hierarchy you can use this bash construct
PATH=$(npm bin):$PATH coffee
我别名为npm-exec
I aliased this to npm-exec
alias npm-exec='PATH=$(npm bin):$PATH'
现在我可以
npm-exec coffee
运行正确的咖啡副本
$ 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
这篇关于如何使用本地安装在node_modules中的软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!