Saturday, October 5, 2013

Bower on windows

Package Manager for The Web

I'm building a framework that one of its requirements is to support web packages dynamically. One of my friends told me about bower. I instantly adopted bower as part of my CI .
As you all don't know my daily based development environment is running on Ubuntu. With that I have less worries, until I need to go and validate my code on Windows OS...

And then I got hit...

I got a few strange errors while running my code. I start looking into it and I found out that the statement require('bower') resulting a reference error. I checked my bower installation and it looked like everything is by the book.
Finally, while googling I saw a comment that someone suggested: "for windows, try install bower locally too". Hmmm.. I did that and it works for me, for now. Might be an issue...

Still Looking for a workaround...

After installing bower globally and locally (A reminder: on windows) I still didn't get it to work and fetch my packages. Now I got error code 127. Ok that error came from the 'spawn' command probably.
First thing I checked if I can run bower from the cmd and indeed it works like a charm. So it probably related to the nodejs environment that spawn a command line to bower, for example:

bower.commands
.install(['chai'], { save: true }, {})
.on('end', function (installed) {
    console.log(installed);
});

I looked into the code error:127 and this code is about running git command. Well some will say it's about code:128
I looked for an answer and I got many that some said it helped them. In my case nothing helped and I can say that for my users I cannot give this solutions since it might not be helpful for them either.
Here are some of the solutions:
The Summary of those are, clean bower cache, put git.exe into path or use other configuration for git.


My, worked for me...

My solution to this issue is.... I just created a simple batch file that gets the bower information as arguments. Nodejs then detect that the running OS is windows and using a standard spawn command, calling to my batch file that is running externally to the nodejs.

I just hope that someone will come up with a better solution ...

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