在 Node 14 中使用 sequelize-cli,类型为:module [英] Using sequelize-cli in Node 14 with type: module
问题描述
我正在尝试使用 Node v14.3
和 sequelize
设置项目.
I'm trying to set up the project using Node v14.3
and sequelize
.
我不想使用 babel-register
.而不是这个,我在 package.json
中设置了 "type": "module"
并使用所有 ES6 - ES11 开箱即用的特性.
I don't want to use babel-register
. Instead of this I set "type": "module"
in my package.json
and use all ES6 - ES11 features out of the box.
我还想使用 sequelize-cli
来设置和应用迁移.除以下命令外,一切正常:
I also want to use sequelize-cli
for setting up and applying migrations. Everything works except the following command:
& sequelize db:migrate
Sequelize CLI [Node: 14.3.0, CLI: 5.5.1, ORM: 5.21.11]
Loaded configuration file "config/config.json".
Using environment "development".
== 20200530214311-create-user: migrating =======
ERROR: Must use import to load ES Module: /home/kasheftin/work/tests/chai-http-publication/migrations/20200530214311-create-user.js
require() of ES modules is not supported.
require() of /home/kasheftin/work/tests/chai-http-publication/migrations/20200530214311-create-user.js from /home/kasheftin/.nvm/versions/node/v14.3.0/lib/node_modules/sequelize-cli/node_modules/umzug/lib/migration.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename 20200530214311-create-user.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/kasheftin/work/tests/chai-http-publication/package.json.
我们看到 sequelize-cli
在底层使用了 require()
.ES 模块不允许这样做.它提出了 3 种方法来解决这个问题:
We see that under the hood sequelize-cli
uses require()
. That's not allowed for ES Module. It suggests 3 ways to solve this:
将 20200530214311-create-user.js 重命名为以 .cjs 结尾 - 无法完成,sequelize-cli 找不到以 .cjs 结尾的迁移.
rename 20200530214311-create-user.js to end in .cjs - Can not be done, sequelize-cli does not find migrations that end with .cjs.
将需要的代码更改为使用 import() - 我不想接触 sequelize-cli 代码.
Change the requiring code to use import() - I don't want to touch sequelize-cli code.
删除 "type": "module" - 我不能,因为一切都停止了.
Remove "type": "module" - I can not because everything stops working.
还有其他方法可以使 sequelize-cli
工作吗?我正在大量使用测试,我希望在运行测试之前自动准备好测试数据库.
Is there any other way to make sequelize-cli
work? I'm using tests heavily and I want the test database to be prepared automatically before running tests.
推荐答案
解决方案是在你的迁移文件夹中添加一个 package.json
来覆盖 type: "module"
来自你的主 package.json
A solution is to add a package.json
in your migrations folder to override the type: "module"
from your main package.json
这个 package.json 看起来像这样:
This package.json would look like this:
{
"type": "commonjs"
}
您的迁移文件必须如下所示:
and your migration file have to look like this:
module.exports = {
up: async (queryInterface, Sequelize) => {
},
down: async (queryInterface, Sequelize) => {
}
};
它适用于 nodeJS 14.16.1
&续集6.6.2
&sequelize-cli 6.2.0
It works well with nodeJS 14.16.1
& Sequelize 6.6.2
& sequelize-cli 6.2.0
这篇关于在 Node 14 中使用 sequelize-cli,类型为:module的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!