Click here to Skip to main content
15,867,834 members
Articles / All Topics

Avoid global npm installs for Projects

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 Oct 2015CPOL1 min read 6.9K  
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)


Written By
Software Developer
Norway Norway
My name is Gjermund Bjaanes. I am 25 years old, born and raised in Norway. I am a developer, geek and enthusiast in general. I’m passionate about programming and software engineering in general. Currently I focus on web development and mobile development on my free time.

I am a software engineer and developer by trade and by passion.

I have always had a very enthusiastic interest for computers, dated to when I was little. It became an obsession for programming, IT, gadgets and new technology. I love being a developer, and that is why I do it as much as I can!

Other than that; In my spare time I like to code, read, cook, hang out with friends and to work out.

Comments and Discussions

 
-- There are no messages in this forum --