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