Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globally set node_modules location for project

If I write

var moment = require('moment');

in my project, Node wastes a lot of time looking in places that do not actually contain the file, as this dtruss output shows.

       PID/THRD  RELATIVE SYSCALL(args)                 = return
 7079/0x7cf313:   1244530 stat64("/Users/burke/code/api/api/models/node_modules/moment\0", 0x7FFF5FBFE5D8, 0x9)          = -1 Err#2
 7079/0x7cf313:   1244575 stat64("/Users/burke/code/api/api/models/node_modules/moment.js\0", 0x7FFF5FBFE578, 0x9)               = -1 Err#2
 7079/0x7cf313:   1244595 stat64("/Users/burke/code/api/api/models/node_modules/moment.json\0", 0x7FFF5FBFE578, 0x9)             = -1 Err#2
 7079/0x7cf313:   1244612 stat64("/Users/burke/code/api/api/models/node_modules/moment.node\0", 0x7FFF5FBFE578, 0x9)             = -1 Err#2
 7079/0x7cf313:   1244628 stat64("/Users/burke/code/api/api/models/node_modules/moment.coffee\0", 0x7FFF5FBFE578, 0x9)           = -1 Err#2
 7079/0x7cf313:   1244663 open("/Users/burke/code/api/api/models/node_modules/moment/package.json\0", 0x0, 0x1B6)                = -1 Err#2
 7079/0x7cf313:   1244694 stat64("/Users/burke/code/api/api/models/node_modules/moment/index.js\0", 0x7FFF5FBFE578, 0x1B6)               = -1 Err#2
 7079/0x7cf313:   1244713 stat64("/Users/burke/code/api/api/models/node_modules/moment/index.json\0", 0x7FFF5FBFE578, 0x1B6)             = -1 Err#2
 7079/0x7cf313:   1244729 stat64("/Users/burke/code/api/api/models/node_modules/moment/index.node\0", 0x7FFF5FBFE578, 0x1B6)             = -1 Err#2
 7079/0x7cf313:   1244745 stat64("/Users/burke/code/api/api/models/node_modules/moment/index.coffee\0", 0x7FFF5FBFE578, 0x1B6)           = -1 Err#2
 7079/0x7cf313:   1244767 stat64("/Users/burke/code/api/api/node_modules/moment\0", 0x7FFF5FBFE5D8, 0x1B6)               = -1 Err#2
 7079/0x7cf313:   1244788 stat64("/Users/burke/code/api/api/node_modules/moment.js\0", 0x7FFF5FBFE578, 0x1B6)            = -1 Err#2
 7079/0x7cf313:   1244805 stat64("/Users/burke/code/api/api/node_modules/moment.json\0", 0x7FFF5FBFE578, 0x1B6)          = -1 Err#2
 7079/0x7cf313:   1244821 stat64("/Users/burke/code/api/api/node_modules/moment.node\0", 0x7FFF5FBFE578, 0x1B6)          = -1 Err#2
 7079/0x7cf313:   1244837 stat64("/Users/burke/code/api/api/node_modules/moment.coffee\0", 0x7FFF5FBFE578, 0x1B6)                = -1 Err#2
 7079/0x7cf313:   1244862 open("/Users/burke/code/api/api/node_modules/moment/package.json\0", 0x0, 0x1B6)               = -1 Err#2
 7079/0x7cf313:   1244887 stat64("/Users/burke/code/api/api/node_modules/moment/index.js\0", 0x7FFF5FBFE578, 0x1B6)              = -1 Err#2
 7079/0x7cf313:   1244904 stat64("/Users/burke/code/api/api/node_modules/moment/index.json\0", 0x7FFF5FBFE578, 0x1B6)            = -1 Err#2
 7079/0x7cf313:   1244920 stat64("/Users/burke/code/api/api/node_modules/moment/index.node\0", 0x7FFF5FBFE578, 0x1B6)            = -1 Err#2
 7079/0x7cf313:   1244936 stat64("/Users/burke/code/api/api/node_modules/moment/index.coffee\0", 0x7FFF5FBFE578, 0x1B6)          = -1 Err#2
 7079/0x7cf313:   1244964 stat64("/Users/burke/code/api/node_modules/moment\0", 0x7FFF5FBFE5D8, 0x1B6)           = 0 0
 7079/0x7cf313:   1244990 stat64("/Users/burke/code/api/node_modules/moment.js\0", 0x7FFF5FBFE578, 0x1B6)                = -1 Err#2
 7079/0x7cf313:   1245015 stat64("/Users/burke/code/api/node_modules/moment.json\0", 0x7FFF5FBFE578, 0x1B6)              = -1 Err#2
 7079/0x7cf313:   1245038 stat64("/Users/burke/code/api/node_modules/moment.node\0", 0x7FFF5FBFE578, 0x1B6)              = -1 Err#2
 7079/0x7cf313:   1245488 madvise(0x1008AE000, 0x21000, 0x9)             = 0 0
 7079/0x7cf313:   1245503 stat64("/Users/burke/code/api/node_modules/moment.coffee\0", 0x7FFF5FBFE578, 0x9)              = -1 Err#2
 7079/0x7cf313:   1245612 open("/Users/burke/code/api/node_modules/moment/package.json\0", 0x0, 0x1B6)           = 11 0

Is there a way to make Node not waste so much time looking in places that don't contain a node_modules directory? Like, I could set some kind of CHECK_HERE_FIRST=$HOME/code/api/node_modules environment variable and if the require is not for a relative path, that would be the first place that Node checked.

I could change all of my require lines to load the relative import but this seems cumbersome for a large project.

like image 690
Kevin Burke Avatar asked Apr 20 '15 02:04

Kevin Burke


People also ask

How do I change the location of a global node module?

js documentation and found that it can be changed very easily using a simple "npm config" command. For example: npm config set prefix "E:\node_modules", From the next time onward, every global installation will save the node modules in "E:\node_modules" folder.

Where are global node_modules stored?

Global modules are installed in the /usr/local/lib/node_modules project directory in the standard system, which is the system's root.

How do I use node modules globally installed?

To install a module from npm globally, you'll simply need to use the --global flag when running the install command to have the module install globally, rather than locally (to the current directory). Note: One caveat with global modules is that, by default, npm will install them to a system directory, not a local one.

How do I install all node modules for a project?

It's simple. If you want to install all the node_modules from the package. json file you simply put: npm install in terminal (on the same directory where the package. json exists) and it would install all the node modules in the folder called node_modules .


1 Answers

This behavior (making too many stat) has been removed (see PR) from io.js v2.3.1, it will be available in the next major version of node.js.

like image 89
Pierre Inglebert Avatar answered Nov 11 '22 21:11

Pierre Inglebert