Error: [ERR_REQUIRE_ESM] in Typescript Sequelize

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 and module properties to many different combinations.
  • Tried removing the module property.
  • Tried, as I said, setting "type": "module" in package.json.
  • Tried placing a package.json inside the config 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.

New contributor

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật