`migration:generate` / `seed:generate` always emit `.js`, which breaks in `"type": "module"` projects — allow configuring the file extension (e.g. `.cjs`)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 76/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- javascript, node.js
- Lĩnh vực
- cli
Hướng nghiên cứu
Start with src/helpers/path-helper.js, then trace getMigrationPath and getSeederPath from src/commands/migration_generate.js and src/commands/seed_generate.js. Check how src/core/yargs.js receives .sequelizerc values and how src/core/migrator.js recognizes extensions. Done means generation accepts js, cjs, ts, and cts, defaults to js, rejects mjs, and produces files the existing loader can load.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
What you are doing?
In a project whose package.json has "type": "module" (common with TypeScript ESM setups), generating a migration and running it fails, because the generated file is CommonJS but has a .js extension, which Node treats as ESM in that scope.
// package.json
{ "type": "module" }
npx sequelize-cli migration:generate --name add-users-table
# -> migrations/20261001000000-add-users-table.js (content: module.exports = { up, down })
npx sequelize-cli db:migrate
What do you expect to happen?
A way to tell the CLI which extension to use for generated migrations and seeders, for example:
npx sequelize-cli migration:generate --name add-users-table --extension cjs
or once in .sequelizerc (whose keys are already passed to yargs as config, see src/core/yargs.js#L6-L17):
// .sequelizerc
module.exports = {
'migrations-path': 'migrations',
extension: 'cjs',
};
so the generated file is 20261001000000-add-users-table.cjs, which db:migrate already loads correctly.
What is actually happening?
The extension is hardcoded to js:
src/helpers/path-helper.js#L47-L49:getFileExtension()always returns'js', even thoughaddFileExtension(basename, options)already passes anoptionsargument to it.src/commands/migration_generate.js#L21andsrc/commands/seed_generate.js#L19build the path through it.- The templates are CommonJS:
src/assets/migrations/skeleton.js#L4,src/assets/seeders/skeleton.js#L4.
Migrations are then loaded with a plain require() by Umzug v2 (umzug/src/migration.js#L68). Under "type": "module" a .js file is ESM, so loading it fails:
# Node 22
module is not defined in ES module scope
# Node < 20.19 / < 22.12
ERR_REQUIRE_ESM
The loader side already supports .cjs: the migrator pattern is /^(?!.*\.d\.ts$).*\.(cjs|js|cts|ts)$/ (src/core/migrator.js#L49), added in #905. Only generation is missing.
Current workaround
We wrap the generate command in a small script that renames the output from .js to .cjs:
#!/usr/bin/env bash
# bin/generate-migration.sh — usage: bin/generate-migration.sh --name <name>
set -euo pipefail
MIGRATIONS_DIR="migrations"
before=$(ls "$MIGRATIONS_DIR")
npx sequelize-cli migration:generate "$@"
for f in "$MIGRATIONS_DIR"/*.js; do
name=$(basename "$f")
if ! grep -qxF "$name" <<< "$before"; then
mv "$f" "${f%.js}.cjs"
echo "Renamed $name -> ${name%.js}.cjs"
fi
done
This works, but every project using "type": "module" has to reinvent it, and it's easy to forget when someone runs sequelize-cli migration:generate directly. A built-in option would remove the need for it.
Proposed change
- Add an
--extensionoption tomigration:generateandseed:generate, settable from.sequelizerclike other options. - Accept only extensions the migrator actually loads (
js,cjs,ts,cts) via yargschoices, defaultjs, so behaviour is unchanged for existing users. - Reject
mjs, since the migrator pattern skips.mjsfiles silently and Umzug v2 loads withrequire(). - Pass the extension explicitly through
getMigrationPath/getSeederPath→getFileName→getFileExtension(extension)(returningextension || 'js'), rather than reading globalargsinsidegetFileExtension(). The same helper is also used bygetModelPath()(path-helper.js#L76) andinit(init-helper.js#L58), and the generatedmodels/index.jsonly loads.jsfiles (models/index.js#L25), so a global setting would silently break model loading. - Template content stays CommonJS, since every accepted extension is loaded through
require().
Related
- #905: added support for running
.cjsmigrations (this is the generation counterpart) - #1436: same root cause (hardcoded extension in
getFileExtension), asking for.tsgeneration. The proposed option would cover that request too. - #960: generated files not working in ESM projects; option to choose CJS vs ESM
- #987 / #990: ESM/CJS support for config and
.sequelizerc
Dialect: any
Database version: N/A
Sequelize CLI version: 6.6.5
Sequelize version: 6.37.7
Node version: 22.x
Would you resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Ngôn ngữ chính
- JavaScript
- Star
- 2.6k
- Fork
- 524
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của sequelize/cli
-
`sequelize init` writes JSON content into `.js` config file when using `.sequelizerc`Có thể đã có người làm @sanskarajput đã nhận 185 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
Tất cả issue của sequelize/cli
Issue tương tự
-
Engineering
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
techmatters/terraso-web-client#3095 ·
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Service process inherits the caller's cwd at first use, holding that folder open on Windows (EBUSY)Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
nextcloud/viewer#3424 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày