I was typing out a response to a question on this site, when I noticed that my answer was getting a little long.

Then I started thinking about how frustrating it was when I was first trying to set up my Windows environment for NodeJS, with Yo, Grunt, Bower and the like.

Or, that time when I couldn’t even install a package because the file name for a file within the package exceeded the allowed length by Windows!

I would get errors about these packages not being found or being invalid. The answers on Stack Overflow would tell people to ‘fix their PATH’, or ‘check their PATH’. (Yeah, I’m totally guilty of telling people to do that too!).

But when you don’t know what a PATH is, how do you fix it?!

So the assumption here is that you already have NodeJS installed, and you have a couple of global packages installed too. You installed a package globally when you added a -g to it like this:

npm install -g bower

When you install a package globally, a couple of things seem to happen:
Firstly, your package is installed in a temp location like C:\Users\Jane\AppData\Roaming\npm, and
Secondly, (following the bower example) you also get a bower windows command script file, and a random bower file.

In order to actually use these files, you need to refer to them from your command line utility (e.g. PowerShell or Command Prompt) like so bower install angular.

But you also need to tell your command line utility where the ‘bower’ files live on your computer, or you get a PATH related error.

What is a PATH related error?

An error that is related to your system PATH will look something like this:

When you type into your command line:
npm 'dosomething'

'node' is not recognized as an internal or external command, operable program or batch file.

grunt 'dosomething'

    throw err;
Error: Cannot find module 'C:\Program Files (x86)\nodejs\node_modules\.bin\node_modules\grunt-cli\bin\grunt'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Or even
yo meanjs

Error meanjs
You don't seem to have a generator with the name meanjs installed.
You can see available generators with npm search yeoman-generator and then install them with npm install [name].
To see the 3 registered generators run yo with the `--help` option.

Got it. But what can I do about it? How do I fix this thing?

To fix this properly, and to have real control of your NodeJS packages, you need to do two things.

Thing to do number 1:

First vow to take control of your NodeJS packages.

This involves closing your eyes and chanting “I will take control of my NodeJS packages” out loud. 50 times.

I jest, it’s actually even easier than that.

Find a place where you want to install your Node Packages. I install my global packages here (you should put them wherever it makes you comfortable):

C:\Program Files (x86)\nodejs

There are 3 key folders that need to be referenced:

C:\Program Files (x86)\nodejs
C:\Program Files (x86)\nodejs\node_modules
C:\Program Files (x86)\nodejs\node_modules\.bin

Next you need to find where your packages are currently installed, it’s likely something like: C:\Users\Jane\AppData\Roaming\npm, then you need to move them into your ideal location.

I haven’t done any kind of scientific analysis, but I put the global windows command scripts here:

C:\Program Files (x86)\nodejs 
C:\Program Files (x86)\nodejs\node_modules\.bin 

(but not in both places)

and the actual packages go here:

C:\Program Files (x86)\nodejs\node_modules

Thing to do number 1 and a half:

Update your PATH to include your freshly moved files.

Here is a lovely ‘draw-n-snip-o-graphic’:

For example, my PATH variable looks like this:

...heaps of stuff...;C:\Program Files (x86)\nodejs;C:\Program Files (x86)\nodejs\;C:\Program Files (x86)\nodejs\node_modules\;C:\Program Files (x86)\nodejs\node_modules\.bin\ 

Thing to do number 2:

Remember that any time you make an update to your Environment Variables, you’ll need to close and reopen your command line utility to recognise the updates.

Also, when you install new global packages, often they’ll revert back to your original temp location, so just keep in mind that you may have to move the new packages you install. Just like we did with your existing packages above.

When you’re moving the files, if you put a command script into the .bin folder and it doesn’t work, then just copy it across to your equivalent of the top level folder, like my ‘nodejs’ folder.

Hope this helps with your pesky PATH problems, or when you hit that error about the package file names being too long.

Take back control of your -g NodeJS packages on Windows

1 Comment

Let me know what you think

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • George

    You could just install nvm-windows if you really want your npm packages in the \Program Files directory instead of manually moving a package each time you do a global install