2

The source code is :

const Sequelize = require('sequelize');
const sequelize = new Sequelize(process.env["DSN"]);

When trying to exec via node (I insist, I'm not trying to exec this code in my browser), I get :

$ node Sequelize.js
file:///Users/snwfdhmp/project/Sequelize.js:1
const Sequelize = require('sequelize');
                  ^

ReferenceError: require is not defined
    at file:///Users/snwfdhmp/project/Sequelize.js:1:19
    at ModuleJob.run (internal/modules/esm/module_job.js:138:23)
    at async Loader.import (internal/modules/esm/loader.js:178:24)

Why does that happen ? Every article on the internet says "Don't use require() in browsers", but I'm not using a browser.

I'm using node v14.4.0 on macOS 10.15.5.

0

1 Answer 1

2

If you defined "type": "module" in your package.json, you should remove it.

The reason is, certain CommonJS variables are not available in ES modules (reference), these are:

require
exports
module.exports
__filename
__dirname

However, require can be imported into an ES module using module.createRequire().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.