[Fix] Error Cannot find module ‘dotenv’

I was recently writing some code in Node.js, when I got an unexpected error; ” Cannot find module ‘dotenv’ “. A snippet of the error is given below:

Cannot find module 'dotenv'

I would like to share the steps that helped me to fix the error; Error: Cannot find module ‘dotenv’

Why Error Cannot find module ‘dotenv’ is seen?

The error, Error: Cannot find module ‘dotenv’ is seen because most of the times dotenv is not installed properly in your project file. The error is also caused when there is no proper deployment of the dotenv

A simple solution to fix this would be to install the latest version of dotenv. If you still face the same error, please make sure to restart your IDE and development server. The problem is with VSCode as it often glitches and a reboot solves the problem.

A detailed solution to fix the Error: Cannot find module ‘dotenv’ is provided below:

How to fix Error Cannot find module ‘dotenv’?

To fix the error; Error: Cannot find module ‘dotenv’, you will have to install the dotenv package in the terminal of your project’s root directory and run the command ‘ npm install dotenv’ shown below :

 npm install dotenv

After running the code please make sure to restart your IDE and development server.

Here is an example of how the error occurs in your index.js.


require('dotenv').config();

//  if you use ES6 you only need this line to import
// import 'dotenv/config'

console.log(process.env.DB_USER);
console.log(process.env.ENV);
console.log(process.env.DB_PORT);

For the next step open the terminal in your project’s root directory or the directory where your package.json file is located and run the command shown below

npm install dotenv

This step is going to add the dotenv package into the dependencies in your project.

The above steps should fix the error for most cases.If even after the above steps the error is not resolved; try to restart your IDE and your development server.

The next step would be to try to delete your node_modules and package-lock.json; please do not delete the package.json file. You will have to re-run npm install and restart your IDE and development server. Follow the code given below:

#  delete node_modules and package-lock.json
rm -rf node_modules
rm -f package-lock.json

# clean npm cache
npm cache clean --force

npm install

npm install dotenv@latest

After these steps please make sure to restart your IDE and development server if you still face the same error. The problem is with VSCode as it often glitches and a reboot solves the problem.

Now to launch the dotenv you will have to create a  new.env file in your root directory. You will create a new .env file by following the code mentioned below:

DB_PORT=1234
DB_USER=james_doe
ENV=dev

Do not forget to add the .env file to your, .gitignore, This is even more important if you are working on a public repository.

Before you import anything else, import and initialize the dotenv package In your index.js file by following the below-mentioned code.

require('dotenv').config();

// if you use ES6 you only need this line to import
// import 'dotenv/config'

console.log(process.env.DB_USER); 
console.log(process.env.ENV); 
console.log(process.env.DB_PORT); 

Your first step should be to load and initialize the dotenv package as the most prioritized thing in your index.js file, especially if you have other files which require access to the environment variables.

Now the final step would be to restart your development server and then you would be able see the properties on the process.env object.

Conclusion

To fix the Error: Cannot find module ‘dotenv’ error; use sass instead of node-sass; you would have to install the latest version of dotenv. If you still face the same error, please make sure to restart your IDE and development server.

The problem is with VSCode as it often glitches and a reboot solves the problem. The error is also caused when there is no proper deployment of dotenv. Check the above mentioned steps to know how to deploy dotenv properly.