{

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.

Locally: In your application, if you run npm install package, It installs to node_modules folder and the It creates an link to node_modules.bin folder Globally:

npm install package -g

It installs to global node_modules folder(/usr/local/ in Unix, %AppData%/npm in windows.). Path to 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 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 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

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.