如何使用node_modules中本地安装的软件包中的可执行文件? [英] How to use executables from a package installed locally in node_modules?

查看:417
本文介绍了如何使用node_modules中本地安装的软件包中的可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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类似的东西.基本上,我想指定一个咖啡脚本的版本,参与该项目的每个人都应该使用.

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?

推荐答案

更新:Seyeong Jeong在下面的答案中指出,自npm 5.2.0起,您可以使用npx [command],即更方便.

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

5.2.0之前版本的旧答案:

推杆问题

./node_modules/.bin

进入PATH的地方是,它仅在当前工作目录是项目目录结构的根目录(即node_modules的位置)时才起作用

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)

无论您的工作目录是什么,您都可以使用以下方式获取本地安装的二进制文件的路径

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

npm bin

要独立于项目目录层次结构中的位置执行本地安装的coffee二进制文件,可以使用此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

无论我在哪里,都可以正确喝咖啡

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

这篇关于如何使用node_modules中本地安装的软件包中的可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆