Where does npm install packages? “Error: Cannot find module” even after installing the module
December 31, 2024
Contents
- “Error: Cannot find module” even after installing the module
- Where does node look for installed modules when specifying a relative path in the require() method
- Why does the error “Error: Cannot find module” occur when moving a .js file to another disk
- Where are the Node.js modules that I installed with npm
- How to check where the Node.js modules are located that I installed with npm
- Can the same package be installed globally and locally at the same time
- How to specify the path to a module in the require() method
- Official documentation on loading modules from files and folders
“Error: Cannot find module” even after installing the module
When importing a module using the require() method, you may encounter an error:
node:internal/modules/cjs/loader:1244 throw err; ^ Error: Cannot find module 'puppeteer' Require stack: - /mnt/disk_d/Share/Dev/suip/!files/test.js at Function._resolveFilename (node:internal/modules/cjs/loader:1241:15) at Function._load (node:internal/modules/cjs/loader:1066:27) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:220:24) at Module.require (node:internal/modules/cjs/loader:1327:12) at require (node:internal/modules/helpers:136:16) at Object.<anonymous> (/mnt/disk_d/Share/Dev/suip/!files/test.js:1:19) at Module._compile (node:internal/modules/cjs/loader:1566:14) at Object..js (node:internal/modules/cjs/loader:1718:10) at Module.load (node:internal/modules/cjs/loader:1305:32) { code: 'MODULE_NOT_FOUND', requireStack: [ '/mnt/disk_d/Share/Dev/suip/!files/test.js' ] }
The key information here is that the imported module was not found.
Obviously, you need to install a module before importing it. But you may receive this error message even after you have installed the module. The fact is that by default npm installs modules locally (and can install them both in the current working folder and in one of the parent folders).
In addition to local installation of modules, there is also a global variant for installing modules – it is enabled by adding the -g option to the command.
So, to fix this error, you need to specify the path to the module in the require() method correctly.
Where does node look for installed modules when specifying a relative path in the require() method
- By default, node looks for the node_modules folder in the current directory.
- If the node_modules folder is not found, it is searched for in the parent directory.
- If the node_modules folder is not found in the parent directory, it is searched for higher in the directory tree, up to the root of the disk.
Why does the error “Error: Cannot find module” occur when moving a .js file to another disk
You may have noticed that the code in the .js file is working, but if you move the file to another disk and run it, an error will occur due to the fact that the imported module was not found. This occurs because:
1) during successful launches, the required module was present in the local node_modules folder or in one of the node_modules folders in the parent directories.
2) during unsuccessful launches, the node_modules folder with the module is missing from all parent folders on the disk from which the .js file is launched.
Where are the Node.js modules that I installed with npm
When installing a module globally (with the -g option)
If you installed the module using the -g option, for example:
sudo npm i -g puppeteer
The location of the modules varies on different Linux distributions, for example, these can be directories:
/usr/lib/node_modules/ /usr/local/lib/node_modules/
When installing a module locally
If you installed the module without using the -g option, for example:
npm i puppeteer
The location of the module is selected based on the following rules:
1) The node_modules directory is searched for in the current folder. If the directory is found, the module is installed in it
2) If the node_modules directory is not found in the previous step, it is searched for in all parent folders, up to the root. If the directory is found, the module is installed in it
3) If the node_modules directory is not found in the previous two steps, then the node_modules directory is created in the current working folder and the module is installed in it
How to check where the Node.js modules are located that I installed with npm
Where are the Node.js modules installed globally with npm
To check where the modules folder is located, run the command:
npm root -g
To find out the location of the node_modules folder, run the command:
npm list -g | head -1
To get a list of modules installed globally, run:
npm list -g
If you want to get only a list of main modules (without submodules) installed globally, run the command:
npm list -g --depth=0
Where are the Node.js modules installed locally with npm
The node_modules folder can be located in any directory where you installed the modules. But usually such a folder is in the user's home directory:
~/node_modules/
The following command will search for the node_modules folder in the local directory, as well as in all parent directories, and will output the path to it if found:
npm root
Note that in different directories, and especially on different disks, the output of the previous command will differ.
To list modules installed locally, run the command:
npm list
To list only the main modules (without submodules) installed locally, run the command:
npm list --depth=0
Can the same package be installed globally and locally at the same time
The same package can be installed globally and locally. Moreover, it can be installed locally multiple times.
How to specify the path to a module in the require() method
The path to a module in the require() method can be specified as:
1) Full (absolute) path to the directory with the module
2) Module name – corresponds to the name of the folder where the module files are located
Examples of specifying an absolute path to a module (on different Linux distributions):
const puppeteer = require('/usr/lib/node_modules/puppeteer'); const puppeteer = require('/usr/local/lib/node_modules/puppeteer');
Example of specifying the name of a module folder:
const puppeteer = require('puppeteer');
When specifying a relative path, a search will be performed in the current folder where the .js script is running, as well as in the node_modules folders in parent directories.
Official documentation on loading modules from files and folders
In fact, the process of finding imported folders and files is more complicated than described above. You can find more details in the official documentation:
- https://nodejs.org/api/modules.html
- https://nodejs.org/api/modules.html#loading-from-node_modules-folders
- https://nodejs.org/api/modules.html#loading-from-the-global-folders
Related articles:
- Error “npm warn cli npm … does not support Node.js ...”. How to update npm and node on Arch Linux and BlackArch (SOLVED) (72.6%)
- Error “Authentication helper program /usr/lib64/squid/basic_ncsa_auth: (2) No such file or directory” (SOLVED) (50.6%)
- UEFI does not see installed Linux (SOLVED) (50.6%)
- What happens if an IPv4 client tries to access an IPv6-only server (SOLVED) (50.6%)
- “Failed - Network error” when exporting from phpMyAdmin (SOLVED) (50.6%)
- WordPress error “Another update is currently in progress” (SOLVED) (RANDOM - 28%)