Cannot find module 'dotenv' error in Node.js [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
3 min

banner

# Cannot find module 'dotenv' error in Node.js [Solved]

To solve the error "Cannot find module 'dotenv'", make sure to install the dotenv package by opening your terminal in your project's root directory and running the following command: npm install dotenv and restart your IDE and development server.

cannot find module dotenv

Here is an example of how the error occurs.

index.js
// โ›”๏ธ Error: Cannot find module 'dotenv' // Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'dotenv' // imported from bobbyhadz-js/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);

Open your terminal in your project's root directory (where your package.json file is located) and run the following command:

shell
npm install dotenv

npm install dotenv

This will add the dotenv package to the dependencies of your project.

If the error is not resolved, try restarting your IDE and your development server.

# Delete your node_modules and reinstall your dependencies

If you still get the error, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

If you are on macOS or Linux, issue the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on Windows, issue the following commands in CMD.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and a reboot solves things sometimes.

# Creating a .env file in the root directory of your project

Now create a .env file in the root directory of your project.

.env
DB_PORT=1234 DB_USER=james_doe ENV=dev
The code for this article is available on GitHub
Make sure to add the .env file to your .gitignore, especially if you work on a public repository.

In your index.js file, before importing anything else, import and initialize the dotenv package.

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); // ๐Ÿ‘‰๏ธ "james_doe" console.log(process.env.ENV); // ๐Ÿ‘‰๏ธ "dev" console.log(process.env.DB_PORT); // ๐Ÿ‘‰๏ธ "1234"

initialize dotenv module using commonjs syntax

The code for this article is available on GitHub

If you use the ES6 modules import/export syntax, use the following import statement instead.

index.js
// โœ… Using ES6 modules import/export syntax import 'dotenv/config' console.log(process.env.DB_USER); // ๐Ÿ‘‰๏ธ "james_doe" console.log(process.env.ENV); // ๐Ÿ‘‰๏ธ "dev" console.log(process.env.DB_PORT); // ๐Ÿ‘‰๏ธ "1234"

initialize dotenv module using es6 import export syntax

The code for this article is available on GitHub
You must load and initialize the dotenv package as the first thing in your index.js file, especially if you have other files that need to access the environment variables.

If you import another file before initializing dotenv, you end up running the files before the properties are set on the process.env object.

Now restart your development server and you should see the properties on the process.env object print out the specified values.

Note that the DB_PORT property has a value of type string, even though we set it as a number in the .env file.

If the error persists, follow the instructions in my Cannot find module 'X' error in Node.js article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev