我正在将我的 NestJS-TypeOrm 应用程序迁移到 monorepo(NX 工作区)。
每当我尝试运行该应用程序时,都会收到此错误:
C:\myproject\node_modules\@nrwl\node\src\executors\node\node-with-require-overrides.js:16
return originalLoader.apply(this, arguments);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\myproject\node_modules\@angular\core\fesm2015\core.mjs not supported.
Instead change the require of C:\myproject\node_modules\@angular\core\fesm2015\core.mjs to a dynamic import() which is available in all CommonJS modules.
at Function.Module._load (C:\myproject\node_modules\@nrwl\node\src\executors\node\node-with-require-overrides.js:16:31)
at ...
我调试了几个小时,并将问题跟踪到将我的实体“导入”到 AppModule 中的 TypeOrm:
import {Foo} from './foo.entity.ts';
@Module({
imports: [
TypeOrmModule.forRoot({...myConfig, entities: [Foo]}),
/*...*/
})
export class AppModule {/*...*/}
但也使用
forFeature()
会导致相同的错误:
import {Foo} from './foo.entity.ts';
@Module({
imports: [TypeOrmModule.forFeature([Foo])],
/*...*/
})
export class MyFeatureModule {/*...*/}
问题似乎可能是由 nx/webpack 创建包含所有代码的单个 main.js 文件引起的,而在 dist 文件夹之前单独包含所有代码文件。
人们建议在我的 tsconfig 中使用
"module": "commonjs"
,或在 package.json 中使用 "type": "module"
,但这没有任何作用:(
任何解决方案的想法都受到高度赞赏🙏
原来我是个白痴。 我的共享库包含一些角度代码,并且 TypeOrm 实体使用该库中的一些枚举。由于 webpack 将整个库编译在一个文件中,包括未使用的 Angular 代码,因此它会尝试
require()
一个未使用的 Angular 核心模块,这显然会失败......
结论:确保将共享接口/枚举提取到专用的 TS/JS 库中,而不是将其放入 Angular 库中。