Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Avoid global npm installs for Projects

0.00/5 (No votes)
10 Oct 2015CPOL1 min read 7.1K  
Avoid global npm installs for projects

Have you ever required gulp or grunt or something similar to be installed globally to be able to build a web project? You probably require npm as well? And possibly bower?

Having these kinds of global requirements are not healthy, nor is it very helpful. It’s much harder to get started with a project if there are many steps you have to follow, just to get all the requirements installed.

There is a much better way that doesn’t require as much set up for a new dev environment.

npm

npm logo

What if you could just use npm for everything? The only thing you really need is node and npm (which usually comes with node.js anyways). The great answer here is npm scripts.

I have recently moved gulp, karma and protractor away from my required installs in my Extreme Results app (which I wrote about here: Learning Web Dev Series – Part 5: Extreme Results). These things are now provided when running “npm install”.

The way to still be able to run all your gulp tasks and what not, is by using npm scripts. For example, instead of doing:

gulp serve

to build, set up watchers and server, you could just do:

npm start

In package.json:

"scripts": {
    "start": "gulp serve"
}

Easy and nice!

Npm has a couple of shorthands like start and test, but for custom script names, you have to do:

npm run script_name

These are the scripts I have right now:

JavaScript
"scripts": {
    "clientdep": "bower install --no-interactive",
    "start": "gulp",
    "test": "gulp tdd",
    "selenium": "webdriver-manager update && webdriver-manager start",
    "e2e": "protractor protractor.conf.js"
},

These can be run like this:

Installs all extra dependencies (from bower, since I am still using that, for now):

npm run clientdep

Build, watchers (automatically reload) and serve (local webserver for development):

npm start

Run unit tests with watchers (automatically rereun tests):

npm test

Update web drivers and start a selenium server for protractor to use:

npm run selenium

Run e2e tests with protractor:

npm run e2e

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)