How I Avoid Installing npm Packages Globally

Posted on

First, I take the installations instructions, but instead of installing globally, I install as a dev dependency inside my project (webpack used for this example):

1
npm install webpack -D

Second, I check the node_modules bin directory to confirm that I have the webpack binary

1
ls node_modules/.bin/

That directory contains symlinks to all the binaries installed with npm for a specific project

The contents of a node_modules/.bin directory

Third, I create an npm script for easily running the binary found in step 2

in package.json, add the following to your package.json file in your scripts:

1
"wp": "node_modules/.bin/webpack"

Here’s how it looks in a project I setup:

1
2
3
4
5
6
7
8
9
10
11
12
{
  "name": "test001",
  "version": "0.0.1",
  "description": "A test of es6 with nw.js",
  "author": "Faison Zutavern",
  "scripts": {
    "wp": "node_modules/.bin/webpack"
  },
  "devDependencies": {
    "webpack": "^1.13.2"
  }
}

Now you can run the following command from any directory in your project and it will run webpack for you

1
npm run wp

Leave a Reply

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