3

I am building an app using Angular, NestJS, TypeORM and PostgreSQL. The issue I have is that when I am running the server using npm run start:dev or npm run start. The server starts, but when I go to the dist folder and try to run node main.js the server throws the following error

SyntaxError: Cannot use import statement outside a module

I need that because I am trying to deploy the app at DigitalOcean and use pm2.

Here is a sample of my code:

package.json:

"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/src/main.js\"",
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config ./ormconfig.ts",
"typeorm:migrate": "npm run typeorm migration:generate -- -n",
"typeorm:run": "npm run typeorm migration:run",

ormconfig.ts

import * as yn from 'yn';
const config: any = {
type: process.env.DB_TYPE as any,
host: +process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
ssl: yn(process.env.SSL_STATUS),
dialectOptions: {
    ssl: { require: yn(process.env.SSL_STATUS) },
  },
entities: [__dirname + '/**/*.entity{.ts,.js}'],

  synchronize: false,
  migrationsRun: true,
  migrations: ['src/database/migrations/**/*.ts'],
  cli: {
    migrationsDir: 'src/database/migrations',
  },
};

export = config;

app.module.ts:

@Module({
  imports: [
    ConfigModule.forRoot({isGlobal: true}),
    DatabaseModule,
    <other modules>
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

database.module.ts

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        type: configService.get('DB_TYPE'),
        host: configService.get('DB_HOST'),
        port: configService.get('DB_PORT'),
        username: configService.get('DB_USER'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_NAME'),
        entities: [__dirname + '/**/*.entity{.ts,.js}'],
        synchronize: false,
      }),
    }),
  ],
  exports: [],
})
export class DatabaseModule {}

The error

enter image description here

double-beep
  • 5,031
  • 17
  • 33
  • 41
Martin Pavlov
  • 31
  • 1
  • 3
  • How are you building the app? There should be no reference to .ts file after the build. Read the error carefully! its referncing to ormconfig.ts which shouldn't be the case. – Shumail Jan 29 '20 at 13:03
  • Does this answer your question? [TypeORM Entity in NESTJS - Cannot use import statement outside a module](https://stackoverflow.com/questions/59435293/typeorm-entity-in-nestjs-cannot-use-import-statement-outside-a-module) – Jay McDoniel Jan 29 '20 at 16:45
  • Unfortunately it does not. [But this here did](https://stackoverflow.com/questions/52121725/maintain-src-folder-structure-when-building-to-dist-folder-with-typescript-3) The problem was that I was using ormconfig.ts with an "import" statement outside the "src" folder. I moved the ormconfig.ts to the "database" folder and resolved this problem. But then I had to provide this ormconfig wherever I was creating connection with the DB. For examples in the seed file -> `const connection = await createConnection(config);` – Martin Pavlov Jan 30 '20 at 09:10
  • Glad you solved your problem ! Don't forget to mark your question as solved thought :p – A. Maitre Jan 30 '20 at 11:16

0 Answers0