How to use the local version of the module in node.js. For example, in my application, I installed coffee script:
npm install coffee-script
This will install it in. / node \ u modules, while the coffee command is in. / node \ u modules /. Bin / coffee. Can I run this command when I am in the project's home folder? I guess I'm looking for something similar to bundle exec in bundler. Basically, I want to specify a version of the coffee script that everyone involved in the project should use.
I know I can add the - g flag to install it globally so that coffee works everywhere, but what if I want to use a different version of coffee for each project?
#1 building
Update: as Seyeong Jeong points out in the answer below, starting with npm 5.2.0, you can use npx [command], which is more convenient.
Old answer before 5.2.0:
The problem of putter
./node_modules/.bin
The PATH is entered only if the current working directory is the root directory of the project directory structure (i.e. the location of node_modules)
Regardless of your working directory, you can use the following command to get the path of the locally installed binaries:
npm bin
To execute locally installed coffee binaries independent of your location in the project directory hierarchy, you can use this bash construct
PATH=$(npm bin):$PATH coffee
I call it NPM Exec
alias npm-exec='PATH=$(npm bin):$PATH'
So now I can
npm-exec coffee
No matter where I am, I can run the right 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
#2 building
If you want to update the PATH variable correctly based on the current working directory, add it to the end of. bashrc -equivalent (or after defining all the contents of the 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
Each time you render a bash prompt, this may increase the delay by a small amount (most likely depending on the size of the project), so it is disabled by default.
You can enable and disable it in the terminal by running node mode and node mode off respectively.
#3 building
Use npm bin command to get the node module / bin directory of the project
$ $(npm bin)/<binary-name> [args]
for example
$ $(npm bin)/bower install
#4 building
I have the same problem. I don't particularly like using aliases routine If you don't like them, this is another solution I use. You must first create a small executable bash script, which says 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 "$@"
You can then use any executable locally / bin using the following command:
./setenv.sh <command> ./setenv.sh 6to5-node server.js ./setenv.sh grunt
If you use scripts in package.json:
..., scripts: { 'start': './setenv.sh <command>' }
#5 building
Use NPM run [- Script] < script name >
After using npm to install the bin software package to the local. / node ﹣ modules directory, modify package.json to add < script name > as follows:
$ npm install --save learnyounode $ edit packages.json >>> in packages.json ... "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "learnyounode": "learnyounode" }, ... $ npm run learnyounode
If npm install has the -- add script option or other functions, or npm run can run without adding to the scripts block, that would be good.