How to use package installed locally in node_modules?
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?
node.js coffeescript npm node-modules
add a comment |
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?
node.js coffeescript npm node-modules
6
A lot of instructions I read say things likenpm install niftycommand
and thenniftycommand
. But this will never work unless you have ./node_modules/.bin in your path, will it?
– Bennett McElwee
Sep 20 '16 at 5:03
1
There is a very good writeup here: firstdoit.com/… — Basically it recommends you to put yourcoffee
command into thenpm scripts
section, like"build": "coffee -co target/directory source/directoy", so you can run
npm run build` from the terminal afterwards.
– Benny Neugebauer
Jan 22 '17 at 21:27
@BennyNeugebauer indeed, that's what I've been doing lately instead of messing with PATH
– typeoneerror
Feb 2 '17 at 17:36
3
Usenpx
which comes withnpm 5.2.0
medium.com/@maybekatz/…
– onmyway133
Mar 14 at 13:18
add a comment |
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?
node.js coffeescript npm node-modules
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?
node.js coffeescript npm node-modules
node.js coffeescript npm node-modules
edited Sep 29 '15 at 8:49
kenorb
64.6k27388391
64.6k27388391
asked Mar 13 '12 at 7:31
typeoneerror
32.8k30115201
32.8k30115201
6
A lot of instructions I read say things likenpm install niftycommand
and thenniftycommand
. But this will never work unless you have ./node_modules/.bin in your path, will it?
– Bennett McElwee
Sep 20 '16 at 5:03
1
There is a very good writeup here: firstdoit.com/… — Basically it recommends you to put yourcoffee
command into thenpm scripts
section, like"build": "coffee -co target/directory source/directoy", so you can run
npm run build` from the terminal afterwards.
– Benny Neugebauer
Jan 22 '17 at 21:27
@BennyNeugebauer indeed, that's what I've been doing lately instead of messing with PATH
– typeoneerror
Feb 2 '17 at 17:36
3
Usenpx
which comes withnpm 5.2.0
medium.com/@maybekatz/…
– onmyway133
Mar 14 at 13:18
add a comment |
6
A lot of instructions I read say things likenpm install niftycommand
and thenniftycommand
. But this will never work unless you have ./node_modules/.bin in your path, will it?
– Bennett McElwee
Sep 20 '16 at 5:03
1
There is a very good writeup here: firstdoit.com/… — Basically it recommends you to put yourcoffee
command into thenpm scripts
section, like"build": "coffee -co target/directory source/directoy", so you can run
npm run build` from the terminal afterwards.
– Benny Neugebauer
Jan 22 '17 at 21:27
@BennyNeugebauer indeed, that's what I've been doing lately instead of messing with PATH
– typeoneerror
Feb 2 '17 at 17:36
3
Usenpx
which comes withnpm 5.2.0
medium.com/@maybekatz/…
– onmyway133
Mar 14 at 13:18
6
6
A lot of instructions I read say things like
npm install niftycommand
and then niftycommand
. But this will never work unless you have ./node_modules/.bin in your path, will it?– Bennett McElwee
Sep 20 '16 at 5:03
A lot of instructions I read say things like
npm install niftycommand
and then niftycommand
. But this will never work unless you have ./node_modules/.bin in your path, will it?– Bennett McElwee
Sep 20 '16 at 5:03
1
1
There is a very good writeup here: firstdoit.com/… — Basically it recommends you to put your
coffee
command into the npm scripts
section, like "build": "coffee -co target/directory source/directoy", so you can run
npm run build` from the terminal afterwards.– Benny Neugebauer
Jan 22 '17 at 21:27
There is a very good writeup here: firstdoit.com/… — Basically it recommends you to put your
coffee
command into the npm scripts
section, like "build": "coffee -co target/directory source/directoy", so you can run
npm run build` from the terminal afterwards.– Benny Neugebauer
Jan 22 '17 at 21:27
@BennyNeugebauer indeed, that's what I've been doing lately instead of messing with PATH
– typeoneerror
Feb 2 '17 at 17:36
@BennyNeugebauer indeed, that's what I've been doing lately instead of messing with PATH
– typeoneerror
Feb 2 '17 at 17:36
3
3
Use
npx
which comes with npm 5.2.0
medium.com/@maybekatz/…– onmyway133
Mar 14 at 13:18
Use
npx
which comes with npm 5.2.0
medium.com/@maybekatz/…– onmyway133
Mar 14 at 13:18
add a comment |
22 Answers
22
active
oldest
votes
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
17
you can even go one step further andalias coffee="npm-exec coffee"
– regular
Mar 18 '13 at 6:12
6
The output changes when you cd into another project. It does not change when you cd within a project.npm bin
searches the chain of 'ancestor directories' to the cwd for a node_modules directory. This is exactly the desired behavior if you specifically want to use the binaries of modules listed in the project's package.json.
– regular
Oct 3 '13 at 9:12
9
oh gosh! do I really have to do something like that in order to have my local modules working? it's quite impracticable to explain it to a team! there's nothing a little bit more straightforward?
– Alexian
Apr 24 '15 at 17:12
14
You can always use npm scripts since they always search the local binaries first. You can set up aliases to each of your binaries there or just use generic names like "build".
– Joe Zim
Sep 22 '15 at 1:47
6
@philosodad, actually no, you don't. ThePATH
will be back to what it was before the command invocation. Setting an environment variable in the same line, before running a command only affects that command's environment.
– regular
Sep 22 '15 at 6:28
|
show 11 more comments
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
27
Definitely should be the modern accepted answer!
– Mike Rapadas
Sep 27 '17 at 8:22
10
I think this needs to be higher up on the thread.
– wpcarro
Jan 5 at 19:51
I dont like installing 3rd party global npm packages whilenpm
andpackage.json
provides nearly same functionality.
– guneysus
Mar 28 at 6:27
3
This saved me a ton of headache! How is this not the accepted answer? :-)
– JackWilson
Jun 1 at 9:13
If "Path must be a string. Received undefined" message appears, here is a fix: github.com/zkat/npx/issues/144#issuecomment-391031816
– Valeriy Katkov
Aug 2 at 15:56
|
show 1 more comment
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
4
I like this simple and generic solution. Makes an alias seem unnecessary.
– Matt Montag
May 31 '15 at 22:13
Seems to be the next best solution that is elegant and more secure than having to doexport PATH="./node_modules/.bin:$PATH"
– jontsai
Sep 9 '15 at 23:16
1
@inf3rno the command is$(npm bin)/jasmine
, notnode $(npm bin)/jasmine
(you probably figured it out but clarifying for others).
– jassa
Aug 19 '16 at 19:32
5
Not a bad solution, but it doesn't run on a standard windows command line with $. Putting it in the package.json scripts section is a better approach I feel, since it's more compatible.
– Timothy Gonzalez
Sep 30 '16 at 22:26
add a comment |
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.
5
I found this approach to be more uniform when dealing with multiple developers on a project - it avoids the need to configure anything locally... you justnpm install
then you have access to your dev dependencies. The only minor down side is you need tonpm run eslint
(or whatever). You can create a script called "start" that runs gulp so that you only need to typenpm start
to start your dev server. Pretty cool stuff and no bash goodness, so your windows friends still like you. :)
– jpoveda
Sep 21 '15 at 21:28
1
adding an alias to put $(npm bin) on your path is clever, but the fact that this will work for people without local config wins my heart
– Conrad.Dean
Mar 25 '16 at 12:37
8
this needs more upvotes! Pass args to your scripts after--
like:npm run learnyounode -- --normal-switches --watch -d *.js
– ptim
Jun 17 '16 at 8:53
I also find this the best solution. There is an in-depth explanation here: lostechies.com/derickbailey/2012/04/24/…
– adampasz
Jul 24 '16 at 4:41
Wow, I can't believe I looked passed the scripts section. This is truly a good solution.
– Timothy Gonzalez
Sep 30 '16 at 22:23
add a comment |
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
6
No longer, see npx referenced above... stackoverflow.com/a/45164863/3246805
– t.j.
Sep 1 '17 at 19:00
add a comment |
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).
1
that's probably the best solution. I also created a bash script called "watch" in my project:./node_modules/.bin/coffee --output lib/ --compile --bare --watch src
– typeoneerror
Mar 13 '12 at 18:27
66
Danger, Will Robinson! Use of relative paths in your $PATH opens up a security hole the size of a planet, especially if you put them right up front as the first item. If the directory you're in is writable by everyone (say somewhere in/tmp
), any process or user can hijack your session by putting malicious versions of ordinary commands (likels
,cp
, etc.) there. These may spawn 'invisible' sub-shells capturing your passwords, etcetera.
– ack
Feb 28 '14 at 20:40
will only work in the root and no other places. thealias npm-exec='PATH=$(npm bin):$PATH'
is slicker.
– oligofren
May 5 '15 at 16:21
1
How bad is this if you don't put it as the first thing in yourPATH
, but the last (using the$(npm bin)
form)? so they can't overwrite your existing stuff, and you would have been trusting the executables in thenpm bin
directory already regardless of thePATH
var; would the threat model be that a) someone malicious gets access to your file system, b) they add executables with names that are close to those system tools, and c) you mistype? Trying to understand scenarios that make this bad, given that you're already trusting foreign executables when usingnpm
-installed programs.
– Omar Diab
Jul 15 '16 at 1:24
You can do shell tricks with an alias and you can path manually and this "works" but it is not quite ideal.
– al_gor_rithm
Dec 18 '17 at 19:42
add a comment |
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>
2
I like this! I simply named my functionn
– jontsai
Sep 9 '15 at 23:20
This is great! Thanks for sharing. I added a fish shell version below.
– Leon li
May 19 '17 at 16:49
add a comment |
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
add a comment |
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.
add a comment |
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"
}
}
add a comment |
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
https://medium.com/ninjadevs/with-npx-you-can-forget-about-installing-unnecessary-npm-packages-globally-and-do-even-more-a28b797ea449
add a comment |
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
add a comment |
Same @regular 's accepted solution, but Fish shell flavour
if not contains (npm bin) $PATH
set PATH (npm bin) $PATH
end
add a comment |
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.binwdio wdio.conf.js
This doesn't work while passing more than 1 argument. E.g. npm-exec gulp <some_task>
– OK999
Oct 4 '16 at 15:55
@OK9999 I'm sure some minor modification will allow passing arguments (because when you pass it here, it's coming in quoted in ""); What I suggest is copy paste the gulp file from bin to your project root (some modifications needed of the file, but it will just work without writing new code etc)
– Dheeraj Bhaskar
Oct 11 '16 at 18:45
Yes, i ended up doing so. The node_modules folder have to be in the folder where the gulpfile exist
– OK999
Oct 11 '16 at 18:53
add a comment |
You can also use direnv and change the $PATH variable only in your working folder.
$ cat .envrc
> export PATH=$(npm bin):$PATH
use with direnv. direnv.net
– kuboon
Jan 12 '17 at 9:31
add a comment |
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.
1
Use$(npm bin)
instead of hardcoding./node_modules/.bin
.
– bfontaine
Jan 25 '17 at 17:27
That makes sense. I've updated the code above.
– Tsutomu Kawamura
Jan 26 '17 at 2:12
Hmm,$(npm bin)
seems too slow to use with eachcd
command. I've restore the code and added a note for it.
– Tsutomu Kawamura
Jan 26 '17 at 2:28
add a comment |
For Windows use this:
/* cmd into "node_modules" folder */
"%CD%.bingrunt" --version
add a comment |
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>'
}
2
this setenv script isn't necessary for package.json scripts. npm already prepends the local node_modules/.bin directory to path for you when executing npm run {scripts}.
– jasonkarns
Feb 6 '15 at 19:39
add a comment |
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"
Addingnpm bin
to end of$PATH
may not execute what user expects: basically another executable but more probably a globally installed package with another version !
– LoganMzz
Nov 8 '17 at 13:46
add a comment |
I am a Windows
user and this is what worked for me:
// First set some variable - i.e. replace is with "xo"
D:projectroot> set xo="./node_modules/.bin/"
// Next, work with it
D:projectroot> %xo%/bower install
Good Luck.
add a comment |
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
.
add a comment |
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.
yeah, I got that far as I stated in my question. how do I specifically call the one in my project besides ./node_modules/.bin/coffee?
– typeoneerror
Mar 13 '12 at 9:47
If you've run npm install with the package.json in your project's main folder, you should have a ./node_modules/.bin/coffee folder in this folder. Using ./node_modules/coffee-script/bin/coffee will run the local version of coffee while just running coffee will run the global installation. If you have another version of coffee installed in another path within this project folder, you can access it using ./path/to/this/installation/coffee.
– almypal
Mar 13 '12 at 10:42
This did not work for me. I am trying to use "svgo", and it only works when installed globally. I have triednpm install svgo
as well asnpm install
with package.json. Both methods installed "successfully", but the "svgo" command is still not available.
– Ryan Wheale
Apr 5 '13 at 4:37
1
Grunt uses this in a clever way, and IMHO so should other packages. First you install thegrunt-cli
package globally, then in your project directory install any (modified) version of thegrunt
package, then when you rungrunt
, it will use this local version.
– ack
Feb 28 '14 at 21:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9679932%2fhow-to-use-package-installed-locally-in-node-modules%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
22 Answers
22
active
oldest
votes
22 Answers
22
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
17
you can even go one step further andalias coffee="npm-exec coffee"
– regular
Mar 18 '13 at 6:12
6
The output changes when you cd into another project. It does not change when you cd within a project.npm bin
searches the chain of 'ancestor directories' to the cwd for a node_modules directory. This is exactly the desired behavior if you specifically want to use the binaries of modules listed in the project's package.json.
– regular
Oct 3 '13 at 9:12
9
oh gosh! do I really have to do something like that in order to have my local modules working? it's quite impracticable to explain it to a team! there's nothing a little bit more straightforward?
– Alexian
Apr 24 '15 at 17:12
14
You can always use npm scripts since they always search the local binaries first. You can set up aliases to each of your binaries there or just use generic names like "build".
– Joe Zim
Sep 22 '15 at 1:47
6
@philosodad, actually no, you don't. ThePATH
will be back to what it was before the command invocation. Setting an environment variable in the same line, before running a command only affects that command's environment.
– regular
Sep 22 '15 at 6:28
|
show 11 more comments
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
17
you can even go one step further andalias coffee="npm-exec coffee"
– regular
Mar 18 '13 at 6:12
6
The output changes when you cd into another project. It does not change when you cd within a project.npm bin
searches the chain of 'ancestor directories' to the cwd for a node_modules directory. This is exactly the desired behavior if you specifically want to use the binaries of modules listed in the project's package.json.
– regular
Oct 3 '13 at 9:12
9
oh gosh! do I really have to do something like that in order to have my local modules working? it's quite impracticable to explain it to a team! there's nothing a little bit more straightforward?
– Alexian
Apr 24 '15 at 17:12
14
You can always use npm scripts since they always search the local binaries first. You can set up aliases to each of your binaries there or just use generic names like "build".
– Joe Zim
Sep 22 '15 at 1:47
6
@philosodad, actually no, you don't. ThePATH
will be back to what it was before the command invocation. Setting an environment variable in the same line, before running a command only affects that command's environment.
– regular
Sep 22 '15 at 6:28
|
show 11 more comments
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
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
edited Oct 17 at 3:30
answered Mar 1 '13 at 12:06
regular
5,0171918
5,0171918
17
you can even go one step further andalias coffee="npm-exec coffee"
– regular
Mar 18 '13 at 6:12
6
The output changes when you cd into another project. It does not change when you cd within a project.npm bin
searches the chain of 'ancestor directories' to the cwd for a node_modules directory. This is exactly the desired behavior if you specifically want to use the binaries of modules listed in the project's package.json.
– regular
Oct 3 '13 at 9:12
9
oh gosh! do I really have to do something like that in order to have my local modules working? it's quite impracticable to explain it to a team! there's nothing a little bit more straightforward?
– Alexian
Apr 24 '15 at 17:12
14
You can always use npm scripts since they always search the local binaries first. You can set up aliases to each of your binaries there or just use generic names like "build".
– Joe Zim
Sep 22 '15 at 1:47
6
@philosodad, actually no, you don't. ThePATH
will be back to what it was before the command invocation. Setting an environment variable in the same line, before running a command only affects that command's environment.
– regular
Sep 22 '15 at 6:28
|
show 11 more comments
17
you can even go one step further andalias coffee="npm-exec coffee"
– regular
Mar 18 '13 at 6:12
6
The output changes when you cd into another project. It does not change when you cd within a project.npm bin
searches the chain of 'ancestor directories' to the cwd for a node_modules directory. This is exactly the desired behavior if you specifically want to use the binaries of modules listed in the project's package.json.
– regular
Oct 3 '13 at 9:12
9
oh gosh! do I really have to do something like that in order to have my local modules working? it's quite impracticable to explain it to a team! there's nothing a little bit more straightforward?
– Alexian
Apr 24 '15 at 17:12
14
You can always use npm scripts since they always search the local binaries first. You can set up aliases to each of your binaries there or just use generic names like "build".
– Joe Zim
Sep 22 '15 at 1:47
6
@philosodad, actually no, you don't. ThePATH
will be back to what it was before the command invocation. Setting an environment variable in the same line, before running a command only affects that command's environment.
– regular
Sep 22 '15 at 6:28
17
17
you can even go one step further and
alias coffee="npm-exec coffee"
– regular
Mar 18 '13 at 6:12
you can even go one step further and
alias coffee="npm-exec coffee"
– regular
Mar 18 '13 at 6:12
6
6
The output changes when you cd into another project. It does not change when you cd within a project.
npm bin
searches the chain of 'ancestor directories' to the cwd for a node_modules directory. This is exactly the desired behavior if you specifically want to use the binaries of modules listed in the project's package.json.– regular
Oct 3 '13 at 9:12
The output changes when you cd into another project. It does not change when you cd within a project.
npm bin
searches the chain of 'ancestor directories' to the cwd for a node_modules directory. This is exactly the desired behavior if you specifically want to use the binaries of modules listed in the project's package.json.– regular
Oct 3 '13 at 9:12
9
9
oh gosh! do I really have to do something like that in order to have my local modules working? it's quite impracticable to explain it to a team! there's nothing a little bit more straightforward?
– Alexian
Apr 24 '15 at 17:12
oh gosh! do I really have to do something like that in order to have my local modules working? it's quite impracticable to explain it to a team! there's nothing a little bit more straightforward?
– Alexian
Apr 24 '15 at 17:12
14
14
You can always use npm scripts since they always search the local binaries first. You can set up aliases to each of your binaries there or just use generic names like "build".
– Joe Zim
Sep 22 '15 at 1:47
You can always use npm scripts since they always search the local binaries first. You can set up aliases to each of your binaries there or just use generic names like "build".
– Joe Zim
Sep 22 '15 at 1:47
6
6
@philosodad, actually no, you don't. The
PATH
will be back to what it was before the command invocation. Setting an environment variable in the same line, before running a command only affects that command's environment.– regular
Sep 22 '15 at 6:28
@philosodad, actually no, you don't. The
PATH
will be back to what it was before the command invocation. Setting an environment variable in the same line, before running a command only affects that command's environment.– regular
Sep 22 '15 at 6:28
|
show 11 more comments
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
27
Definitely should be the modern accepted answer!
– Mike Rapadas
Sep 27 '17 at 8:22
10
I think this needs to be higher up on the thread.
– wpcarro
Jan 5 at 19:51
I dont like installing 3rd party global npm packages whilenpm
andpackage.json
provides nearly same functionality.
– guneysus
Mar 28 at 6:27
3
This saved me a ton of headache! How is this not the accepted answer? :-)
– JackWilson
Jun 1 at 9:13
If "Path must be a string. Received undefined" message appears, here is a fix: github.com/zkat/npx/issues/144#issuecomment-391031816
– Valeriy Katkov
Aug 2 at 15:56
|
show 1 more comment
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
27
Definitely should be the modern accepted answer!
– Mike Rapadas
Sep 27 '17 at 8:22
10
I think this needs to be higher up on the thread.
– wpcarro
Jan 5 at 19:51
I dont like installing 3rd party global npm packages whilenpm
andpackage.json
provides nearly same functionality.
– guneysus
Mar 28 at 6:27
3
This saved me a ton of headache! How is this not the accepted answer? :-)
– JackWilson
Jun 1 at 9:13
If "Path must be a string. Received undefined" message appears, here is a fix: github.com/zkat/npx/issues/144#issuecomment-391031816
– Valeriy Katkov
Aug 2 at 15:56
|
show 1 more comment
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
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
edited Mar 1 at 3:28
answered Jul 18 '17 at 10:59
Seyeong Jeong
6,40922036
6,40922036
27
Definitely should be the modern accepted answer!
– Mike Rapadas
Sep 27 '17 at 8:22
10
I think this needs to be higher up on the thread.
– wpcarro
Jan 5 at 19:51
I dont like installing 3rd party global npm packages whilenpm
andpackage.json
provides nearly same functionality.
– guneysus
Mar 28 at 6:27
3
This saved me a ton of headache! How is this not the accepted answer? :-)
– JackWilson
Jun 1 at 9:13
If "Path must be a string. Received undefined" message appears, here is a fix: github.com/zkat/npx/issues/144#issuecomment-391031816
– Valeriy Katkov
Aug 2 at 15:56
|
show 1 more comment
27
Definitely should be the modern accepted answer!
– Mike Rapadas
Sep 27 '17 at 8:22
10
I think this needs to be higher up on the thread.
– wpcarro
Jan 5 at 19:51
I dont like installing 3rd party global npm packages whilenpm
andpackage.json
provides nearly same functionality.
– guneysus
Mar 28 at 6:27
3
This saved me a ton of headache! How is this not the accepted answer? :-)
– JackWilson
Jun 1 at 9:13
If "Path must be a string. Received undefined" message appears, here is a fix: github.com/zkat/npx/issues/144#issuecomment-391031816
– Valeriy Katkov
Aug 2 at 15:56
27
27
Definitely should be the modern accepted answer!
– Mike Rapadas
Sep 27 '17 at 8:22
Definitely should be the modern accepted answer!
– Mike Rapadas
Sep 27 '17 at 8:22
10
10
I think this needs to be higher up on the thread.
– wpcarro
Jan 5 at 19:51
I think this needs to be higher up on the thread.
– wpcarro
Jan 5 at 19:51
I dont like installing 3rd party global npm packages while
npm
and package.json
provides nearly same functionality.– guneysus
Mar 28 at 6:27
I dont like installing 3rd party global npm packages while
npm
and package.json
provides nearly same functionality.– guneysus
Mar 28 at 6:27
3
3
This saved me a ton of headache! How is this not the accepted answer? :-)
– JackWilson
Jun 1 at 9:13
This saved me a ton of headache! How is this not the accepted answer? :-)
– JackWilson
Jun 1 at 9:13
If "Path must be a string. Received undefined" message appears, here is a fix: github.com/zkat/npx/issues/144#issuecomment-391031816
– Valeriy Katkov
Aug 2 at 15:56
If "Path must be a string. Received undefined" message appears, here is a fix: github.com/zkat/npx/issues/144#issuecomment-391031816
– Valeriy Katkov
Aug 2 at 15:56
|
show 1 more comment
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
4
I like this simple and generic solution. Makes an alias seem unnecessary.
– Matt Montag
May 31 '15 at 22:13
Seems to be the next best solution that is elegant and more secure than having to doexport PATH="./node_modules/.bin:$PATH"
– jontsai
Sep 9 '15 at 23:16
1
@inf3rno the command is$(npm bin)/jasmine
, notnode $(npm bin)/jasmine
(you probably figured it out but clarifying for others).
– jassa
Aug 19 '16 at 19:32
5
Not a bad solution, but it doesn't run on a standard windows command line with $. Putting it in the package.json scripts section is a better approach I feel, since it's more compatible.
– Timothy Gonzalez
Sep 30 '16 at 22:26
add a comment |
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
4
I like this simple and generic solution. Makes an alias seem unnecessary.
– Matt Montag
May 31 '15 at 22:13
Seems to be the next best solution that is elegant and more secure than having to doexport PATH="./node_modules/.bin:$PATH"
– jontsai
Sep 9 '15 at 23:16
1
@inf3rno the command is$(npm bin)/jasmine
, notnode $(npm bin)/jasmine
(you probably figured it out but clarifying for others).
– jassa
Aug 19 '16 at 19:32
5
Not a bad solution, but it doesn't run on a standard windows command line with $. Putting it in the package.json scripts section is a better approach I feel, since it's more compatible.
– Timothy Gonzalez
Sep 30 '16 at 22:26
add a comment |
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 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
answered Dec 13 '14 at 1:18
jassa
16.1k32123
16.1k32123
4
I like this simple and generic solution. Makes an alias seem unnecessary.
– Matt Montag
May 31 '15 at 22:13
Seems to be the next best solution that is elegant and more secure than having to doexport PATH="./node_modules/.bin:$PATH"
– jontsai
Sep 9 '15 at 23:16
1
@inf3rno the command is$(npm bin)/jasmine
, notnode $(npm bin)/jasmine
(you probably figured it out but clarifying for others).
– jassa
Aug 19 '16 at 19:32
5
Not a bad solution, but it doesn't run on a standard windows command line with $. Putting it in the package.json scripts section is a better approach I feel, since it's more compatible.
– Timothy Gonzalez
Sep 30 '16 at 22:26
add a comment |
4
I like this simple and generic solution. Makes an alias seem unnecessary.
– Matt Montag
May 31 '15 at 22:13
Seems to be the next best solution that is elegant and more secure than having to doexport PATH="./node_modules/.bin:$PATH"
– jontsai
Sep 9 '15 at 23:16
1
@inf3rno the command is$(npm bin)/jasmine
, notnode $(npm bin)/jasmine
(you probably figured it out but clarifying for others).
– jassa
Aug 19 '16 at 19:32
5
Not a bad solution, but it doesn't run on a standard windows command line with $. Putting it in the package.json scripts section is a better approach I feel, since it's more compatible.
– Timothy Gonzalez
Sep 30 '16 at 22:26
4
4
I like this simple and generic solution. Makes an alias seem unnecessary.
– Matt Montag
May 31 '15 at 22:13
I like this simple and generic solution. Makes an alias seem unnecessary.
– Matt Montag
May 31 '15 at 22:13
Seems to be the next best solution that is elegant and more secure than having to do
export PATH="./node_modules/.bin:$PATH"
– jontsai
Sep 9 '15 at 23:16
Seems to be the next best solution that is elegant and more secure than having to do
export PATH="./node_modules/.bin:$PATH"
– jontsai
Sep 9 '15 at 23:16
1
1
@inf3rno the command is
$(npm bin)/jasmine
, not node $(npm bin)/jasmine
(you probably figured it out but clarifying for others).– jassa
Aug 19 '16 at 19:32
@inf3rno the command is
$(npm bin)/jasmine
, not node $(npm bin)/jasmine
(you probably figured it out but clarifying for others).– jassa
Aug 19 '16 at 19:32
5
5
Not a bad solution, but it doesn't run on a standard windows command line with $. Putting it in the package.json scripts section is a better approach I feel, since it's more compatible.
– Timothy Gonzalez
Sep 30 '16 at 22:26
Not a bad solution, but it doesn't run on a standard windows command line with $. Putting it in the package.json scripts section is a better approach I feel, since it's more compatible.
– Timothy Gonzalez
Sep 30 '16 at 22:26
add a comment |
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.
5
I found this approach to be more uniform when dealing with multiple developers on a project - it avoids the need to configure anything locally... you justnpm install
then you have access to your dev dependencies. The only minor down side is you need tonpm run eslint
(or whatever). You can create a script called "start" that runs gulp so that you only need to typenpm start
to start your dev server. Pretty cool stuff and no bash goodness, so your windows friends still like you. :)
– jpoveda
Sep 21 '15 at 21:28
1
adding an alias to put $(npm bin) on your path is clever, but the fact that this will work for people without local config wins my heart
– Conrad.Dean
Mar 25 '16 at 12:37
8
this needs more upvotes! Pass args to your scripts after--
like:npm run learnyounode -- --normal-switches --watch -d *.js
– ptim
Jun 17 '16 at 8:53
I also find this the best solution. There is an in-depth explanation here: lostechies.com/derickbailey/2012/04/24/…
– adampasz
Jul 24 '16 at 4:41
Wow, I can't believe I looked passed the scripts section. This is truly a good solution.
– Timothy Gonzalez
Sep 30 '16 at 22:23
add a comment |
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.
5
I found this approach to be more uniform when dealing with multiple developers on a project - it avoids the need to configure anything locally... you justnpm install
then you have access to your dev dependencies. The only minor down side is you need tonpm run eslint
(or whatever). You can create a script called "start" that runs gulp so that you only need to typenpm start
to start your dev server. Pretty cool stuff and no bash goodness, so your windows friends still like you. :)
– jpoveda
Sep 21 '15 at 21:28
1
adding an alias to put $(npm bin) on your path is clever, but the fact that this will work for people without local config wins my heart
– Conrad.Dean
Mar 25 '16 at 12:37
8
this needs more upvotes! Pass args to your scripts after--
like:npm run learnyounode -- --normal-switches --watch -d *.js
– ptim
Jun 17 '16 at 8:53
I also find this the best solution. There is an in-depth explanation here: lostechies.com/derickbailey/2012/04/24/…
– adampasz
Jul 24 '16 at 4:41
Wow, I can't believe I looked passed the scripts section. This is truly a good solution.
– Timothy Gonzalez
Sep 30 '16 at 22:23
add a comment |
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.
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.
edited Aug 25 at 15:33
vsync
45.4k35157217
45.4k35157217
answered Feb 16 '15 at 20:10
jla
3,24512328
3,24512328
5
I found this approach to be more uniform when dealing with multiple developers on a project - it avoids the need to configure anything locally... you justnpm install
then you have access to your dev dependencies. The only minor down side is you need tonpm run eslint
(or whatever). You can create a script called "start" that runs gulp so that you only need to typenpm start
to start your dev server. Pretty cool stuff and no bash goodness, so your windows friends still like you. :)
– jpoveda
Sep 21 '15 at 21:28
1
adding an alias to put $(npm bin) on your path is clever, but the fact that this will work for people without local config wins my heart
– Conrad.Dean
Mar 25 '16 at 12:37
8
this needs more upvotes! Pass args to your scripts after--
like:npm run learnyounode -- --normal-switches --watch -d *.js
– ptim
Jun 17 '16 at 8:53
I also find this the best solution. There is an in-depth explanation here: lostechies.com/derickbailey/2012/04/24/…
– adampasz
Jul 24 '16 at 4:41
Wow, I can't believe I looked passed the scripts section. This is truly a good solution.
– Timothy Gonzalez
Sep 30 '16 at 22:23
add a comment |
5
I found this approach to be more uniform when dealing with multiple developers on a project - it avoids the need to configure anything locally... you justnpm install
then you have access to your dev dependencies. The only minor down side is you need tonpm run eslint
(or whatever). You can create a script called "start" that runs gulp so that you only need to typenpm start
to start your dev server. Pretty cool stuff and no bash goodness, so your windows friends still like you. :)
– jpoveda
Sep 21 '15 at 21:28
1
adding an alias to put $(npm bin) on your path is clever, but the fact that this will work for people without local config wins my heart
– Conrad.Dean
Mar 25 '16 at 12:37
8
this needs more upvotes! Pass args to your scripts after--
like:npm run learnyounode -- --normal-switches --watch -d *.js
– ptim
Jun 17 '16 at 8:53
I also find this the best solution. There is an in-depth explanation here: lostechies.com/derickbailey/2012/04/24/…
– adampasz
Jul 24 '16 at 4:41
Wow, I can't believe I looked passed the scripts section. This is truly a good solution.
– Timothy Gonzalez
Sep 30 '16 at 22:23
5
5
I found this approach to be more uniform when dealing with multiple developers on a project - it avoids the need to configure anything locally... you just
npm install
then you have access to your dev dependencies. The only minor down side is you need to npm run eslint
(or whatever). You can create a script called "start" that runs gulp so that you only need to type npm start
to start your dev server. Pretty cool stuff and no bash goodness, so your windows friends still like you. :)– jpoveda
Sep 21 '15 at 21:28
I found this approach to be more uniform when dealing with multiple developers on a project - it avoids the need to configure anything locally... you just
npm install
then you have access to your dev dependencies. The only minor down side is you need to npm run eslint
(or whatever). You can create a script called "start" that runs gulp so that you only need to type npm start
to start your dev server. Pretty cool stuff and no bash goodness, so your windows friends still like you. :)– jpoveda
Sep 21 '15 at 21:28
1
1
adding an alias to put $(npm bin) on your path is clever, but the fact that this will work for people without local config wins my heart
– Conrad.Dean
Mar 25 '16 at 12:37
adding an alias to put $(npm bin) on your path is clever, but the fact that this will work for people without local config wins my heart
– Conrad.Dean
Mar 25 '16 at 12:37
8
8
this needs more upvotes! Pass args to your scripts after
--
like: npm run learnyounode -- --normal-switches --watch -d *.js
– ptim
Jun 17 '16 at 8:53
this needs more upvotes! Pass args to your scripts after
--
like: npm run learnyounode -- --normal-switches --watch -d *.js
– ptim
Jun 17 '16 at 8:53
I also find this the best solution. There is an in-depth explanation here: lostechies.com/derickbailey/2012/04/24/…
– adampasz
Jul 24 '16 at 4:41
I also find this the best solution. There is an in-depth explanation here: lostechies.com/derickbailey/2012/04/24/…
– adampasz
Jul 24 '16 at 4:41
Wow, I can't believe I looked passed the scripts section. This is truly a good solution.
– Timothy Gonzalez
Sep 30 '16 at 22:23
Wow, I can't believe I looked passed the scripts section. This is truly a good solution.
– Timothy Gonzalez
Sep 30 '16 at 22:23
add a comment |
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
6
No longer, see npx referenced above... stackoverflow.com/a/45164863/3246805
– t.j.
Sep 1 '17 at 19:00
add a comment |
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
6
No longer, see npx referenced above... stackoverflow.com/a/45164863/3246805
– t.j.
Sep 1 '17 at 19:00
add a comment |
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
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
answered Oct 28 '16 at 21:20
mightyiam
1,8111633
1,8111633
6
No longer, see npx referenced above... stackoverflow.com/a/45164863/3246805
– t.j.
Sep 1 '17 at 19:00
add a comment |
6
No longer, see npx referenced above... stackoverflow.com/a/45164863/3246805
– t.j.
Sep 1 '17 at 19:00
6
6
No longer, see npx referenced above... stackoverflow.com/a/45164863/3246805
– t.j.
Sep 1 '17 at 19:00
No longer, see npx referenced above... stackoverflow.com/a/45164863/3246805
– t.j.
Sep 1 '17 at 19:00
add a comment |
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).
1
that's probably the best solution. I also created a bash script called "watch" in my project:./node_modules/.bin/coffee --output lib/ --compile --bare --watch src
– typeoneerror
Mar 13 '12 at 18:27
66
Danger, Will Robinson! Use of relative paths in your $PATH opens up a security hole the size of a planet, especially if you put them right up front as the first item. If the directory you're in is writable by everyone (say somewhere in/tmp
), any process or user can hijack your session by putting malicious versions of ordinary commands (likels
,cp
, etc.) there. These may spawn 'invisible' sub-shells capturing your passwords, etcetera.
– ack
Feb 28 '14 at 20:40
will only work in the root and no other places. thealias npm-exec='PATH=$(npm bin):$PATH'
is slicker.
– oligofren
May 5 '15 at 16:21
1
How bad is this if you don't put it as the first thing in yourPATH
, but the last (using the$(npm bin)
form)? so they can't overwrite your existing stuff, and you would have been trusting the executables in thenpm bin
directory already regardless of thePATH
var; would the threat model be that a) someone malicious gets access to your file system, b) they add executables with names that are close to those system tools, and c) you mistype? Trying to understand scenarios that make this bad, given that you're already trusting foreign executables when usingnpm
-installed programs.
– Omar Diab
Jul 15 '16 at 1:24
You can do shell tricks with an alias and you can path manually and this "works" but it is not quite ideal.
– al_gor_rithm
Dec 18 '17 at 19:42
add a comment |
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).
1
that's probably the best solution. I also created a bash script called "watch" in my project:./node_modules/.bin/coffee --output lib/ --compile --bare --watch src
– typeoneerror
Mar 13 '12 at 18:27
66
Danger, Will Robinson! Use of relative paths in your $PATH opens up a security hole the size of a planet, especially if you put them right up front as the first item. If the directory you're in is writable by everyone (say somewhere in/tmp
), any process or user can hijack your session by putting malicious versions of ordinary commands (likels
,cp
, etc.) there. These may spawn 'invisible' sub-shells capturing your passwords, etcetera.
– ack
Feb 28 '14 at 20:40
will only work in the root and no other places. thealias npm-exec='PATH=$(npm bin):$PATH'
is slicker.
– oligofren
May 5 '15 at 16:21
1
How bad is this if you don't put it as the first thing in yourPATH
, but the last (using the$(npm bin)
form)? so they can't overwrite your existing stuff, and you would have been trusting the executables in thenpm bin
directory already regardless of thePATH
var; would the threat model be that a) someone malicious gets access to your file system, b) they add executables with names that are close to those system tools, and c) you mistype? Trying to understand scenarios that make this bad, given that you're already trusting foreign executables when usingnpm
-installed programs.
– Omar Diab
Jul 15 '16 at 1:24
You can do shell tricks with an alias and you can path manually and this "works" but it is not quite ideal.
– al_gor_rithm
Dec 18 '17 at 19:42
add a comment |
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).
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).
edited Oct 6 '15 at 14:02
answered Mar 13 '12 at 11:52
Linus Gustav Larsson Thiel
32.8k68993
32.8k68993
1
that's probably the best solution. I also created a bash script called "watch" in my project:./node_modules/.bin/coffee --output lib/ --compile --bare --watch src
– typeoneerror
Mar 13 '12 at 18:27
66
Danger, Will Robinson! Use of relative paths in your $PATH opens up a security hole the size of a planet, especially if you put them right up front as the first item. If the directory you're in is writable by everyone (say somewhere in/tmp
), any process or user can hijack your session by putting malicious versions of ordinary commands (likels
,cp
, etc.) there. These may spawn 'invisible' sub-shells capturing your passwords, etcetera.
– ack
Feb 28 '14 at 20:40
will only work in the root and no other places. thealias npm-exec='PATH=$(npm bin):$PATH'
is slicker.
– oligofren
May 5 '15 at 16:21
1
How bad is this if you don't put it as the first thing in yourPATH
, but the last (using the$(npm bin)
form)? so they can't overwrite your existing stuff, and you would have been trusting the executables in thenpm bin
directory already regardless of thePATH
var; would the threat model be that a) someone malicious gets access to your file system, b) they add executables with names that are close to those system tools, and c) you mistype? Trying to understand scenarios that make this bad, given that you're already trusting foreign executables when usingnpm
-installed programs.
– Omar Diab
Jul 15 '16 at 1:24
You can do shell tricks with an alias and you can path manually and this "works" but it is not quite ideal.
– al_gor_rithm
Dec 18 '17 at 19:42
add a comment |
1
that's probably the best solution. I also created a bash script called "watch" in my project:./node_modules/.bin/coffee --output lib/ --compile --bare --watch src
– typeoneerror
Mar 13 '12 at 18:27
66
Danger, Will Robinson! Use of relative paths in your $PATH opens up a security hole the size of a planet, especially if you put them right up front as the first item. If the directory you're in is writable by everyone (say somewhere in/tmp
), any process or user can hijack your session by putting malicious versions of ordinary commands (likels
,cp
, etc.) there. These may spawn 'invisible' sub-shells capturing your passwords, etcetera.
– ack
Feb 28 '14 at 20:40
will only work in the root and no other places. thealias npm-exec='PATH=$(npm bin):$PATH'
is slicker.
– oligofren
May 5 '15 at 16:21
1
How bad is this if you don't put it as the first thing in yourPATH
, but the last (using the$(npm bin)
form)? so they can't overwrite your existing stuff, and you would have been trusting the executables in thenpm bin
directory already regardless of thePATH
var; would the threat model be that a) someone malicious gets access to your file system, b) they add executables with names that are close to those system tools, and c) you mistype? Trying to understand scenarios that make this bad, given that you're already trusting foreign executables when usingnpm
-installed programs.
– Omar Diab
Jul 15 '16 at 1:24
You can do shell tricks with an alias and you can path manually and this "works" but it is not quite ideal.
– al_gor_rithm
Dec 18 '17 at 19:42
1
1
that's probably the best solution. I also created a bash script called "watch" in my project:
./node_modules/.bin/coffee --output lib/ --compile --bare --watch src
– typeoneerror
Mar 13 '12 at 18:27
that's probably the best solution. I also created a bash script called "watch" in my project:
./node_modules/.bin/coffee --output lib/ --compile --bare --watch src
– typeoneerror
Mar 13 '12 at 18:27
66
66
Danger, Will Robinson! Use of relative paths in your $PATH opens up a security hole the size of a planet, especially if you put them right up front as the first item. If the directory you're in is writable by everyone (say somewhere in
/tmp
), any process or user can hijack your session by putting malicious versions of ordinary commands (like ls
, cp
, etc.) there. These may spawn 'invisible' sub-shells capturing your passwords, etcetera.– ack
Feb 28 '14 at 20:40
Danger, Will Robinson! Use of relative paths in your $PATH opens up a security hole the size of a planet, especially if you put them right up front as the first item. If the directory you're in is writable by everyone (say somewhere in
/tmp
), any process or user can hijack your session by putting malicious versions of ordinary commands (like ls
, cp
, etc.) there. These may spawn 'invisible' sub-shells capturing your passwords, etcetera.– ack
Feb 28 '14 at 20:40
will only work in the root and no other places. the
alias npm-exec='PATH=$(npm bin):$PATH'
is slicker.– oligofren
May 5 '15 at 16:21
will only work in the root and no other places. the
alias npm-exec='PATH=$(npm bin):$PATH'
is slicker.– oligofren
May 5 '15 at 16:21
1
1
How bad is this if you don't put it as the first thing in your
PATH
, but the last (using the $(npm bin)
form)? so they can't overwrite your existing stuff, and you would have been trusting the executables in the npm bin
directory already regardless of the PATH
var; would the threat model be that a) someone malicious gets access to your file system, b) they add executables with names that are close to those system tools, and c) you mistype? Trying to understand scenarios that make this bad, given that you're already trusting foreign executables when using npm
-installed programs.– Omar Diab
Jul 15 '16 at 1:24
How bad is this if you don't put it as the first thing in your
PATH
, but the last (using the $(npm bin)
form)? so they can't overwrite your existing stuff, and you would have been trusting the executables in the npm bin
directory already regardless of the PATH
var; would the threat model be that a) someone malicious gets access to your file system, b) they add executables with names that are close to those system tools, and c) you mistype? Trying to understand scenarios that make this bad, given that you're already trusting foreign executables when using npm
-installed programs.– Omar Diab
Jul 15 '16 at 1:24
You can do shell tricks with an alias and you can path manually and this "works" but it is not quite ideal.
– al_gor_rithm
Dec 18 '17 at 19:42
You can do shell tricks with an alias and you can path manually and this "works" but it is not quite ideal.
– al_gor_rithm
Dec 18 '17 at 19:42
add a comment |
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>
2
I like this! I simply named my functionn
– jontsai
Sep 9 '15 at 23:20
This is great! Thanks for sharing. I added a fish shell version below.
– Leon li
May 19 '17 at 16:49
add a comment |
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>
2
I like this! I simply named my functionn
– jontsai
Sep 9 '15 at 23:20
This is great! Thanks for sharing. I added a fish shell version below.
– Leon li
May 19 '17 at 16:49
add a comment |
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>
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>
answered Aug 17 '15 at 21:09
Bob9630
715617
715617
2
I like this! I simply named my functionn
– jontsai
Sep 9 '15 at 23:20
This is great! Thanks for sharing. I added a fish shell version below.
– Leon li
May 19 '17 at 16:49
add a comment |
2
I like this! I simply named my functionn
– jontsai
Sep 9 '15 at 23:20
This is great! Thanks for sharing. I added a fish shell version below.
– Leon li
May 19 '17 at 16:49
2
2
I like this! I simply named my function
n
– jontsai
Sep 9 '15 at 23:20
I like this! I simply named my function
n
– jontsai
Sep 9 '15 at 23:20
This is great! Thanks for sharing. I added a fish shell version below.
– Leon li
May 19 '17 at 16:49
This is great! Thanks for sharing. I added a fish shell version below.
– Leon li
May 19 '17 at 16:49
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Feb 5 at 14:54
answered Aug 11 '17 at 10:54
k0pernikus
17.2k23104186
17.2k23104186
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 '13 at 6:14
namuol
7,35943151
7,35943151
add a comment |
add a comment |
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"
}
}
add a comment |
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"
}
}
add a comment |
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"
}
}
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"
}
}
answered Mar 11 at 16:21
guneysus
3,92812929
3,92812929
add a comment |
add a comment |
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
https://medium.com/ninjadevs/with-npx-you-can-forget-about-installing-unnecessary-npm-packages-globally-and-do-even-more-a28b797ea449
add a comment |
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
https://medium.com/ninjadevs/with-npx-you-can-forget-about-installing-unnecessary-npm-packages-globally-and-do-even-more-a28b797ea449
add a comment |
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
https://medium.com/ninjadevs/with-npx-you-can-forget-about-installing-unnecessary-npm-packages-globally-and-do-even-more-a28b797ea449
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
https://medium.com/ninjadevs/with-npx-you-can-forget-about-installing-unnecessary-npm-packages-globally-and-do-even-more-a28b797ea449
edited Dec 19 at 3:10
answered Aug 30 at 22:31
Jair Reina
1,2151210
1,2151210
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 18 '16 at 22:09
Nathan
4,05921922
4,05921922
add a comment |
add a comment |
Same @regular 's accepted solution, but Fish shell flavour
if not contains (npm bin) $PATH
set PATH (npm bin) $PATH
end
add a comment |
Same @regular 's accepted solution, but Fish shell flavour
if not contains (npm bin) $PATH
set PATH (npm bin) $PATH
end
add a comment |
Same @regular 's accepted solution, but Fish shell flavour
if not contains (npm bin) $PATH
set PATH (npm bin) $PATH
end
Same @regular 's accepted solution, but Fish shell flavour
if not contains (npm bin) $PATH
set PATH (npm bin) $PATH
end
edited Feb 7 '17 at 11:58
answered Sep 8 '15 at 14:42
Pioneer Skies
1,2501911
1,2501911
add a comment |
add a comment |
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.binwdio wdio.conf.js
This doesn't work while passing more than 1 argument. E.g. npm-exec gulp <some_task>
– OK999
Oct 4 '16 at 15:55
@OK9999 I'm sure some minor modification will allow passing arguments (because when you pass it here, it's coming in quoted in ""); What I suggest is copy paste the gulp file from bin to your project root (some modifications needed of the file, but it will just work without writing new code etc)
– Dheeraj Bhaskar
Oct 11 '16 at 18:45
Yes, i ended up doing so. The node_modules folder have to be in the folder where the gulpfile exist
– OK999
Oct 11 '16 at 18:53
add a comment |
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.binwdio wdio.conf.js
This doesn't work while passing more than 1 argument. E.g. npm-exec gulp <some_task>
– OK999
Oct 4 '16 at 15:55
@OK9999 I'm sure some minor modification will allow passing arguments (because when you pass it here, it's coming in quoted in ""); What I suggest is copy paste the gulp file from bin to your project root (some modifications needed of the file, but it will just work without writing new code etc)
– Dheeraj Bhaskar
Oct 11 '16 at 18:45
Yes, i ended up doing so. The node_modules folder have to be in the folder where the gulpfile exist
– OK999
Oct 11 '16 at 18:53
add a comment |
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.binwdio wdio.conf.js
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.binwdio wdio.conf.js
edited Nov 15 '17 at 11:38
LoganMzz
1,22021226
1,22021226
answered Aug 3 '16 at 23:00
Dheeraj Bhaskar
14.2k74352
14.2k74352
This doesn't work while passing more than 1 argument. E.g. npm-exec gulp <some_task>
– OK999
Oct 4 '16 at 15:55
@OK9999 I'm sure some minor modification will allow passing arguments (because when you pass it here, it's coming in quoted in ""); What I suggest is copy paste the gulp file from bin to your project root (some modifications needed of the file, but it will just work without writing new code etc)
– Dheeraj Bhaskar
Oct 11 '16 at 18:45
Yes, i ended up doing so. The node_modules folder have to be in the folder where the gulpfile exist
– OK999
Oct 11 '16 at 18:53
add a comment |
This doesn't work while passing more than 1 argument. E.g. npm-exec gulp <some_task>
– OK999
Oct 4 '16 at 15:55
@OK9999 I'm sure some minor modification will allow passing arguments (because when you pass it here, it's coming in quoted in ""); What I suggest is copy paste the gulp file from bin to your project root (some modifications needed of the file, but it will just work without writing new code etc)
– Dheeraj Bhaskar
Oct 11 '16 at 18:45
Yes, i ended up doing so. The node_modules folder have to be in the folder where the gulpfile exist
– OK999
Oct 11 '16 at 18:53
This doesn't work while passing more than 1 argument. E.g. npm-exec gulp <some_task>
– OK999
Oct 4 '16 at 15:55
This doesn't work while passing more than 1 argument. E.g. npm-exec gulp <some_task>
– OK999
Oct 4 '16 at 15:55
@OK9999 I'm sure some minor modification will allow passing arguments (because when you pass it here, it's coming in quoted in ""); What I suggest is copy paste the gulp file from bin to your project root (some modifications needed of the file, but it will just work without writing new code etc)
– Dheeraj Bhaskar
Oct 11 '16 at 18:45
@OK9999 I'm sure some minor modification will allow passing arguments (because when you pass it here, it's coming in quoted in ""); What I suggest is copy paste the gulp file from bin to your project root (some modifications needed of the file, but it will just work without writing new code etc)
– Dheeraj Bhaskar
Oct 11 '16 at 18:45
Yes, i ended up doing so. The node_modules folder have to be in the folder where the gulpfile exist
– OK999
Oct 11 '16 at 18:53
Yes, i ended up doing so. The node_modules folder have to be in the folder where the gulpfile exist
– OK999
Oct 11 '16 at 18:53
add a comment |
You can also use direnv and change the $PATH variable only in your working folder.
$ cat .envrc
> export PATH=$(npm bin):$PATH
use with direnv. direnv.net
– kuboon
Jan 12 '17 at 9:31
add a comment |
You can also use direnv and change the $PATH variable only in your working folder.
$ cat .envrc
> export PATH=$(npm bin):$PATH
use with direnv. direnv.net
– kuboon
Jan 12 '17 at 9:31
add a comment |
You can also use direnv and change the $PATH variable only in your working folder.
$ cat .envrc
> export PATH=$(npm bin):$PATH
You can also use direnv and change the $PATH variable only in your working folder.
$ cat .envrc
> export PATH=$(npm bin):$PATH
answered Nov 16 '16 at 0:22
Erem
67821017
67821017
use with direnv. direnv.net
– kuboon
Jan 12 '17 at 9:31
add a comment |
use with direnv. direnv.net
– kuboon
Jan 12 '17 at 9:31
use with direnv. direnv.net
– kuboon
Jan 12 '17 at 9:31
use with direnv. direnv.net
– kuboon
Jan 12 '17 at 9:31
add a comment |
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.
1
Use$(npm bin)
instead of hardcoding./node_modules/.bin
.
– bfontaine
Jan 25 '17 at 17:27
That makes sense. I've updated the code above.
– Tsutomu Kawamura
Jan 26 '17 at 2:12
Hmm,$(npm bin)
seems too slow to use with eachcd
command. I've restore the code and added a note for it.
– Tsutomu Kawamura
Jan 26 '17 at 2:28
add a comment |
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.
1
Use$(npm bin)
instead of hardcoding./node_modules/.bin
.
– bfontaine
Jan 25 '17 at 17:27
That makes sense. I've updated the code above.
– Tsutomu Kawamura
Jan 26 '17 at 2:12
Hmm,$(npm bin)
seems too slow to use with eachcd
command. I've restore the code and added a note for it.
– Tsutomu Kawamura
Jan 26 '17 at 2:28
add a comment |
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.
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.
edited Jan 26 '17 at 2:25
answered Dec 19 '16 at 16:49
Tsutomu Kawamura
1,068810
1,068810
1
Use$(npm bin)
instead of hardcoding./node_modules/.bin
.
– bfontaine
Jan 25 '17 at 17:27
That makes sense. I've updated the code above.
– Tsutomu Kawamura
Jan 26 '17 at 2:12
Hmm,$(npm bin)
seems too slow to use with eachcd
command. I've restore the code and added a note for it.
– Tsutomu Kawamura
Jan 26 '17 at 2:28
add a comment |
1
Use$(npm bin)
instead of hardcoding./node_modules/.bin
.
– bfontaine
Jan 25 '17 at 17:27
That makes sense. I've updated the code above.
– Tsutomu Kawamura
Jan 26 '17 at 2:12
Hmm,$(npm bin)
seems too slow to use with eachcd
command. I've restore the code and added a note for it.
– Tsutomu Kawamura
Jan 26 '17 at 2:28
1
1
Use
$(npm bin)
instead of hardcoding ./node_modules/.bin
.– bfontaine
Jan 25 '17 at 17:27
Use
$(npm bin)
instead of hardcoding ./node_modules/.bin
.– bfontaine
Jan 25 '17 at 17:27
That makes sense. I've updated the code above.
– Tsutomu Kawamura
Jan 26 '17 at 2:12
That makes sense. I've updated the code above.
– Tsutomu Kawamura
Jan 26 '17 at 2:12
Hmm,
$(npm bin)
seems too slow to use with each cd
command. I've restore the code and added a note for it.– Tsutomu Kawamura
Jan 26 '17 at 2:28
Hmm,
$(npm bin)
seems too slow to use with each cd
command. I've restore the code and added a note for it.– Tsutomu Kawamura
Jan 26 '17 at 2:28
add a comment |
For Windows use this:
/* cmd into "node_modules" folder */
"%CD%.bingrunt" --version
add a comment |
For Windows use this:
/* cmd into "node_modules" folder */
"%CD%.bingrunt" --version
add a comment |
For Windows use this:
/* cmd into "node_modules" folder */
"%CD%.bingrunt" --version
For Windows use this:
/* cmd into "node_modules" folder */
"%CD%.bingrunt" --version
edited Feb 22 '17 at 16:35
answered Feb 2 '17 at 12:59
b3wii
454313
454313
add a comment |
add a comment |
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>'
}
2
this setenv script isn't necessary for package.json scripts. npm already prepends the local node_modules/.bin directory to path for you when executing npm run {scripts}.
– jasonkarns
Feb 6 '15 at 19:39
add a comment |
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>'
}
2
this setenv script isn't necessary for package.json scripts. npm already prepends the local node_modules/.bin directory to path for you when executing npm run {scripts}.
– jasonkarns
Feb 6 '15 at 19:39
add a comment |
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 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>'
}
edited May 23 '17 at 11:55
Community♦
11
11
answered Dec 30 '14 at 22:20
nkh
4,0761910
4,0761910
2
this setenv script isn't necessary for package.json scripts. npm already prepends the local node_modules/.bin directory to path for you when executing npm run {scripts}.
– jasonkarns
Feb 6 '15 at 19:39
add a comment |
2
this setenv script isn't necessary for package.json scripts. npm already prepends the local node_modules/.bin directory to path for you when executing npm run {scripts}.
– jasonkarns
Feb 6 '15 at 19:39
2
2
this setenv script isn't necessary for package.json scripts. npm already prepends the local node_modules/.bin directory to path for you when executing npm run {scripts}.
– jasonkarns
Feb 6 '15 at 19:39
this setenv script isn't necessary for package.json scripts. npm already prepends the local node_modules/.bin directory to path for you when executing npm run {scripts}.
– jasonkarns
Feb 6 '15 at 19:39
add a comment |
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"
Addingnpm bin
to end of$PATH
may not execute what user expects: basically another executable but more probably a globally installed package with another version !
– LoganMzz
Nov 8 '17 at 13:46
add a comment |
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"
Addingnpm bin
to end of$PATH
may not execute what user expects: basically another executable but more probably a globally installed package with another version !
– LoganMzz
Nov 8 '17 at 13:46
add a comment |
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'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"
edited Aug 23 '16 at 5:30
answered Jul 15 '16 at 1:48
Omar Diab
623924
623924
Addingnpm bin
to end of$PATH
may not execute what user expects: basically another executable but more probably a globally installed package with another version !
– LoganMzz
Nov 8 '17 at 13:46
add a comment |
Addingnpm bin
to end of$PATH
may not execute what user expects: basically another executable but more probably a globally installed package with another version !
– LoganMzz
Nov 8 '17 at 13:46
Adding
npm bin
to end of $PATH
may not execute what user expects: basically another executable but more probably a globally installed package with another version !– LoganMzz
Nov 8 '17 at 13:46
Adding
npm bin
to end of $PATH
may not execute what user expects: basically another executable but more probably a globally installed package with another version !– LoganMzz
Nov 8 '17 at 13:46
add a comment |
I am a Windows
user and this is what worked for me:
// First set some variable - i.e. replace is with "xo"
D:projectroot> set xo="./node_modules/.bin/"
// Next, work with it
D:projectroot> %xo%/bower install
Good Luck.
add a comment |
I am a Windows
user and this is what worked for me:
// First set some variable - i.e. replace is with "xo"
D:projectroot> set xo="./node_modules/.bin/"
// Next, work with it
D:projectroot> %xo%/bower install
Good Luck.
add a comment |
I am a Windows
user and this is what worked for me:
// First set some variable - i.e. replace is with "xo"
D:projectroot> set xo="./node_modules/.bin/"
// Next, work with it
D:projectroot> %xo%/bower install
Good Luck.
I am a Windows
user and this is what worked for me:
// First set some variable - i.e. replace is with "xo"
D:projectroot> set xo="./node_modules/.bin/"
// Next, work with it
D:projectroot> %xo%/bower install
Good Luck.
answered Jan 26 '17 at 9:27
Akash
6,12013840
6,12013840
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
edited May 19 '17 at 16:45
answered May 19 '17 at 16:29
Leon li
2,6642428
2,6642428
add a comment |
add a comment |
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.
yeah, I got that far as I stated in my question. how do I specifically call the one in my project besides ./node_modules/.bin/coffee?
– typeoneerror
Mar 13 '12 at 9:47
If you've run npm install with the package.json in your project's main folder, you should have a ./node_modules/.bin/coffee folder in this folder. Using ./node_modules/coffee-script/bin/coffee will run the local version of coffee while just running coffee will run the global installation. If you have another version of coffee installed in another path within this project folder, you can access it using ./path/to/this/installation/coffee.
– almypal
Mar 13 '12 at 10:42
This did not work for me. I am trying to use "svgo", and it only works when installed globally. I have triednpm install svgo
as well asnpm install
with package.json. Both methods installed "successfully", but the "svgo" command is still not available.
– Ryan Wheale
Apr 5 '13 at 4:37
1
Grunt uses this in a clever way, and IMHO so should other packages. First you install thegrunt-cli
package globally, then in your project directory install any (modified) version of thegrunt
package, then when you rungrunt
, it will use this local version.
– ack
Feb 28 '14 at 21:00
add a comment |
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.
yeah, I got that far as I stated in my question. how do I specifically call the one in my project besides ./node_modules/.bin/coffee?
– typeoneerror
Mar 13 '12 at 9:47
If you've run npm install with the package.json in your project's main folder, you should have a ./node_modules/.bin/coffee folder in this folder. Using ./node_modules/coffee-script/bin/coffee will run the local version of coffee while just running coffee will run the global installation. If you have another version of coffee installed in another path within this project folder, you can access it using ./path/to/this/installation/coffee.
– almypal
Mar 13 '12 at 10:42
This did not work for me. I am trying to use "svgo", and it only works when installed globally. I have triednpm install svgo
as well asnpm install
with package.json. Both methods installed "successfully", but the "svgo" command is still not available.
– Ryan Wheale
Apr 5 '13 at 4:37
1
Grunt uses this in a clever way, and IMHO so should other packages. First you install thegrunt-cli
package globally, then in your project directory install any (modified) version of thegrunt
package, then when you rungrunt
, it will use this local version.
– ack
Feb 28 '14 at 21:00
add a comment |
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.
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.
answered Mar 13 '12 at 7:58
almypal
5,14931723
5,14931723
yeah, I got that far as I stated in my question. how do I specifically call the one in my project besides ./node_modules/.bin/coffee?
– typeoneerror
Mar 13 '12 at 9:47
If you've run npm install with the package.json in your project's main folder, you should have a ./node_modules/.bin/coffee folder in this folder. Using ./node_modules/coffee-script/bin/coffee will run the local version of coffee while just running coffee will run the global installation. If you have another version of coffee installed in another path within this project folder, you can access it using ./path/to/this/installation/coffee.
– almypal
Mar 13 '12 at 10:42
This did not work for me. I am trying to use "svgo", and it only works when installed globally. I have triednpm install svgo
as well asnpm install
with package.json. Both methods installed "successfully", but the "svgo" command is still not available.
– Ryan Wheale
Apr 5 '13 at 4:37
1
Grunt uses this in a clever way, and IMHO so should other packages. First you install thegrunt-cli
package globally, then in your project directory install any (modified) version of thegrunt
package, then when you rungrunt
, it will use this local version.
– ack
Feb 28 '14 at 21:00
add a comment |
yeah, I got that far as I stated in my question. how do I specifically call the one in my project besides ./node_modules/.bin/coffee?
– typeoneerror
Mar 13 '12 at 9:47
If you've run npm install with the package.json in your project's main folder, you should have a ./node_modules/.bin/coffee folder in this folder. Using ./node_modules/coffee-script/bin/coffee will run the local version of coffee while just running coffee will run the global installation. If you have another version of coffee installed in another path within this project folder, you can access it using ./path/to/this/installation/coffee.
– almypal
Mar 13 '12 at 10:42
This did not work for me. I am trying to use "svgo", and it only works when installed globally. I have triednpm install svgo
as well asnpm install
with package.json. Both methods installed "successfully", but the "svgo" command is still not available.
– Ryan Wheale
Apr 5 '13 at 4:37
1
Grunt uses this in a clever way, and IMHO so should other packages. First you install thegrunt-cli
package globally, then in your project directory install any (modified) version of thegrunt
package, then when you rungrunt
, it will use this local version.
– ack
Feb 28 '14 at 21:00
yeah, I got that far as I stated in my question. how do I specifically call the one in my project besides ./node_modules/.bin/coffee?
– typeoneerror
Mar 13 '12 at 9:47
yeah, I got that far as I stated in my question. how do I specifically call the one in my project besides ./node_modules/.bin/coffee?
– typeoneerror
Mar 13 '12 at 9:47
If you've run npm install with the package.json in your project's main folder, you should have a ./node_modules/.bin/coffee folder in this folder. Using ./node_modules/coffee-script/bin/coffee will run the local version of coffee while just running coffee will run the global installation. If you have another version of coffee installed in another path within this project folder, you can access it using ./path/to/this/installation/coffee.
– almypal
Mar 13 '12 at 10:42
If you've run npm install with the package.json in your project's main folder, you should have a ./node_modules/.bin/coffee folder in this folder. Using ./node_modules/coffee-script/bin/coffee will run the local version of coffee while just running coffee will run the global installation. If you have another version of coffee installed in another path within this project folder, you can access it using ./path/to/this/installation/coffee.
– almypal
Mar 13 '12 at 10:42
This did not work for me. I am trying to use "svgo", and it only works when installed globally. I have tried
npm install svgo
as well as npm install
with package.json. Both methods installed "successfully", but the "svgo" command is still not available.– Ryan Wheale
Apr 5 '13 at 4:37
This did not work for me. I am trying to use "svgo", and it only works when installed globally. I have tried
npm install svgo
as well as npm install
with package.json. Both methods installed "successfully", but the "svgo" command is still not available.– Ryan Wheale
Apr 5 '13 at 4:37
1
1
Grunt uses this in a clever way, and IMHO so should other packages. First you install the
grunt-cli
package globally, then in your project directory install any (modified) version of the grunt
package, then when you run grunt
, it will use this local version.– ack
Feb 28 '14 at 21:00
Grunt uses this in a clever way, and IMHO so should other packages. First you install the
grunt-cli
package globally, then in your project directory install any (modified) version of the grunt
package, then when you run grunt
, it will use this local version.– ack
Feb 28 '14 at 21:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9679932%2fhow-to-use-package-installed-locally-in-node-modules%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
A lot of instructions I read say things like
npm install niftycommand
and thenniftycommand
. But this will never work unless you have ./node_modules/.bin in your path, will it?– Bennett McElwee
Sep 20 '16 at 5:03
1
There is a very good writeup here: firstdoit.com/… — Basically it recommends you to put your
coffee
command into thenpm scripts
section, like"build": "coffee -co target/directory source/directoy", so you can run
npm run build` from the terminal afterwards.– Benny Neugebauer
Jan 22 '17 at 21:27
@BennyNeugebauer indeed, that's what I've been doing lately instead of messing with PATH
– typeoneerror
Feb 2 '17 at 17:36
3
Use
npx
which comes withnpm 5.2.0
medium.com/@maybekatz/…– onmyway133
Mar 14 at 13:18