question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error reading config.js, must use import to load ES Module

See original GitHub issue

Issue Creation Checklist

[x] I have read the contribution guidelines

Bug Description

When running the migration from the CLI (‘npx sequelize-cli db:migrate’), I am getting the following error:

ERROR: Error reading "config\config.js". Error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\...\config\config.js
require() of ES modules is not supported.
require() of C:\...\config\config.js from C:\Users\...\AppData\Roaming\npm-cache\_npx\13936\node_modules\sequelize-cli\lib\helpers\config-helper.js is an ES module file as it is a .js file whose nearest pare
nt package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\...\package.json.

I’ve tried renaming it to a .cjs file and I get even more errors. I can’t remove the “type”:“module” as I have my project fully set up to use that. So I would like to figure out the third option. “change the requiring code to use import()”

SSCCE

Here is the link to the SSCCE for this issue: LINK-HERE

This is my config.js file:

// You can delete this code block if you have included a link to your SSCCE above!

// import dotenv from 'dotenv';
// dotenv.config();

import 'dotenv/config.js';

const username = process.env.NAME;
const password = process.env.PASSWORD;
const database = process.env.DATABASE;
const host = process.env.HOST;
const port = process.env.DB_PORT;
const dialect = process.env.DIALECT;
const node_env = process.env.NODE_ENV;
const session_secret = process.env.SESSION_SECRET;
const base_url = process.env.BASE_URL;
const client_url = process.env.CLIENT_APP_LOC;
const secure_cookie = process.env.SECURE_COOKIE;

const config = {
	dev: {
		username,
		password,
		database,
		host,
		port,
		dialect,
		logging: true,
		session_secret,
		base_url,
		client_url,
		secure_cookie,
	},
	testing: {
		username,
		password,
		database,
		host,
		port,
		dialect,
		logging: true,
		session_secret,
		base_url,
		client_url,
		secure_cookie,
	},
	production: {
		username,
		password,
		database,
		host,
		port,
		dialect,
		logging: true,
		session_secret,
		base_url,
		client_url,
		secure_cookie,
	},
};

export default config[node_env];

What do you expect to happen?

I wanted it to correctly implement my migration and add in the new field.

What is actually happening?

I am getting the above error instead.

Output here

Additional context

Add any other context and details here.

Environment

  • Sequelize version: 6.6.2
  • Node.js version: 14.17.0
  • If TypeScript related: TypeScript version: XXX

Bug Report Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don’t know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mariusacommented, Nov 4, 2021

@mattiamalonni @sdepold 7 days to close an issue is extremely aggressive, common… Especially as maintainers don’t even look at these valid requests.

0reactions
ephyscommented, Apr 16, 2022

I believe this was fixed by https://github.com/sequelize/cli/pull/987

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error [ERR_REQUIRE_ESM]: Must use import to load ES ...
I just installed the library onto my TypeScript project and I get the following error: Error [ERR_REQUIRE_ESM]: Must use import to load ES ......
Read more >
Importing in Node.js: error "Must use import to load ES Module"
You are using CommonJS right now with the .js extension. To import ES6 modules, you will have to change the extension of the...
Read more >
Must use import to load ES Module - Netlify Support Forums
Hello, I have been getting this error on production, but it works fine in my local when I try to preview using netlify...
Read more >
JavaScript : ESlint - Error: Must use import to load ES Module
JavaScript : ESlint - Error : Must use import to load ES Module [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] JavaScript...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Absolute specifiers like 'file:///opt/nodejs/config.js' . ... Instead, use import() to load an ES module from a CommonJS module.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found