close

[Solved] Error: Cannot find module ‘dotenv’

While trying to run my project I am facing the following error: Error: Cannot find module ‘dotenv’. We are going to Learn about All Possible Solutions So Lets Get Start with This Article.

How Error: Cannot find module ‘dotenv’ Occurs?

While trying to run my project I am facing the following error.

Error: Cannot find module 'dotenv'

Or

Error [ERR_MODULE_NOT_FOUND]: Cannot find package ‘dotenv’

So here I am writing all the possible solutions that I have tried to resolve this error.

How To Solve Error: Cannot find module ‘dotenv’?

  1. How To Solve Error: Cannot find module ‘dotenv’?

    To Solve Error: Cannot find module ‘dotenv’ Before using dotenv You need to install dotenv. First of all, open Your terminal and run the following command: npm install dotenv@latest Then, create .env file at the root of your project directory. and Define some Variables: BASE_URL=’https://mysite.com’ Then in your index.js file initialize it just like below: require(‘dotenv’).config(); Now, You can access its variable Like process.env.BASE_URL. And Now Your error is solved. thank You.

  2. Error: Cannot find module ‘dotenv’

    To Solve Error: Cannot find module ‘dotenv’ Before using dotenv You need to install dotenv. First of all, open Your terminal and run the following command: npm install dotenv@latest Then, create .env file at the root of your project directory. and Define some Variables: BASE_URL=’https://mysite.com’ Then in your index.js file initialize it just like below: require(‘dotenv’).config(); Now, You can access its variable Like process.env.BASE_URL. And Now Your error is solved. thank You.

Solution 1: You need to install dotenv

Before using dotenv You need to install dotenv. First of all, open Your terminal and run the following command.

npm install dotenv@latest

Then, create .env file at the root of your project directory. and Define some Variables.

BASE_URL='https://mysite.com'

Then in your index.js file initialize it just like below.

require('dotenv').config();

Now, You can access its variable Like process.env.BASE_URL.

require('dotenv').config();

console.log(process.env.BASE_URL); // OUTPUT: https://mysite.com

And Now Your error is solved. thank You.

Conclusion

It’s all About this error. Hope We solved Your error. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also, Read

Leave a Comment