Running NPM modules locally

06 May 2017

When a node module is globally installed using npm install webpack -g, we can access it from the command prompt as
> webpack ./entry.js bundle.js 

Consider if you want a particular version of package for a project/folder. Then you will install the  package without -g for that project alone, then you can't access the package directly as mentioned above.

To execute you can do one of the following

  1. Navigate to ./node_modules/webpack/ folder and then execute above statement with the JS file paths fixed. but this might create many confusions when you have multiple bundles.
  2. If you don't have packages.json in your folder then perform npm init, after which you can add following npm run-scripts like below
"scripts": {
    "build": "webpack ./entry.js bundle.js --module-bind \"css=style-loader!css-loader\"",
    "test": "echo \"Error: no test specified\" && exit 1"

The above build script can be triggered using npm run build from command prompt.

This will allow you to run the package using your local modules from "node_modules/packagename/", if not found then it automatically checks global.

When running the build command in Windows, the loader options should be escaped using \" as shown above.