Skip to content

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

@mdodge-ecgrow

Description

@mdodge-ecgrow

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.

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions