When you call require('lodash') or import 'lodash', Node.js looks for a node_modules folder in the current file's directory, then in each parent directory up to the file system root, and stops at the first match. This walk up the tree is what node_modules resolution means, and it is how packages are found without any path configuration.
The details differ slightly between CommonJS and ES modules, and package.json fields such as main and exports decide which file inside the package is loaded.
For a bare specifier (one that does not start with ./, ../ or /), Node.js builds a list of candidate directories. If the importing file is /home/app/src/api/user.js, the search order is:
The first directory that contains a matching package wins. Nearer copies shadow farther ones, which is how two versions of the same package can coexist in one project.
You can see the list yourself:
module.paths contains exactly the candidate directories for that file. To see which file a specifier resolves to, use require.resolve():
Once Node.js finds node_modules/pkg, it must pick a file inside it. For CommonJS require('pkg') the rules are:
package.json has an exports field, use it (this takes priority).main field.main, try index.js, then index.json, then index.node.For a path such as require('pkg/utils'), Node.js tries the exact file, then adds extensions (.js, .json, .node), then treats it as a directory. With an exports field, deep paths are only allowed if they are listed.
Modern packages use exports to define a public entry surface:
With this file, import 'demo-lib' loads index.mjs, require('demo-lib') loads index.cjs, and demo-lib/utils is allowed. Any other deep path, such as demo-lib/dist/secret.js, throws ERR_PACKAGE_PATH_NOT_EXPORTED. Conditions such as import, require, node and default let one package serve both module systems.
ES module resolution follows the same node_modules walk for bare specifiers, but with stricter rules for relative paths:
import './util.js', not import './util'.index.js automatically.import.meta.resolve('pkg') to get the resolved URL.NODE_PATH environment variable is ignored.Before searching node_modules, Node.js checks whether the specifier is a built-in module. require('fs') and require('node:fs') both return the core module even if a package called fs is installed. The node: prefix makes the intent explicit and is recommended.
For CommonJS only, extra search folders can be added with NODE_PATH, and Node.js also checks $HOME/.node_modules, $HOME/.node_libraries and $PREFIX/lib/node as a last resort. These legacy locations are discouraged; keep dependencies in the project's node_modules.
| Symptom | Likely cause | Fix |
|---|---|---|
Cannot find module 'x' | Package not installed in any parent node_modules | Run npm install x |
ERR_PACKAGE_PATH_NOT_EXPORTED | Deep import not listed in exports | Import a documented entry point |
| Two versions of a package loaded | Nested node_modules shadows the top-level copy | Run npm ls x, then npm dedupe |
ERR_MODULE_NOT_FOUND for a relative import | Missing file extension in ESM | Add .js |
| Wrong file loaded | Another copy found earlier in the walk | Print require.resolve('x') |
A fast check is node -e "console.log(require.resolve('x'))" from the failing file's folder, and npm ls x to see every installed copy.
Yes. It starts in the importing file's folder and walks up through every parent directory until it finds the package or reaches the root.
Packages installed with npm install -g are mainly for command-line tools. Node.js does not search the global folder for require() by default, so install libraries locally.
main names one entry file. exports can define several entry points, choose files by condition (import or require), and block deep imports. When both exist, exports wins in current Node.js versions.
The node_modules walk is relative to the importing file. A package installed in a sibling or child project is not on the path of a different project.
Node.js resolves bare specifiers by checking node_modules in the current directory and then every parent, picking the first match. Inside the package, exports or main selects the file. ES modules add stricter rules: explicit extensions and no directory imports. Use require.resolve(), import.meta.resolve() and npm ls to see what is actually loaded.