If you are a node Js developer you might have come across the error “Cannot find module ‘dotenv'”. This error occurs when the application is unable to find the dotenv package while trying to run the application. The ‘dotenv’ module helps to manage all the environment variables in your Node application.
In this article, we will discuss about the error and check different solutions to fix the error.
What is the “Cannot find module ‘dotenv'” error In Node?
The “Cannot find module ‘dotenv'” occurs when you try to run your Node application and it is unable to locate the dotenv
package in your project directory. The dotenv
module is important become it is used to manage all the environment variables like DB_key or API keys which are important to run an application smoothly.
Causes of the – Cannot find module ‘dotenv’ Error
Some of the common causes of this error are:
- The dotenv package is not installed in the project.
- Not imported correctly.
- The module is not listed in the package.json file.
- The .env file is not in the root directory.
- Cache issues
- Node JS installtion issue
Solution 1: Install the dotenv modue
The first thing you should do is check if the module is installed or not in your application. To check open your package.json
file and check under devDependencies object.
If the package is not listed, then install the module using this command:
npm install dotenv
This will install the dotenv
module locally in your node application.
If you want to install it globally use the -g
flag at the end, like this:
npm install dotenv -g
This command will install the package globally in your system. And now restart your local server and hopefully you won’t get the error.
Solution 2: Import dotenv in your code correctly
Sometimes even after installing the module correctly in your application, you might get the error because you have not correctly imported the module in your node project.
To add/import the file, add the following code in your main file (usually index.js or main.js) of your Node JS application.
require('dotenv').config()
The above line of code will load all the environment variables from the .env
file in your root directory.
Once imported, you can use the Environment Variables in your project using the proper syntax like this:
require('dotenv').config()
console.log(process.env.API_KEY)
console.log(process.env.DB_ID)
Note: Always make sure to load and initialize the dotenv
package first in your main JS file (example index.js or main.js) before using any variable in the .env
file.
Solution 3: Create .env
file in the root directory
Sometimes even after you have installed and imported the module in your project correctly, you might still get the “Cannot find module ‘dotenv'” error in your Node application. This is because you might not have created the .env
file in your root directory of the project.
So first create the .env
file in your project’s root directory with your required Environment Variables.
API_KEY = 123456789
DB_ID = test_database_id
And now import it in your main node Js file, like this:
require('dotenv').config()
console.log(process.env.API_KEY)
console.log(process.env.DB_ID)
If you have done all the above steps correctly, you won’t get the error anymore while running the application.
Solution 4: Delete entire node_modules and reinstall all the dependencies
If the above solutions didn’t work and you are still getting the error, then you should delete the entire node_modules
folder and the package-lock.json
or yarn.lock
file and then reinstall all the dependencies again.
This will fix all the dependency issues that are caused due to outdated npm packages or Node versions.
Delete the following files and folders only:
- node_modules folder
package-lock.json
oryarn.lock
file
Once deleted use the npm install
to reinstall the modules in the project. If you are using yarn, then just use the command yarn
to reinstall.
Important: Make sure you DO NOT delete the package.json file because it is needed to reinstall all the dependencies using
npm install
command.
Solution 5: Check for typos
Sometimes a simple typo in the import code can create this kinda error, so double-check your import code in your application code to avoid such errors.
Make sure you import the dotenv
module properly with the correct code as shown below:
require('dotenv').config()
And also make sure that the .env
file has a dot(.) in front of the name. Do not forget that or it will give you an error.
Once everything is perfectly set up and with no typos , restart your local server again and this time the “cannot find module dotenv/config” error will be resolved.
Conclusion:
The “Cannot find module ‘dotenv’” error is a very common error that we encounter as NodeJS developers while working with the Environment variables.
This error can occur due to various reasons however the most common ones are installation issues, incorrect import code, the problem with the file path, small typos, and corrupt node_modules files or Node js versions.
By following the solutions in this article you should be able to resolve the error in your Node application and start building your project without any issues.
FAQs
1. What is the dotenv module used for?
The dotenv module is used the manage environment variables in Node JS.
2. How do I install the dotenv module in Node Js?
To install dotenv
module in your node js application, use any of the following commands:
npm install dotenv
# to install it globally use
npm install -g dotenv
3. Why is the variables in the .env file not loading in my application?
To make sure that the environment variables are accessible and loading inside your application, it is important to load and initialize the dotenv package at the beginning of your main page (usually main.js or index.js)