Assembling portable Node.js (Manually installing nodeJS into folder under Windows)

December 2nd, 2014 | Permalink

For those who (like me) want to use nodeJS (or, like me, soft written on it, like LESS or bower) in a folder without installing it. And those, who want to have it somewhere on their USB or i don’t know. For example, for LESS there is a whole plenty of third-party compilers. But original is always better, isn’t it?

This approach may be useful for numerous occasions, one of which is having several different versions of node on your PC. Also it may be handy to set everything up once and then use it on different machines, without interfering with those machines’ software.

 1. Node.exe

First of all, nodeJS is pretty portable by itself. It is a single exe-file, node.exe, which you can always download from nodejs.org/dist:

http://nodejs.org/dist/latest/node.exe

2. Npm

Then, there’s npm. It has it’s own folder on nodejs.org/dist:

http://nodejs.org/dist/npm/

There’s date on the right of file list, so you probably will need latest zip archive available. For today it’s version 1.4.9

3. Unpacking

Just create the folder for the nodejs somewhere and unpack npm package into it and copy node.exe there, too. So npm.cmd and node.exe will be alongside:

PZHsoIC[1]

That’s all actually. Now you can open a command line in that folder and make sure you can use node and npm:

0amOm0f[1]

4. Installing packages and creating launchers

Now you can install node packages (say, using npm install).

Some packages (like less) create own cmd files for execution in node folder. Some others (like bower) create them in node_modules/.bin folder:

W9Tb7bb[1]

 

Hddh3FK[1]

So now i can call lessc from c:\nport\node\lessc.cmd and bower from c:\nport\node\node_modules\bower.cmd. But! Because while installing bower we didn’t use the -g or -global flag – bower cmd file requires node.exe to be available globally on the system (added to system path):

7q1WhEi[1]

What we need to do now is create another executable for bower. In order to do so – let’s first take a look at node_modules\.bin\bower.cmd file:

@IF EXIST "%~dp0\node.exe" (
 "%~dp0\node.exe" "%~dp0\..\bower\bin\bower" %*
) ELSE (
 node "%~dp0\..\bower\bin\bower" %*
)

Actually we don’t need most of these. All we need to know from the file is the name of bower’s executable script (that should be passed to node as parameter), which is, in this case, bower\bin\bower. Now we can write a new cmd file (alongside node.js) to run bower from that folder:

 @ECHO OFF
"%~dp0\node.exe" "%~dp0\node_modules\bower\bin\bower" %*

Let’s look at second line first. %~dp0 is a link to the folder containing our cmd file. In my case it is c:\nport\node. %* stands for all the parameters passed to our cmd file on run. So if this line is saved alongside node.exe into, say, bower.cmd into c:\nport\node and called with -v parameter the result will be something like "c:\nport\node\\node.exe" "c:\nport\node\\node_modules\bower\bin\bower" -v:

7gv5pzp[1]

Which is exactly what we need: now we can run c:\nport\node\bower.cmd from anywhere we want:
IZaQFO3[1]

 

The @ECHO OFF line will make sure “c:\nport\node\\node.exe”  “c:\nport\node\\node_modules\bower\bin\bower” -v line will not be outputted to user.

5. Adding to PATH

Now, to run lessc, node, npm and bower from everywhere on this pc under this user you can modify PATH system variable to add c:\nport\node to it.

At this point – why may the whole approach be better than downloading node installer from nodejs.org? Only because you have total control now with this approach (when using node installed globally in the system via installer the cmd files are not always working and PATHs are not always correctly set somehow). Also, portability matters.

In order to add this – go to computer properties > Advanced system properties (or type systempropertiesadvanced in Run dialog), there should be “Environment Variables” button, under User Variables find PATH and double-click it, add ;C:\nport\node to the end of it, press OK:

DZoHnFg[1]

 

Or simply execute setx PATH "%PATH%;C:\nport\node" from your commandline which is much easier when you know what you are doing.

Now you should be able to type ‘bower’ or ‘lessc’ from anywhere on your pc and get it running.