The "Cannot find module 'glob'" error is a common error message that occurs in Node.js applications when the Node.js runtime fails to locate the 'glob' module. This error arises when your application tries to require or import the 'glob' module but Node.js cannot find it in the local node_modules directory of your project.
There are several common causes for this error:
- The 'glob' module is not installed: If you have not installed the 'glob' module in your project using npm (Node Package Manager) or Yarn, Node.js will not be able to find it. This is the most common cause of the error.
- Node.js is looking in the wrong place: Node.js may be looking in the wrong place for the 'glob' module. This can happen if the module was installed globally instead of locally, or if the NODE_PATH environment variable is misconfigured.
- The 'glob' module is not listed as a dependency in package.json: If 'glob' is not listed as a dependency in your project's
package.jsonfile, it may not be installed when you runnpm installoryarn install.
This error typically occurs when you are setting up a new Node.js project or environment, or when you are trying to run a project that was set up by someone else and the 'glob' module is missing from the environment. It can also occur when there are problems with your project's node_modules directory or package.json file.
Pre-requisites
This article assumes that your system already has nodejs and npm installed, this can be verified using:
node -v npm -v
Sample Output:
~]# node -v v16.18.1 ~]# npm -v 8.19.2
Scenario-1: The 'glob' module is not installed
If the 'glob' module is not installed, you can install it using npm (Node Package Manager) or Yarn.
If you're using npm, run the following command in your terminal:
npm install glob
If you're using Yarn, use this command instead:
yarn add glob
These commands will install the 'glob' module into your node_modules directory and add it as a dependency in your package.json file.
After running one of these commands, you should be able to import the 'glob' module in your Node.js files without encountering the "Cannot find module 'glob'" error.
Scenario-2: Node.js is looking in the wrong place
Problem Statement:
In Node.js, modules can be installed in two different ways: globally and locally.
- Global Installation: When a module is installed globally (using
npm install -g <module_name>), it is installed in a centralized location in the system and made available to all Node.js applications running on that system. This is often used for command line tools or other utilities that need to be accessible across multiple projects. - Local Installation: When a module is installed locally (using
npm install <module_name>), it is installed in thenode_modulesdirectory within the current project directory. This ensures that the specific version of the module is available to that particular project only.
When you try to require or import a module in your Node.js application, Node.js by default first looks in the local node_modules directory of the project. If it doesn't find the module there, it will give the "Cannot find module 'glob'" error, even if the module is installed globally.
The NODE_PATH is an environment variable in Node.js that can be used to specify additional directories to search for modules. If the NODE_PATH variable is misconfigured, it could lead Node.js to look in the wrong place for modules, causing the "Cannot find module 'glob'" error.
Solution:
If you suspect that Node.js is looking in the wrong place for the 'glob' module, there are a couple of things you can do:
- Install 'glob' Locally: The first and simplest solution is to make sure that the 'glob' module is installed locally in your project. You can do this by running
npm install globin your project's root directory. This will install 'glob' in the localnode_modulesdirectory and ensure that Node.js can find it. - Check the
NODE_PATHEnvironment Variable: If you're using theNODE_PATHenvironment variable, ensure that it's configured correctly. It should include the path to the globalnode_modulesdirectory if you're using globally installed modules, or it should be set to the localnode_modulesdirectory if you're using locally installed modules. However, the use ofNODE_PATHis discouraged and it's generally better to install modules locally when they're needed for specific projects.
cd <project_directory> npm install glob
Scenario-3: The 'glob' module is not listed as a dependency in package.json
Problem Statement:
In Node.js, the package.json file is a crucial part of any project. It lists the project's dependencies, that is, the other packages and libraries that the project needs to run properly. When you run npm install or yarn install without any other arguments, Node.js looks at the package.json file and installs the versions of the packages listed there.
If the 'glob' module (or any other module) is used in your project but is not listed in the package.json file, Node.js won't know to install it when you run npm install or yarn install. As a result, when you try to run your project, Node.js won't be able to find the 'glob' module and will throw the "Cannot find module 'glob'" error.
This scenario can often happen when you're setting up an existing project on a new system or environment. If you forget to list 'glob' (or any other module) in the package.json file, anyone who tries to set up the project will encounter the "Cannot find module 'glob'" error when they try to run it, even if they've run npm install or yarn install.
Solution:
The solution to this problem is straightforward: make sure that all the modules used in your project are listed as dependencies in your package.json file. If you've installed 'glob' but it's not listed in package.json, you can fix the issue by adding it manually or by reinstalling 'glob' with the --save flag.
To reinstall 'glob' and save it as a dependency, you can use the following command:
npm install --save glob
Or, if you're using Yarn:
yarn add glob
These commands will install 'glob' if it's not already installed, and they will add it to the dependencies list in package.json. This ensures that 'glob' will be installed automatically when you (or anyone else) runs npm install or yarn install in the future. After adding 'glob' to package.json, you should be able to import it in your Node.js files without encountering the "Cannot find module 'glob'" error.
Summary
In this comprehensive article, we explored the "Cannot find module 'glob'" error that Node.js developers often encounter. We discussed the reasons behind the error, which mainly include the 'glob' module not being installed, Node.js looking in the wrong place for the module, or the module not being listed as a dependency in the project's package.json file. We further delved into ways to reproduce the error for each scenario and provided step-by-step solutions to rectify it. We hope this guide will serve as a useful resource for developers experiencing this issue and aid them in creating robust and error-free applications.

