Angular-CLI + TypeORM:模块解析失败:'import' 和 'export' 可能仅与 'sourceType: module' 一起出现
Angular-CLI + TypeORM: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module'
我尝试将 typeorm 添加到新的 angular-cli 项目,但就我添加对 typeorm 的引用而言,我遇到了编译器错误。
./node_modules/typeorm/browser/index.js:3:0 - Error: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (3:0)
File was processed with these loaders:
* ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js
* ./node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| /*!
| */
> import "reflect-metadata";
| // -------------------------------------------------------------------------
| // Commonly Used exports
代码
import { Component, OnInit } from '@angular/core';
// import { createConnection, Connection } from 'typeorm';
import { createConnection, Connection } from 'typeorm/browser';
import { Message } from '../../entities/message';
@Component({...})
export class TestPageComponent {
typeOrmConnection?: Connection;
async connect() {
try {
...
package.json
{
...
"devDependencies": {
"@angular/cli": "~13.2.2",
"typescript": "~4.5.5",
...
},
"dependencies": {
"@angular/common": "~13.2.1",
"typeorm": "^0.2.43",
...
}
}
tsconfig.base.json
{
"compilerOptions": {
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "commonjs",
"lib": ["es2017", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@asello/database-test": ["libs/database-test/src/index.ts"],
"@asello/design-system": ["libs/design-system/src/index.ts"],
"@asello/ui-core": ["libs/ui-core/src/index.ts"]
},
},
"exclude": ["node_modules", "tmp"]
}
TypeORM 中的示例始终使用较旧的 angular/typescript 版本。我需要升级吗?感谢您的帮助!
此问题已在新的 TypeORM 版本 (0.2.44) 中得到修复,并且构建现在可以正常运行而不会出现构建错误。
我尝试将 typeorm 添加到新的 angular-cli 项目,但就我添加对 typeorm 的引用而言,我遇到了编译器错误。
./node_modules/typeorm/browser/index.js:3:0 - Error: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (3:0)
File was processed with these loaders:
* ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js
* ./node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| /*!
| */
> import "reflect-metadata";
| // -------------------------------------------------------------------------
| // Commonly Used exports
代码
import { Component, OnInit } from '@angular/core';
// import { createConnection, Connection } from 'typeorm';
import { createConnection, Connection } from 'typeorm/browser';
import { Message } from '../../entities/message';
@Component({...})
export class TestPageComponent {
typeOrmConnection?: Connection;
async connect() {
try {
...
package.json
{
...
"devDependencies": {
"@angular/cli": "~13.2.2",
"typescript": "~4.5.5",
...
},
"dependencies": {
"@angular/common": "~13.2.1",
"typeorm": "^0.2.43",
...
}
}
tsconfig.base.json
{
"compilerOptions": {
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "commonjs",
"lib": ["es2017", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@asello/database-test": ["libs/database-test/src/index.ts"],
"@asello/design-system": ["libs/design-system/src/index.ts"],
"@asello/ui-core": ["libs/ui-core/src/index.ts"]
},
},
"exclude": ["node_modules", "tmp"]
}
TypeORM 中的示例始终使用较旧的 angular/typescript 版本。我需要升级吗?感谢您的帮助!
此问题已在新的 TypeORM 版本 (0.2.44) 中得到修复,并且构建现在可以正常运行而不会出现构建错误。