Posted

2 minutes read

There are a few options to achieve that if you are using npm prior to version 5.2.0 and we list all options below.

If you are using npm greater than 5.2.0, have a look at our previous article how to use npx the npm package runner.

  1. First option would be to export the path in the .bashrc (or .zshrc file, depending on which one you use).

If you put the following


./node_modules/.bin

into your PATH variable it only works when your current working directory is the root of your project directory structure

Here is the npm command to get the path of locally install binaries:


npm bin

which will output a similar path


/Users/[user]/[project]/node_modules/.bin

Prior to npm 5.2.0, you should add the following to you PATH variable to execute a locally install module:


PATH=$(npm bin):$PATH jest

You can also create an alian for the execute command similar to:


alias npm-run='PATH=$(npm bin):$PATH'

and then you can run the following:


npm-run jest

or go even further


alias jest="npm-run jest"

Node: don’t forget to source your .bashrc (or .zshrc file in your terminal with


source .bashrc

or


source .zshrc

  1. Second option is to export the path to .bashrc (or .zshrc file, depending on which one you use).


export PATH="$(npm bin):$PATH"

  1. Third option is to to use npm scripts in package.json, because the always lookup the local binaries first.
    You can set up aliases or just use generic naming convention for scripts like “build”, “test”, “run:local”, “run:prod”, etc.

More details related to npm scripts.


"scripts": {
    "mymodule": "mymodule"
}

Then you can run with:


npm run mymodule

and even use arguments:


npm run mymodule -- args