How to resolve 'node_modules directory missing or not created' issue when using npm install command
1. Purpose
In this post, I will demonstrate how to resolve the node_modules
directory not being created issue when using the npm install
command.
Suppose we have created a new directory named nodejs1
:
Then, we call the npm install
command to install a module, such as express
:
After executing the command, we list the directory including hidden files:
Where is the node_modules
directory? It should be created automatically after executing the command npm install xxx
, but it’s missing in the current working directory! Where is it?
2. Environment
- Node.js v12.14.0
- npm 7.11.2
3. The solution and reason
3.1 The solution
Suppose your home directory is ~/
. npm has already created the node_modules
directory in your home path, e.g., ~/node_modules
.
You can see that our modules are installed in that directory by default. What if we want to install them into the current directory?
We can do the following to create the node_modules
directory in our current working directory:
Now, after executing the npm init
command, let’s see the results:
The package.json
content is:
Then, we can install the package again:
You can see that the package.json
and node_modules
are both created correctly now in the current working directory.
We can check the content of package.json
again:
You can see that our package express
is added into the dependencies of package.json
.
3.2 The reason
According to the npm official document, the npm init
is:
npm init <initializer>
can be used to set up a new or existing npm package.
initializer
in this case is an npm package namedcreate-<initializer>
, which will be installed bynpm-exec
, and then have its main bin executed — presumably creating or updatingpackage.json
and running any other initialization-related operations.
The npm init
command would try to initialize the package.json
in the current directory. According to this article, the npm install xxx
is:
npm install
<folder>
:Install the package in the directory as a symlink in the current project. Its dependencies will be installed before it’s linked. If
<folder>
sits inside the root of your project, its dependencies may be hoisted to the top-level node_modules as they would for other types of dependencies.
That is to say, npm install
would try to install the dependency in the top-level node_modules
directory and then link the files to your working directory.
When you require
a package, say bar.js
, in your code, Node.js would try to find the dependency in the following order:
The key point is:
- Read the dependencies of the nearest
node_modules
first. - Recursively look up
node_modules
dependencies.
4. Summary
In this post, I demonstrated how to resolve the node_modules
initialization problem when trying to create it using the npm install xxx
command. The key point is that you should first call npm init
to create the package.json
file, and then call npm install xxx
to install the dependency. Node.js will only create the node_modules
directory in the current working directory if a package.json
file exists.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!