Last updated: Apr 5, 2024
Reading timeยท3 min
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.
Here is an example of how the error occurs.
// โ๏ธ 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:
npm install dotenv
dotenv
package to the dependencies of your project.If the error is not resolved, try restarting your IDE and your development server.
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
.
# 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.
# 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.
.env
file in the root directory of your projectNow create a .env
file in the root directory of your project.
DB_PORT=1234 DB_USER=james_doe ENV=dev
.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.
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"
If you use the ES6 modules import/export syntax, use the following import statement instead.
// โ 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"
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.
You can learn more about the related topics by checking out the following tutorials: