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
I encountered the same problem and I don't particularly like using aliases (as regular's suggested), and if you don't like them too then here's another workaround that I use, you first have to create a tiny executable bash script, say 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 "$@"
and then you can then use any executables in your local /bin
using this command:
./setenv.sh <command>
./setenv.sh 6to5-node server.js
./setenv.sh grunt
If you're using scripts
in package.json then:
...,
scripts: {
'start': './setenv.sh <command>'
}
I've always used the same approach as @guneysus to solve this problem, which is creating a script in the package.json file and use it running npm run script-name.
However, in the recent months I've been using npx and I love it.
For example, I downloaded an Angular project and I didn't want to install the Angular CLI globally. So, with npx installed, instead of using the global angular cli command (if I had installed it) like this:
ng serve
I can do this from the console:
npx ng serve
Here's an article I wrote about NPX and that goes deeper into it.
I am a Windows
user and this is what worked for me:
// First set some variable - i.e. replace is with "xo"
D:\project\root> set xo="./node_modules/.bin/"
// Next, work with it
D:\project\root> %xo%/bower install
Good Luck.
You can use:
npx <command>
npx
looks for command in .bin
directory of your node_modules
Store the following in a file called npm-exec.bat
and add it to your %PATH%
@echo off
set cmd="npm bin"
FOR /F "tokens=*" %%i IN (' %cmd% ') DO SET modules=%%i
"%modules%"\%*
Then you can use it like
npm-exec <command> <arg0> <arg1> ...
For example
To execute wdio
installed in local node_modules directory, do:
npm-exec wdio wdio.conf.js
i.e. it will run .\node_modules\.bin\wdio wdio.conf.js
Update: I no longer recommend this method, both for the mentioned security reasons and not the least the newer npm bin
command. Original answer below:
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/.bin
first in your path:
export PATH="./node_modules/.bin:$PATH"
If you put this in your ~/.profile
, coffee
will always be ./node_modules/.bin/coffee
if available, otherwise /usr/local/bin/coffee
(or whatever prefix you are installing node modules under).
Same @regular 's accepted solution, but Fish shell flavour
if not contains (npm bin) $PATH
set PATH (npm bin) $PATH
end