How to use executables from a package installed locally in node_modules?
This tutorial uses how to run executable files from the node_modules folder of an application.
Npm packages are installed locally or globally using the npm install command. You can also check other posts on npm command deprecate option is deprecated Locally: In your application, if you run the npm install package, It installs to the node_modules folder and the It creates a link to the node_modules.bin folder Globally:
npm install package -g
It installs to the global node_modules folder(/usr/local/ in Unix, %AppData%/npm in windows.). The path to the binary folder is /usr/local/bin folder in Unix and
For example, if you install the global package using -g
option
npm install -g http-server
It installs the binary path to the global path and works on any folder with a command.
http-server
It is a good idea to install locally always as we can have different versions of running executable binary scripts or CLI commands from different applications.
Run binary executable from node_module package locally
First, Install http-server locally on a project.
npm install http-server
the http-server executable is added to the node_modules/.bin folder.
It works automatically in the application with the recent npm version.
In the older versions, add node_modules/.bin
path environment variable PATH
. You can use the command line as given below
http-server
or with npm scripts
scripts: {
'server': http-server
}
Below commands(npm root and npm bin) gives an idea of the npm binary path and node_modules folder path.
A:\work\react-css-examples>npm root
A:\work\react-css-examples\node_modules
A:\work\react-css-examples>npm bin
A:\work\react-css-examples\node_modules\.bin