10

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?

1 Answer 1

2

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Am I missing something? How does this answer the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.