que-es-carpeta-node-modules-de-npm

What is the Node_modules Folder in NPM

  • 3 min

When working with Node.js projects that use NPM to manage dependencies, you will see a folder called node_modules appear.

At first glance, it may seem like a jumble of folders and files without meaning. But in reality, the node_modules folder is an essential component of the Node.js ecosystem.

In the node_modules folder, NPM stores all your project’s dependencies. When you write code that requires external packages, NPM searches for and uses the appropriate versions of those packages stored in this folder.

Every time you run the npm install command to install a package, NPM downloads the package and all its required dependencies from the NPM package registry and places them in this folder.

The structure of the node_modules folder can become very extensive and vary depending on the number of dependencies and their own dependencies. But fortunately, it’s not a folder you have to touch manually. NPM will do the work for you.

Structure of the node_modules folder

Each dependency is installed inside node_modules in its own folder, named after the package. Each subfolder can contain files and folders corresponding to the modules that the dependency needs to function.

Let’s look at a (very) simplified example of the structure the node_modules folder might have for a project with two dependencies: “express” and “lodash”.

📂 node_modules/ ├── 📂 express/ │ ├── 📄 index.js │ ├── 📂 lib/ │ │ ├── 📄 application.js │ │ ├── 📄 request.js │ │ └── 📄 response.js │ ├── 📂 node_modules/ │ │ ├── 📂 mime/ │ │ │ ├── 📄 index.js │ │ │ └── 📄 LICENSE │ │ ├── 📂 accepts/ │ │ │ └── 📄 index.js │ │ └── … │ └── … └── 📂 lodash/ ├── 📄 index.js ├── 📄 chain.js └── …

So, in this example, we have the node_modules folder with two subfolders: express and lodash. In turn, each of these subfolders contains the files and folders necessary for the dependencies to function correctly.

It is important that you do not manually modify files inside the node_modules folder, as NPM handles managing dependencies and their versions automatically.

If you need to make changes or customizations to a dependency, you can do so from your own project instead of modifying the node_modules folder directly.