最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript & TypeORM: Cannot use import statement outside a module - Stack Overflow

programmeradmin2浏览0评论

I've set up a Typescript project with TypeORM and I am facing some issues with the pilation.

My package structure is like this:

root
├── db
│   ├── migrations
│   │   ├── a_migration.ts
│   ├── connection
│   │   ├── config.ts <- ormconfig
│   │   ├── encrypt.ts
│   │   ├── index.ts <- creates the connection
├── src
│   ├── card
│   │   ├── entity.ts
├── package.json
├── tsconfig.json

My config.ts is:


export = {
  type: 'postgres',
  host: POSTGRES_HOST,
  port: POSTGRES_PORT,
  username: POSTGRES_USER,
  password: POSTGRES_PASS,
  database: POSTGRES_DB,
  synchronize: true,
  logging: false,
  entities: ['**src/**/*entity.{ts,js}'],
  migrations: ['**/migrations/*.{ts,js}'],
  cli: {
    entitiesDir: 'src/entity',
    migrationsDir: 'db/migrations',
  },
  namingStrategy: new SnakeNamingStrategy(),
};

tsconfig.json:

{
  "pilerOptions": {
    "baseUrl": ".",
    "target": "es6",
    "module": "CommonJS",
    "allowJs": false,
    "esModuleInterop": true,
    "noImplicitAny": true,
    "resolveJsonModule": true,
    "outDir": "./build",
    "strict": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "include": ["./src/*", "./db/*"],
  "exclude": ["./**/__tests__/*", "./**/__functionaltests__/*"]
}

I tried replacing the ** prefix in entities & migrations with a path.join + __dirname but then typeorm could not detect the migration files and the entities. I understand this has something to do with the path resolving where the code ends up under the build folder but I am not sure how I can tackle this.

If I live it like this the CLI works for the unpiled code (ts) executing the app with something like nodemon but not for the piled (js) one.

The error I am getting from the piled js is the: SyntaxError: Cannot use import statement outside a module

Any help is appreciated, thanks a lot!

I've set up a Typescript project with TypeORM and I am facing some issues with the pilation.

My package structure is like this:

root
├── db
│   ├── migrations
│   │   ├── a_migration.ts
│   ├── connection
│   │   ├── config.ts <- ormconfig
│   │   ├── encrypt.ts
│   │   ├── index.ts <- creates the connection
├── src
│   ├── card
│   │   ├── entity.ts
├── package.json
├── tsconfig.json

My config.ts is:


export = {
  type: 'postgres',
  host: POSTGRES_HOST,
  port: POSTGRES_PORT,
  username: POSTGRES_USER,
  password: POSTGRES_PASS,
  database: POSTGRES_DB,
  synchronize: true,
  logging: false,
  entities: ['**src/**/*entity.{ts,js}'],
  migrations: ['**/migrations/*.{ts,js}'],
  cli: {
    entitiesDir: 'src/entity',
    migrationsDir: 'db/migrations',
  },
  namingStrategy: new SnakeNamingStrategy(),
};

tsconfig.json:

{
  "pilerOptions": {
    "baseUrl": ".",
    "target": "es6",
    "module": "CommonJS",
    "allowJs": false,
    "esModuleInterop": true,
    "noImplicitAny": true,
    "resolveJsonModule": true,
    "outDir": "./build",
    "strict": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "include": ["./src/*", "./db/*"],
  "exclude": ["./**/__tests__/*", "./**/__functionaltests__/*"]
}

I tried replacing the ** prefix in entities & migrations with a path.join + __dirname but then typeorm could not detect the migration files and the entities. I understand this has something to do with the path resolving where the code ends up under the build folder but I am not sure how I can tackle this.

If I live it like this the CLI works for the unpiled code (ts) executing the app with something like nodemon but not for the piled (js) one.

The error I am getting from the piled js is the: SyntaxError: Cannot use import statement outside a module

Any help is appreciated, thanks a lot!

Share Improve this question asked May 15, 2021 at 11:08 Ilias MentzIlias Mentz 5202 gold badges7 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Your entities and migrations should be given the build directory instead of the src directory.

And synchronize should set as false.

Try to update your config.ts like below:

export = {
  type: 'postgres',
  host: POSTGRES_HOST,
  port: POSTGRES_PORT,
  username: POSTGRES_USER,
  password: POSTGRES_PASS,
  database: POSTGRES_DB,
  synchronize: false,
  logging: false,
  entities: ['build/src/**/*entity.{ts,js}'],
  migrations: ['build/db/migrations/*.{ts,js}'],
  cli: {
    entitiesDir: 'src/entity',
    migrationsDir: 'db/migrations',
  },
  namingStrategy: new SnakeNamingStrategy(),
};

Note the changes in entities and migrations properties. If this did not work, most probably it might be due to the paths I specified are not inside the build directory. In that case, change those as necessary.

Hope this helps you! Cheers

发布评论

评论列表(0)

  1. 暂无评论