NPM and run-script

actionhero javascript node.js 
2012-09-09
↞ See all posts



Just a handy reminder that you can add "arbitrary" commands to your node NPM modules without creating binaries.

One of my favorite features of NPM is the option of installing packages either globally or locally (with local as the default). Do you want a special version of forever for project-_a and not project-_b? You can!

1global: `forever start app.js` 2local: `./node_modules/.bin/forever start app.js`

You can also add ./node_modules/.bin to your path when developing to make this even easier.

This saves me so many headaches when compared to Ruby. There’s no need for something like Bundler (which is a great solution to Ruby’s problems BTW). I need to explicitly opt do something globally, so I should be aware of what I am getting into. The downside of this is potentially wasted disk space for duplicate packages, but I am OK with that. Because of NPM’s philosophy of local execution, you may not want to create binaries for your packages, but still might want a way to call actions from the command line. NPM to the rescue!

The following syntax can be used:

1npm run-script #{package} #{command}

You all probably know that you can define a "run" and "test" actions for any package, but you can keep adding more. For example, here’s the relevant scripts block from actionHero:

1{ 2 "scripts": { 3 "start": "./scripts/actionHero", 4 "startCluster": "./scripts/actionHeroCluster", 5 "install": "./scripts/install", 6 "test": "./node_modules/.bin/vows spec/* -v --dot-matrix", 7 "generate": "./scripts/generate", 8 "generateAction": "./scripts/generateAction", 9 "generateTask": "./scripts/generateTask" 10 } 11}

By default, NPM will use the "run", "start", "stop", "restart" and "test" actions from the directory you are currently in. However, you can run actions from any package available to you (either local or global) with run-script. actionHero uses this to crete new projects with the command "npm run-script actionHero generate". Note the syntax:

1npm run-script #{package} #{command}
Hi, I'm Evan

I write about Technology, Software, and Startups. I use my Product Management, Software Engineering, and Leadership skills to build teams that create world-class digital products.

Get in touch