How to use executables from a package installed locally in node_modules? - 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
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.
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?
UPDATE: As Seyeong Jeong points out in their answer below, since npm 5.2.0 you can use npx [command], which is more convenient.
OLD ANSWER for versions before 5.2.0:
The problem with putting
./node_modules/.bin
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
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
I aliased this to npm-exec
alias npm-exec='PATH=$(npm bin):$PATH'
So, now I can
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
You don't have to manipulate $PATH anymore!
From npm#5.2.0, npm ships with npx package which lets you run commands from a local node_modules/.bin or from a central cache.
Simply run:
$ npx [options] <command>[#version] [command-arg]...
By default, npx will check whether <command> exists in $PATH, or in the local project binaries, and execute that.
Calling npx <command> when <command> isn't already in your $PATH will automatically install a package with that name from the NPM registry for you, and invoke it. When it's done, the installed package won’t be anywhere in your globals, so you won’t have to worry about pollution in the long-term. You can prevent this behaviour by providing --no-install option.
For npm < 5.2.0, you can install npx package manually by running the following command:
$ npm install -g npx
Use the npm bin command to get the node modules /bin directory of your project
$ $(npm bin)/<binary-name> [args]
e.g.
$ $(npm bin)/bower install
Use npm run[-script] <script name>
After using npm to install the bin package to your local ./node_modules directory, modify package.json to add <script name> like this:
$ npm install --save learnyounode
$ edit packages.json
>>> in packages.json
...
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"learnyounode": "learnyounode"
},
...
$ npm run learnyounode
It would be nice if npm install had a --add-script option or something or if npm run would work without adding to the scripts block.
update: If you're on the recent npm (version >5.2)
You can use:
npx <command>
npx looks for command in .bin directory of your node_modules
old answer:
For Windows
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%"\%*
Usage
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).
Use npm-run.
From the readme:
npm-run
Find & run local executables from node_modules
Any executable available to an npm lifecycle script is available to npm-run.
Usage
$ npm install mocha # mocha installed in ./node_modules
$ npm-run mocha test/* # uses locally installed mocha executable
Installation
$ npm install -g npm-run
TL;DR: Use npm exec with npm#>=7.
The npx command which was mentioned in other answers has been completely rewritten in npm#7 which ships by default with node#15 and can be installed on node#>=10. The implementation is now equal to the newly introduced npm exec command, which is similar but not equal to the previous npx command implementation.
One difference is e.g. that it always interactively asks if a dependency should be downloaded when it is not already installed (can also be overwritten with the params --yes or --no).
Here is an example for npm exec. The double dashes (--) separates the npm exec params from the actual command params:
npm exec --no -- jest --coverage
See also the updated, official documentation to npm exec.
If you want to keep npm, then npx should do what you need.
If switching to yarn (a npm replacement by facebook) is an option for you, then you can call:
yarn yourCmd
scripts inside the package.json will take precedence, if none is found it will look inside the ./node_modules/.bin/ folder.
It also outputs what it ran:
$ yarn tsc
yarn tsc v0.27.5
$ "/home/philipp/rate-pipeline/node_modules/.bin/tsc"
So you don't have to setup scripts for each command in your package.json.
If you had a script defined at .scripts inside your package.json:
"tsc": "tsc" // each command defined in the scripts will be executed from `./node_modules/.bin/` first
yarn tsc would be equivalent to yarn run tsc or npm run tsc:
yarn tsc
yarn tsc v0.27.5
$ tsc
The PATH solution has the issue that if $(npm bin) is placed in your .profile/.bashrc/etc it is evaluated once and is forever set to whichever directory the path was first evaluated in. If instead you modify the current path then every time you run the script your path will grow.
To get around these issues, I create a function and used that. It doesn't modify your environment and is simple to use:
function npm-exec {
$(npm bin)/$#
}
This can then be used like this without making any changes to your environment:
npm-exec r.js <args>
I prefer to not rely on shell aliases or another package.
Adding a simple line to scripts section of your package.json, you can run local npm commands like
npm run webpack
package.json
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "webpack"
},
"devDependencies": {
"webpack": "^4.1.1",
"webpack-cli": "^2.0.11"
}
}
If you want your PATH variable to correctly update based on your current working directory, add this to the end of your .bashrc-equivalent (or after anything that defines 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
This may add a short delay every time the bash prompt gets rendered (depending on the size of your project, most likely), so it's disabled by default.
You can enable and disable it within your terminal by running node-mode and node-mode-off, respectively.
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.
Same #regular 's accepted solution, but Fish shell flavour
if not contains (npm bin) $PATH
set PATH (npm bin) $PATH
end
zxc is like "bundle exec" for nodejs. It is similar to using PATH=$(npm bin):$PATH:
$ npm install -g zxc
$ npm install gulp
$ zxc which gulp
/home/nathan/code/project1/node_modules/.bin/gulp
You can also use direnv and change the $PATH variable only in your working folder.
$ cat .envrc
> export PATH=$(npm bin):$PATH
Add this script to your .bashrc. Then you can call coffee or anyhting locally. This is handy for your laptop, but don't use it on your server.
DEFAULT_PATH=$PATH;
add_local_node_modules_to_path(){
NODE_MODULES='./node_modules/.bin';
if [ -d $NODE_MODULES ]; then
PATH=$DEFAULT_PATH:$NODE_MODULES;
else
PATH=$DEFAULT_PATH;
fi
}
cd () {
builtin cd "$#";
add_local_node_modules_to_path;
}
add_local_node_modules_to_path;
note: this script makes aliase of cd command, and after each call of cd it checks node_modules/.bin and add it to your $PATH.
note2: you can change the third line to NODE_MODULES=$(npm bin);. But that would make cd command too slow.
For Windows use this:
/* cmd into "node_modules" folder */
"%CD%\.bin\grunt" --version
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'd love to know if this is an insecure/bad idea, but after thinking about it a bit I don't see an issue here:
Modifying Linus's insecure solution to add it to the end, using npm bin to find the directory, and making the script only call npm bin when a package.json is present in a parent (for speed), this is what I came up with for zsh:
find-up () {
path=$(pwd)
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
precmd() {
if [ "$(find-up package.json)" != "" ]; then
new_bin=$(npm bin)
if [ "$NODE_MODULES_PATH" != "$new_bin" ]; then
export PATH=${PATH%:$NODE_MODULES_PATH}:$new_bin
export NODE_MODULES_PATH=$new_bin
fi
else
if [ "$NODE_MODULES_PATH" != "" ]; then
export PATH=${PATH%:$NODE_MODULES_PATH}
export NODE_MODULES_PATH=""
fi
fi
}
For bash, instead of using the precmd hook, you can use the $PROMPT_COMMAND variable (I haven't tested this but you get the idea):
__add-node-to-path() {
if [ "$(find-up package.json)" != "" ]; then
new_bin=$(npm bin)
if [ "$NODE_MODULES_PATH" != "$new_bin" ]; then
export PATH=${PATH%:$NODE_MODULES_PATH}:$new_bin
export NODE_MODULES_PATH=$new_bin
fi
else
if [ "$NODE_MODULES_PATH" != "" ]; then
export PATH=${PATH%:$NODE_MODULES_PATH}
export NODE_MODULES_PATH=""
fi
fi
}
export PROMPT_COMMAND="__add-node-to-path"
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.
In case you are using fish shell and do not want to add to $path for security reason. We can add the below function to run local node executables.
### 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
Now you can run thing like:
n coffee
or more arguments like:
n browser-sync --version
Note, if you are bash user, then #Bob9630 answers is the way to go by leveraging bash's $#, which is not available in fishshell.
I propose a new solution I have developed (05/2021)
You can use lpx https://www.npmjs.com/package/lpx to
run a binary found in the local node_modules/.bin folder
run a binary found in the node_modules/.bin of a workspace root from anywhere in the workspace
lpx does not download any package if the binary is not found locally (ie not like npx)
Example :
lpx tsc -b -w will run tsc -b -w with the local typescript package
Include coffee-script in package.json with the specific version required in each project, typically like this:
"dependencies":{
"coffee-script": ">= 1.2.0"
Then run npm install to install dependencies in each project. This will install the specified version of coffee-script which will be accessible locally to each project.
Related
Run Laravel Mix without a global nodejs and npm installation
I have a laravel project and I need to build styles and scripts with laravel-mix, but a testing server (Ubuntu 20.04.4) hasn't a globally installed node. Node and npm are in different folders in the system so I run commands like this: /path/to/node /path/to/npm install /path/to/node /path/to/npm run dev But when I run npm run dev (this command runs laravel-mix build), I see the error: > mazer#2.0.0 dev > mix /usr/bin/env: ‘node’: No such file or directory In the package.json it looks like this: "scripts": { "dev": "mix" ... } I checked the laravel-mix package (in node_modules) and found this: #!/usr/bin/env node. The package checks the node var in this file, but there is no node var. I don't need to change the env file, so how can I change this path or set a temporary system var? Is there any way to simulate that the variable is there?
I have one solution for this problem. The issue regarding naming misspelling or path symlinks. so that you need to link symlinks for nodejs with this command ln -s /usr/bin/nodejs /usr/bin/node or sudo ln -s /usr/bin/nodejs /usr/bin/node
I resolved my issue with Docker, so now I run this command on git push: docker run --rm -v /path/to/project:/var/www/html node:16.16.0-alpine npm run dev --prefix /var/www/html Perhaps it will be useful to someone. UPD I found another way to resolve it, I use PATH incorrectly and for this reason it didn't work: Wrong I set paths to node and npm and then add it to PATH like this: NODE_PATH="/path/to/node_folder/node" NPM_PATH="/path/to/node_folder/npm" PATH="${NODE_PATH}:${NPM_PATH}:$PATH" And the system can't find npm and node anyway. The right way Add /path/to/node_folders (node and npm are in it) to PATH: NODE_DIR="/path/to/node_folder" PATH="${NODE_DIR}:$PATH" And then, I can run just npm install and npm run dev without full paths to them.
Why ts-node index.ts doesn't work, but ts-node through npm run works? [duplicate]
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 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. 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?
UPDATE: As Seyeong Jeong points out in their answer below, since npm 5.2.0 you can use npx [command], which is more convenient. OLD ANSWER for versions before 5.2.0: The problem with putting ./node_modules/.bin 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 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 I aliased this to npm-exec alias npm-exec='PATH=$(npm bin):$PATH' So, now I can 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
You don't have to manipulate $PATH anymore! From npm#5.2.0, npm ships with npx package which lets you run commands from a local node_modules/.bin or from a central cache. Simply run: $ npx [options] <command>[#version] [command-arg]... By default, npx will check whether <command> exists in $PATH, or in the local project binaries, and execute that. Calling npx <command> when <command> isn't already in your $PATH will automatically install a package with that name from the NPM registry for you, and invoke it. When it's done, the installed package won’t be anywhere in your globals, so you won’t have to worry about pollution in the long-term. You can prevent this behaviour by providing --no-install option. For npm < 5.2.0, you can install npx package manually by running the following command: $ npm install -g npx
Use the npm bin command to get the node modules /bin directory of your project $ $(npm bin)/<binary-name> [args] e.g. $ $(npm bin)/bower install
Use npm run[-script] <script name> After using npm to install the bin package to your local ./node_modules directory, modify package.json to add <script name> like this: $ npm install --save learnyounode $ edit packages.json >>> in packages.json ... "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "learnyounode": "learnyounode" }, ... $ npm run learnyounode It would be nice if npm install had a --add-script option or something or if npm run would work without adding to the scripts block.
update: If you're on the recent npm (version >5.2) You can use: npx <command> npx looks for command in .bin directory of your node_modules old answer: For Windows 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%"\%* Usage 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).
Use npm-run. From the readme: npm-run Find & run local executables from node_modules Any executable available to an npm lifecycle script is available to npm-run. Usage $ npm install mocha # mocha installed in ./node_modules $ npm-run mocha test/* # uses locally installed mocha executable Installation $ npm install -g npm-run
TL;DR: Use npm exec with npm#>=7. The npx command which was mentioned in other answers has been completely rewritten in npm#7 which ships by default with node#15 and can be installed on node#>=10. The implementation is now equal to the newly introduced npm exec command, which is similar but not equal to the previous npx command implementation. One difference is e.g. that it always interactively asks if a dependency should be downloaded when it is not already installed (can also be overwritten with the params --yes or --no). Here is an example for npm exec. The double dashes (--) separates the npm exec params from the actual command params: npm exec --no -- jest --coverage See also the updated, official documentation to npm exec.
If you want to keep npm, then npx should do what you need. If switching to yarn (a npm replacement by facebook) is an option for you, then you can call: yarn yourCmd scripts inside the package.json will take precedence, if none is found it will look inside the ./node_modules/.bin/ folder. It also outputs what it ran: $ yarn tsc yarn tsc v0.27.5 $ "/home/philipp/rate-pipeline/node_modules/.bin/tsc" So you don't have to setup scripts for each command in your package.json. If you had a script defined at .scripts inside your package.json: "tsc": "tsc" // each command defined in the scripts will be executed from `./node_modules/.bin/` first yarn tsc would be equivalent to yarn run tsc or npm run tsc: yarn tsc yarn tsc v0.27.5 $ tsc
The PATH solution has the issue that if $(npm bin) is placed in your .profile/.bashrc/etc it is evaluated once and is forever set to whichever directory the path was first evaluated in. If instead you modify the current path then every time you run the script your path will grow. To get around these issues, I create a function and used that. It doesn't modify your environment and is simple to use: function npm-exec { $(npm bin)/$# } This can then be used like this without making any changes to your environment: npm-exec r.js <args>
I prefer to not rely on shell aliases or another package. Adding a simple line to scripts section of your package.json, you can run local npm commands like npm run webpack package.json { "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "webpack": "webpack" }, "devDependencies": { "webpack": "^4.1.1", "webpack-cli": "^2.0.11" } }
If you want your PATH variable to correctly update based on your current working directory, add this to the end of your .bashrc-equivalent (or after anything that defines 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 This may add a short delay every time the bash prompt gets rendered (depending on the size of your project, most likely), so it's disabled by default. You can enable and disable it within your terminal by running node-mode and node-mode-off, respectively.
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.
Same #regular 's accepted solution, but Fish shell flavour if not contains (npm bin) $PATH set PATH (npm bin) $PATH end
zxc is like "bundle exec" for nodejs. It is similar to using PATH=$(npm bin):$PATH: $ npm install -g zxc $ npm install gulp $ zxc which gulp /home/nathan/code/project1/node_modules/.bin/gulp
You can also use direnv and change the $PATH variable only in your working folder. $ cat .envrc > export PATH=$(npm bin):$PATH
Add this script to your .bashrc. Then you can call coffee or anyhting locally. This is handy for your laptop, but don't use it on your server. DEFAULT_PATH=$PATH; add_local_node_modules_to_path(){ NODE_MODULES='./node_modules/.bin'; if [ -d $NODE_MODULES ]; then PATH=$DEFAULT_PATH:$NODE_MODULES; else PATH=$DEFAULT_PATH; fi } cd () { builtin cd "$#"; add_local_node_modules_to_path; } add_local_node_modules_to_path; note: this script makes aliase of cd command, and after each call of cd it checks node_modules/.bin and add it to your $PATH. note2: you can change the third line to NODE_MODULES=$(npm bin);. But that would make cd command too slow.
For Windows use this: /* cmd into "node_modules" folder */ "%CD%\.bin\grunt" --version
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'd love to know if this is an insecure/bad idea, but after thinking about it a bit I don't see an issue here: Modifying Linus's insecure solution to add it to the end, using npm bin to find the directory, and making the script only call npm bin when a package.json is present in a parent (for speed), this is what I came up with for zsh: find-up () { path=$(pwd) while [[ "$path" != "" && ! -e "$path/$1" ]]; do path=${path%/*} done echo "$path" } precmd() { if [ "$(find-up package.json)" != "" ]; then new_bin=$(npm bin) if [ "$NODE_MODULES_PATH" != "$new_bin" ]; then export PATH=${PATH%:$NODE_MODULES_PATH}:$new_bin export NODE_MODULES_PATH=$new_bin fi else if [ "$NODE_MODULES_PATH" != "" ]; then export PATH=${PATH%:$NODE_MODULES_PATH} export NODE_MODULES_PATH="" fi fi } For bash, instead of using the precmd hook, you can use the $PROMPT_COMMAND variable (I haven't tested this but you get the idea): __add-node-to-path() { if [ "$(find-up package.json)" != "" ]; then new_bin=$(npm bin) if [ "$NODE_MODULES_PATH" != "$new_bin" ]; then export PATH=${PATH%:$NODE_MODULES_PATH}:$new_bin export NODE_MODULES_PATH=$new_bin fi else if [ "$NODE_MODULES_PATH" != "" ]; then export PATH=${PATH%:$NODE_MODULES_PATH} export NODE_MODULES_PATH="" fi fi } export PROMPT_COMMAND="__add-node-to-path"
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.
In case you are using fish shell and do not want to add to $path for security reason. We can add the below function to run local node executables. ### 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 Now you can run thing like: n coffee or more arguments like: n browser-sync --version Note, if you are bash user, then #Bob9630 answers is the way to go by leveraging bash's $#, which is not available in fishshell.
I propose a new solution I have developed (05/2021) You can use lpx https://www.npmjs.com/package/lpx to run a binary found in the local node_modules/.bin folder run a binary found in the node_modules/.bin of a workspace root from anywhere in the workspace lpx does not download any package if the binary is not found locally (ie not like npx) Example : lpx tsc -b -w will run tsc -b -w with the local typescript package
Include coffee-script in package.json with the specific version required in each project, typically like this: "dependencies":{ "coffee-script": ">= 1.2.0" Then run npm install to install dependencies in each project. This will install the specified version of coffee-script which will be accessible locally to each project.
How to make bin script available for npm package installed locally
I read on the npm documentation that you can't use bin scripts of locally installed packages. So, how gulp can be launched as bin command when installed locally? What's making it available when locally installed, I reviewed the gulp package.json and the bin scripts, I don't found any answer.
From NPMJS documentation: To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs. So, your locally installed packages binaries will be executable like this ./bin/node_modules/.bin/the_binary This is if you want to launch the binary directly. Or, as specified in the scripts part of the documentation: In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. Thus, you can simply write a wrapper script like scripts: { "build": "the_binary" } and call your script like this npm run build Bonus As of npm#2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script: npm run test -- --grep="pattern"
You can use lpx https://www.npmjs.com/package/lpx to run a binary found in the local node_modules/.bin folder run a binary found in the node_modules/.bin of a workspace root from anywhere in the workspace lpx does not download any package if the binary is not found locally (ie not like npx) Example : lpx tsc -b -w will run tsc -b -w with the local typescript package
CircleCI not caching my globally installed node module?
I am trying to cache a command line tool needed for my build process. The tool is made out of NodeJS. The build succeeds, but I need it to run faster. The relevant parts of my circle.yml look like this : dependencies: post: - npm -g list - if [ $(npm -g list | grep -c starrynight) -lt 1 ]; then npm install -g starrynight; else echo "StarryNight seems to be cached"; fi test: override: - npm -g list - starrynight run-tests --framework nightwatch The second npm -g list shows starrynight available for use, but the first one shows that it is not being cached. echo $(npm prefix -g) . . . gets me . . . /home/ubuntu/nvm/v0.10.33 . . . so I am assuming CircleCI doesn't cache anything installed globally into nvm. Nothing I have tried gets me my message, "StarryNight seems to be cached". How can I cache starrynight?
Ok, I figured this out. Thanks to Hirokuni Kim of CircleCI for pointing me in the right direction. The relevant bits of the new circle.yml looks like this : machine: node: version: 0.10.33 dependencies: cache_directories: - ~/nvm/v0.10.33/lib/node_modules/starrynight - ~/nvm/v0.10.33/bin/starrynight pre: - if [ ! -e ~/nvm/v0.10.33/bin/starrynight ]; then npm install -g starrynight; else echo "Starrynight seems to be cached"; fi; Hirokuni suggested caching ~/nvm but cache retrieval took as long as the build, since it restores every available version of nodejs. I had tried previously to cache just ~/nvm/v0.10.33/lib/node_modules/starrynight on its own, without realizing that the sister 'directory' bin/starrynight is actually an essential symlink to the entry point of the module. My working assumption is that NodeJS modules run from the command line through a series of symbolic references, probably as follows. . . npm install -g starrynight creates two new artifacts: an environment alias for npm named starrynight a symlink in the ${prefix}/bin directory, which points to the entry point file, starrynight.js specified with the bin key in package.json. When the user types starrynight as a CLI command the shell interprets it as an alias for npm and executes it. npm examines $0, gets starrynight, and starts up nodejs with the symlink ${prefix}/bin/starrynight as the module to execute. That symlink refers to ~/nvm/v0.10.33/lib/node_modules/starrynight where the real action takes place. In short, it is necessary to cache both ${prefix}/lib/node_modules/xxx and ${prefix}/bin/xxx
The path in the accepted answer doesn't seem to work anymore. I've checked on the build instance that now global npm packages are in /opt/circleci/nodejs/<version> so I've added to the circle.yml the following: - "/opt/circleci/nodejs/v4.3.2/lib/node_modules" - "/opt/circleci/nodejs/v4.3.2/bin"
NPM : how to source ./node_modules/.bin folder?
I have a problem on npm installation I have created a project say project A cd ~/projectA npm install sails but sails command is not found after installation. I know that it is successfully install in ~/projectA/node_modules directory. but the executable cannot be sourced. And i know it is installed ~/projectA/node_modules/.bin How can I source the .bin automatically whenever I enter into this projectA folder? Did I did something wrong?
I can give you an inelegant solution, which is working for me. I just exported my path in my .bashrc file. export PATH="$PATH:./node_modules/.bin" Edit: It's been a while, but I have changed my strategy to use npm scripts instead. In the above case, setup package.json as follows: "scripts": { "sails": "sails" } Which you can then run with npm run sails or with arguments npm run sails -- <args>
You should use the npm bin command to get an absolute path to your current node bin directory. For example: ➤ lessc bash: lessc: command not found ➤ npm bin /home/brice/[...]/node_modules/.bin ➤ export PATH=$(npm bin):$PATH ➤ lessc --version lessc 1.7.3 (Less Compiler) [JavaScript] This avoids the problem of relative paths, especially if you're going to be using this in a build system that will invoke the command in subdirectories.
A bit more robust is: export PATH=$(npm bin):$PATH You can either run it, add it to your shell profile, or create an alias like: alias snpm='export PATH=$(npm bin):$PATH' If you do go the alias route, be sure to use single quotes so it delays the execution of the variables!
To use on the command line like sails generate foo you will need to install the npm module globally. npm install -g sails You could also use the path to the bin in the command if you don't want to install globally: ./node_modules/sails/bin/sails.js generate foo
The official instructions for sails (https://github.com/balderdashy/sails) advises To install the latest stable release with the command-line tool: sudo npm -g install sails This installs globally and adds to a directory like /usr/local/bin that should be in your $PATH. But to answer the general question regarding the location of the binaries if you install locally, they should be placed in ./node_modules/.bin directory (so run ./node_modules/.bin/sails ...)
If you don't like to mess up with your PATH for running a npm script that isn't global -- e.g. you are the only one to use it --, I would personally recommend the use of an sh "alias". npm install (locally) your beloved package (json-diff here, for instance) cd ~ && npm install json-diff alias it (save it in your ~/.xxxxrc file): alias diffj "\`npm bin\`/json-diff !*" Then, for diffing 2 json's: diffj old.json new.json
In my ~/.bashrc, I have the following: function nbin { local dir; dir=$(npm bin) if [ -d "$dir" ]; then ( # subshell to not change this shell's $PATH PATH=$dir:$PATH eval "$#" ) else echo "\"$dir\" is not an npm binary directory." >&1 return 1 fi } I can then run executable foo in the .bin directory as: nbin foo
Possible workaround with NPM 5.2+ using the npx command. npx sails new test-project See this question for a similar use case and elegant solutions.