如何让heroku获得模块支持



我有一个使用ES模块进行knex迁移的express服务器。我所有的文件扩展名都是.js

我可以成功运行迁移:yarn run knex migrate:latest --env development

当我尝试使用heroku-cli:heroku run knex migrate:latest运行迁移时,我会得到以下错误:

require() of ES Module /app/knexfile.js from /app/node_modules/knex/lib/migrations/util/import-file.js not supported.
Instead change the require of knexfile.js in /app/node_modules/knex/lib/migrations/util/import-file.js to a dynamic import() which is available in all CommonJS modules.
Error [ERR_REQUIRE_ESM]: require() of ES Module /app/knexfile.js from /app/node_modules/knex/lib/migrations/util/import-file.js not supported.

我的yarn.lock文件已签入并且是最新的。我的package.json文件指定了请求的版本:

"engines": {
"node": "16.x",
"npm": "8.x",
"yarn": "1.x"
},
"type": "module",

如何让heroku获得模块支持?

Knex0.95.x检查process.env.npm_package_type==='module'以确定导入的文件是CommonJs模块还是ES模块。

该变量不会在具有指定版本的yarn的heroku-nodejs构建包环境中设置。我将此添加到heroku配置变量中以解决此问题。

最新更新