When I try running any command in Sequelize, inside or outside the docker container, that needs the configuration file (In my case, config.mjs
), I get this error:
ERROR: Error reading "build/database/config/config.mjs". Error: Error [ERR_REQUIRE_ESM]: require() of ES Module /backend/build/database/config/config.mjs not supported. Instead change the require of /backend/build/database/config/config.mjs to a dynamic import() which is available in all CommonJS modules.
Sometimes, it mentions Instead change the require of config.js in /backend/node_modules/sequelize-cli/lib/helpers/config-helper.js
, which points to the lib itself.
I tried changing config.mjs
to CommonJs using require
instead of import
, but that creates a cascade of files not being able to import it and thus having to be converted to CommonJS, such as the index.js
in the models folder, that would give me this error:
File '/home/giovani/Projetos/GiovaniJogos/backend/src/database/config/config.ts' is not a module.
All these files are already transpiled from Typescript, in the build
folder.
I’m using .mjs
instead of .js
because config.js
was not being treated as a module. I used it instead of setting "type": "module"
in package.json
to see if it solved the problem, it didn’t.
This is my config.mts
(before transpilation):
import { type Options } from 'sequelize'
const config: Options = {
username: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
host: process.env.MYSQL_HOST,
port: Number(process.env.MYSQL_PORT),
dialect: 'mysql'
}
module.exports = config
This is my index.ts
:
import { Sequelize } from 'sequelize'
import * as config from '../config/config.mjs'
export default new Sequelize(config)
This is my tsconfig.json
:
{ "compilerOptions": { "target": "ES6", "module": "ES6", "sourceMap": true, "outDir": "./build", "rootDir": "./src", "strict": true, "moduleResolution": "node", "baseUrl": ".", "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "experimentalDecorators": true, "emitDecoratorMetadata": true, "strictPropertyInitialization": false, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "skipLibCheck": true, "paths": { "*": ["node_modules/*"] } }, "include": ["src/**/*"], }
These are my packages versions:
"sequelize-cli": "^6.3.0",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^5.3.3",
"sequelize": "^6.9.0"
"mysql2": "^3.0.0"
Node: 20.12.2
- Tried changing
target
andmodule
properties to many different combinations. - Tried removing the
module
property. - Tried, as I said, setting
"type": "module"
inpackage.json
. - Tried placing a
package.json
inside theconfig
folder with just{"type": "commonjs"}
, as I saw suggested in some posts. - Tried the instructions in this post, which makes the same cascade I mentioned before.
I always get the [ERR_REQUIRE_ESM]
error or the SyntaxError: Cannot use import statement outside a module
if I don’t convert it to a module with the .mts
extension or the package.json
configuration.
I’m perplexed since this is a very basic configuration and worked just fine, with these EXACT config.ts
and index.ts
files, in 3 tutorials I read.
Giovani Souza Kill StevePlays is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.