Languages

Express.js - dotenv require error

0 points
Asked by:
Lily
208

When I try to import dotenv using:

require('dotenv').config()

I get the following error:

file:///C:/some_path/index.js:9
require('dotenv').config();
^

ReferenceError: require is not defined
    at file:///C:/some_path

Can someone help me to fix this?

1 answer
0 points
Answered by:
Lily
208

There are two solutions for this problem:

1. Remove "type": "module" from the package.json file

2. If you installed esm (es modules) and you are using import keyword instead of require to import packages use the following commands:

  1. Import dotenv
    import dotenv from 'dotenv';
  2. Then use:
    dotenv.config();
0 comments Add comment
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join