What are node_modules in a project?

The node_modules directory is where Node.js installs packages that are listed as dependencies in your project’s package.json file. When you run the npm install command, npm installs the packages listed in the package.json file and their dependencies in the node_modules directory.

For example, if your package.json file looks like this:

{
  "name": "my-project",
  "dependencies": {
    "lodash": "^4.17.20",
    "express": "^4.17.1"
  }
}


Running npm install will install the lodash and express packages in the node_modules directory, along with any other dependencies that those packages have. The directory structure will look something like this:

node_modules/
├── express/
│   └── (express files)
└── lodash/
    └── (lodash files)


The node_modules directory is not meant to be modified directly.

Causes to remove node modules from a project?

There are several reasons you might want to remove the node_modules directory from a project:

  1. To free up disk space: The node_modules directory can take up a lot of space, especially if you have installed a lot of dependencies. Removing the directory can free up a significant amount of disk space.

  2. To resolve issues with dependencies: If you are having issues with one or more dependencies in your project, removing the node_modules directory and then running npm install to reinstall the dependencies can sometimes resolve the issue.

  3. To update dependencies: If you want to update the dependencies in your project to their latest versions, you can remove the node_modules directory and then run npm install to install the updated dependencies.

  4. To start fresh: If you want to completely reset your project and start fresh with a clean slate, removing the node_modules directory is one way to do that.

How to remove node_modules from a project?

There are several ways to remove the node_modules directory from a project:

  1. Using the command line: You can use the rm command to delete the node_modules directory. For example rm -rf node_modules.

  2. Using a file explorer: You can use a file explorer such as Finder on macOS or File Explorer on Windows to navigate to the project directory and delete the node_modules directory.

  3. Using a terminal emulator: If you are using a terminal emulator such as Terminal on macOS or PowerShell on Windows, you can navigate to the project directory and use a command such as rmdir /s node_modules to delete the directory.

  4. Using a code editor: Some code editors, such as Visual Studio Code, provide an option to delete files and directories directly from the editor. You can use this option to delete the node_modules directory.

It’s important to note that deleting the node_modules directory will remove all of the dependencies that are installed in the directory. If you want to keep some of the dependencies, you will need to move them out of the node_modules directory before deleting it.

Happy learning/coding 😎