How to use package installed locally in node_modules?

YOU can get the path of locally installed binaries with

npm bin

To execute a locally installed Eg: backtopjs binary independent of where you are in the project directory hierarchy you can use this bash construct

PATH=$(npm bin):$PATH backtopjs 

you can also aliased this to npm-exec

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

So, now you can

npm-exec backtopjs 

to run the correct copy of backtopjs no matter of where you are

$ pwd

$ npm-exec which backtopjs  

$ cd lib/
$ npm-exec which backtopjs  

$ cd ~/project2
$ npm-exec which backtopjs  

Another easier way now is to use npm

Install using

npm i npx
$ npx [options] <command>[@version] [command-arg]...

By default, npx will check whether exists in $PATH, or in the local project binaries, and execute that.

Calling npx </code> when 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-installoption.

For npm < 5.2.0, you can install npx package manually by running the following command npm i -g npx

Leave a Comment

Your email address will not be published. Required fields are marked *