Languages

Express.js - dotenv require error

0 points
Asked by:
Lily
548

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
548

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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