3

I have installed node.js at my local system (path : C:\Program Files\nodejs). I installed some modules and expected them to be placed in (C:\Program Files\nodejs\node_modules). But, the installed modules are placed at C:\Users\Administrator\AppData\Roaming\npm\node_modules. Is this right or wrong? Can i access these modules globally?

My system platform is windows 7, and my version of node is 0.10.

Ari
  • 2,933
  • 5
  • 21
  • 44
Harsan
  • 55
  • 1
  • 6

4 Answers4

3

The current (January 2018) version of Node.js is 9.4.0, so I'm not sure if it is compatible with your version.

You can set the default global installation path for node_modules by modifying your npmrc file.

Execute in a prompt: npm config list. It should among other things display a prefix setting, which is set to your Roaming AppData folder, for example: C:\Users\Administrator\AppData\Roaming\npm. You can override this setting by executing npm config set prefix C:\Program Files\nodejs\node_modules\npm.

Now, once you install node_modules globally they will be placed in that directory.

2

Running just npm install will install all modules into a folder in the current directory called node_modules; all files within the same root directory (even in sibling folders), will check for modules here when you call require. You should install any modules that you wish to require in your project this way.

If you want to install a module from npm globally, you can append the -g flag. This is usually for command-line tools, which you want to be accessible across multiple projects. An example would be npm install nodemon -g

If you are still confused, I recommend you reference this blog post from the makers of node on global/local installation: http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/

Ari
  • 2,933
  • 5
  • 21
  • 44
  • 2
    Global modules are *not* modules which can be used for any project without installing locally. Global modules are usually just command line tools. You can't install any module globally and expect it to work in any project. – Munim Nov 29 '13 at 11:11
  • @Munim Of course. Perhaps my wording was unclear; I'll edit it to remove confusion. – Ari Nov 29 '13 at 11:13
  • Thanks. Just wanted to point it out, because one in every five questions about node.js these days is about global modules not working when `require`d. – Munim Nov 29 '13 at 11:14
2

You can find out default paths (user's and global's ones) by command:

npm config list

It is in 'prefix' variable, e.g.:

; userconfig C:\Users\pavel\.npmrc
cache = "C:\\ProgramData\\npm-cache"
prefix = "C:\\ProgramData\\npm"

To change default path have to use the command, e.g.:

npm config set prefix="C:\ProgramData\npm"
Pax Beach
  • 1,079
  • 14
  • 20
0

By default, any packages you install will install to a global installation directory which is why they are showing up in C:\Users\Administrator\AppData\Roaming\npm\node_modules. If you want to install the packages to your local node_modules folder you will need to type in the following:

npm install (package name) --save-dev
SDH
  • 383
  • 3
  • 17