[FIXED] NPM install doesn't create node_modules directory
Troubleshooting Guide for Fixing when npm install Doesn't Create node_modules Directory
Jun 21, 2023 | Read time 7 minutes🔔 Table of contents
Introduction
When I was working on one of my side projects, I noticed that my node_modules folder was not right.
When I ran npm install
on a fresh project, the node_modules folder was nowhere to be found!
This also means the npm install
did not create the packages I wanted in the node_modules directory.
So after a bit of scratching my head, I decided to investigate a bit further.
Additionally, I got this lovely error as well:
npm ERR! install Couldn't read dependencies
npm ERR! Darwin 15.2.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "."
npm ERR! node v4.4.6
npm ERR! npm v2.15.5
npm ERR! code EISDIR
npm ERR! errno -21
npm ERR! syscall read
npm ERR! eisdir EISDIR: illegal operation on a directory, read
npm ERR! eisdir This is most likely not a problem with npm itself
npm ERR! eisdir and is related to npm not being able to find a package.json in
npm ERR! eisdir a package you are trying to install.
This post will go over my steps to troubleshoot this issue, and give you some solutions to fix this.
Now if npm install
did not create the node_modules directory, it could be due to several reasons. Here are some steps you can take to troubleshoot and fix the issue:
Verify your package.json file
The most likely culprit of this problem is that the package.json is out of whack.
At the basic step, make sure that you are running the npm install
command in the directory that contains the package.json file.
Now if everything goes well, the node_modules directory is created and the dependency files will also be created based on the library versions listed in package.json.
Now if you do not have a package.json file in your project - you can create one using the npm init
command.
Open your terminal or command prompt, navigate to the desired project directory, and run the following command:
npm init
Follow the prompts to provide the necessary information such as project name, version, description, and other details. Press Enter to accept the default values or provide your own.
Install packages with proper command: Once you have a package.json file in place, you can install packages by using the npm install command followed by the package name. For example, if you want to install the Express package, run the following command:
npm install express --save
The –save option will update your package.json file, adding the Express package as a dependency along with its version.
Verify node_modules directory: After executing the npm install command, check if the node_modules directory has been created in your project folder. It should now contain the installed packages.
By ensuring the presence of a proper package.json file and using the correct npm install command with the –save option, you should be able to resolve the issue and have the node_modules directory generated successfully.
Clearing NPM Cache
Clear npm cache: Sometimes, issues with the npm cache can prevent the creation of the node_modules directory. Clear the npm cache by running the following command:
npm cache clean --force
Latest version of NPM
Update npm: Outdated versions of npm may have bugs or compatibility issues. Update npm to the latest version using the command:
npm install -g npm@latest
Verify npm configuration: Check if the npm configuration is set to install packages locally. Run the command:
npm config get prefix
It should return a path pointing to a local directory rather than a system-wide location like /usr/local.
Use a specific version of Node.js: In some cases, there may be compatibility issues between Node.js and npm. Try switching to a different version of Node.js to see if it resolves the problem. You can use a Node version manager like nvm (Node Version Manager) to install and manage multiple versions of Node.js.
Manually create the directory
Manually create the directory: If none of the above steps work, you can create the node_modules directory manually in your project folder. Open your terminal or command prompt, navigate to your project directory, and run the following command:
mkdir node_modules
Install dependencies individually: Instead of running npm install to install all dependencies at once, you can try installing them one by one. Update your package.json file by removing the dependencies section or only keeping a few dependencies. Then, run npm install to install those specific packages. Repeat this process for each dependency.
Check .NPMRC file
The problem could be related to the configuration of the .npmrc file. Here’s a step-by-step solution based on the given information:
Locate the .npmrc file: In your project directory or the root directory of your system, search for the .npmrc file. It is a configuration file for npm.
Open the .npmrc file: Use a text editor to open the .npmrc file and examine its contents.
Verify the registry URL: Look for a line in the file that specifies the registry URL. It should resemble the following:
registry=https://registry.npmjs.org/
Ensure the URL is correct: Confirm that the registry URL is set to https://registry.npmjs.org/. If it points to a different URL or is missing altogether, update the URL to the correct value.
Save the changes: After updating the .npmrc file, save the changes and close the file.
Run npm install again: Open your terminal or command prompt, navigate to your project directory, and execute the npm install command once more.
By verifying and correcting the registry URL in the .npmrc file, you should be able to resolve the issue and have the node_modules directory created correctly during the npm install process.
Summary
Overall to fix issues when NPM install doesn’t create node_modules directory is a multi step process. My problem could be widely different to you, but there are a few things we can try to resolve this.
The most likely culprit is that the package.json is not right. So we need to validate and check if the basic structure is correct JSON and that you have the right package versions.
Next options we can do is to clear NPM cache, update your NPM and Node versions, check your permissions and manually create the node_modules folder.
Additionally we can check that the .NPMRC file is correct.