Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does npm look for a package.json file?

Tags:

node.js

I know npm looks for a package.json in the current directory, but it also appears to find a package.json several directories above my current directory. How does this work? Does npm look at parent directories until it finds a package.json?

If so, does require() also search for a package.json when looking for modules?

Is this behavior part of npm or is it part of CommonJS?

like image 847
royco Avatar asked Feb 18 '12 21:02

royco


1 Answers

npm doesn't really do much work w/ package.json except for all the heavy lifting of grabbing/resolving dependencies and putting them in the proper directory structure. To the bet of my knowledge, npm does not inject its self a runtime dependency.

Node's require() performs the package.json resolution/parsing at runtime, as well as interaction with the node_modules directory.

Here's some pointers into the code that does that:

  • package.json resolution
  • node_modules resolution
  • the meat of package resolution
like image 148
Nevir Avatar answered Oct 06 '22 06:10

Nevir