无法将数据库连接到 Typescript ES 模块

问题描述 投票:0回答:1

我正在尝试使用 TypeOrm 库以便从我的 Typescipt 代码连接到数据库。每当我将这个库与 CommonJs 代码一起使用时,它都工作得很好。但是当我尝试通过运行来设置 ESM 项目时

npx typeorm init --name MyProject --database postgres --module esm 

命令,稍后尝试通过运行连接到数据库

npm start

它给了我以下错误:

npm start

> [email protected] start
> node --loader ts-node/esm src/index.ts

(node:12236) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Error [ERR_LOADER_CHAIN_INCOMPLETE]: "ts-node/esm 'resolve'" did not call the next hook in its chain and did not explicitly signal a short circuit. If this is intentional, include `shortCircuit: true` in the hook's return.
    at new NodeError (node:internal/errors:405:5)
    at ESMLoader.resolve (node:internal/modules/esm/loader:843:13)
    at async ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:7) {
  code: 'ERR_LOADER_CHAIN_INCOMPLETE'
}

如何解决这个错误?还有其他经过验证的方法可以从 ES 模块连接到数据库(mysql、postgres 等)吗?有没有一种方法可以在模块类型的项目中创建一个文件,在 CommonJs 类型的项目中创建另一个文件,并使用后者来将第一个文件提供的数据上传到数据库并从数据库中读取?

我需要 ES 模块的顶级等待。因此,如果有一种方法可以制作文件 CommonJs 并且仍然可以访问顶级等待,我对此持开放态度。

我尝试更改数据库(mysql 到 postgres)。这没有帮助。

typescript typeorm es6-modules
1个回答
0
投票

尝试

npm install ts-node@latest

然后使用

npm run start

再次启动您的项目
© www.soinside.com 2019 - 2024. All rights reserved.