Friday, August 30, 2013

npm install ++

npm install ++

I have this npm project that I'm working on for some time now. I got to a point that I want to give my users a smooth nodejs package installation process. It means that any additional global dependencies that the user will be asked to install in additional will be part of the "npm install" process.

I start thinking of how can I accomplish that? I searched the web and bumped into some possibilities such as adding a script section to the package.json file.
Sounds like a great idea, so I gave it a try.

How can I add a script to a package.json file?

For adding a script to the package.json file, all you have to do is add a script section. Read about it in here
...
 "scripts": {
    "install": "npm install -g bower"
  }
...

I executed the above script as part of my package.json file, on my Ubuntu environment, but I got an error: permission denied. Oh well I need to add 'sudo' to the command, but how can I get the password prompt?! Another question was, how can I add cross platform script? I cannot change the command dynamically.

Use nodejs script for get the job done

I thought about how can I solve this one. I had some ideas but the one i used was to execute nested nodejs module for getting cross platform installation.
...
 "scripts": {
    "install": "node installer.js"
  }
...

Spawning with sudo

I created an installer.js script and my first task was to spawn a process with a sudo command. For that I used the sudo package that is pretty much using the spawn but with sudo prompt and it's working just great.


Wrap up

All I have left to do is detecting on which OS I'm running and with that spawn with sudo or w/o and i get to run 'npm install' that gets the dependencies as part of its process.


NPM Module: package-script

I created a module just for that, you can use it...
npm install package-script

1 comment: