Search code examples
angularangular-clitypeorm

Angular-CLI + TypeORM: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module'


I try to add typeorm to a new angular-cli project, but as far as I add a reference to typeorm, I get an compiler-error.

./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

Code

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"]
}

The samples from TypeORM always use an older angular/typescript version. Do I need do upgrade? Thanks for your help!


Solution

  • The problem was fixed in the new TypeORM Version (0.2.44) und the build now works without build errors.

    Version: https://github.com/typeorm/typeorm/releases/tag/0.2.44