#import {DataSource} from 'typeorm' - SyntaxError: Cannot use import statement outside a module

3 messages · Page 1 of 1 (latest)

split knot
#

I'm trying to setup migrations config in my nestjs app with typeorm. Currently I'm getting error when trying to start app by running the script yarn start:dev.
import { DataSource } from 'typeorm'; ^^^^^^ SyntaxError: Cannot use import statement outside a module

My DataSource config looks like:

import * as dotenv from 'dotenv';

dotenv.config();

const config = new DataSource({
  type: 'postgres',
  host: process.env.POSTGRES_HOST,
  port: Number(process.env.POSTGRES_PORT),
  username: process.env.POSTGRES_USER,
  password: process.env.POSTGRES_PASSWORD,
  database: process.env.POSTGRES_DB,
  migrations: [__dirname + '/../*.js', __dirname + '/../**/*.js'],
  migrationsTableName: 'migration',
});

export default config;```

And the path in my app to this file is src/migration/config/ormConfig.ts. Also in src/migration I got all my migrations and some of them inside folders - src/migration/someFolder/someMigration.js and so on. Why am I getting such error? And how can I run my app without this error? In my package.json - "ts-node": "^10.9.1", "typeorm": "^0.3.17", "typescript": "^5.2.2"
limpid laurel
#

That someMigration.js you have in the src, can you show it?

split knot
#

Yes. ```
module.exports = class renameColumns1658156407600 {
async up(queryRunner) {
await queryRunner.query(`
ALTER TABLE vehicles
RENAME tonnage TO load_capacity;

    ALTER TABLE vehicles
    RENAME capacity TO volume_capacity;
    
    ALTER TABLE vehicles
    RENAME weight_license TO city_center_entry_permit;
    
    ALTER TABLE vehicles
    RENAME with_crew TO is_with_crew;
    `);

}

async down(queryRunner) {
await queryRunner.query(`
ALTER TABLE vehicles
RENAME load_capacity TO tonnage;

    ALTER TABLE vehicles
    RENAME volume_capacity TO capacity;
    
    ALTER TABLE vehicles
    RENAME city_center_entry_permit TO weight_license;
    
    ALTER TABLE vehicles
    RENAME is_with_crew TO with_crew;
    `);

}
};```