Expo typeorm migration commands throw Cannot use import statement outside a module

277 Views Asked by At

I have an Expo project with expo-sqlite and typeorm. Relevant configuration:

package.json

{
  "name": "testproject",
  "main": "expo-router/entry",
  //...
  "dependencies": {
    //...
    "expo-sqlite": "~11.3.2",
    //...
    "reflect-metadata": "^0.1.13",
    "typeorm": "^0.3.17"
  },
  "devDependencies": {
    //...
    "babel-plugin-transform-typescript-metadata": "^0.3.2",
    "ts-node": "^10.9.1",
    //...
  },
  //...
}

tsconfig.json

{
  "extends": "expo/tsconfig.base",
  "compilerOptions": {
    "strict": true,
    "paths": {
      "@/*": [
        "./*"
      ]
    },
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false
  },
  "include": [
    "**/*.ts",
    "**/*.tsx",
    ".expo/types/**/*.ts",
    "expo-env.d.ts"
  ],
}

babel.config.js

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      // Required for expo-router
      'expo-router/babel',
      '@babel/transform-react-jsx-source',
      'babel-plugin-transform-typescript-metadata'
    ],
  };
};

data-source.ts

import { DataSource, DataSourceOptions } from "typeorm";

export const options: DataSourceOptions = {
    type: "expo",
    database: 'TestProject',
    driver: require('expo-sqlite'),
    entities: [
        'entities/*.ts'
    ],
    //synchronize: true
    migrations: ['data/migrations/*.ts'],
    migrationsTableName: 'migrations'
};

const dataSource = new DataSource(options)
export default dataSource;

All of these files are in the root of the project (based on expo tabs template). Entities are in the /entities directory and migrations are in the /data/migrations directory

Attempting to run either npx typeorm migration:run -d ./data-source.ts or npx typeorm migration:generate CreateTables -d ./data-source.ts results in the following error

Error during migration run: Error: Unable to open file: "D:\Programming\TestProject\data-source.ts". Cannot use import statement outside a module at CommandUtils.loadDataSource (D:\Programming\TestProject\node_modules\typeorm\commands\CommandUtils.js:22:19) at async Object.handler (D:\Programming\TestProject\node_modules\typeorm\commands\MigrationRunCommand.js:41:26)

I have tried using npx typeorm-ts-node-esm as well with the same result.

I have also tried changing the data-source.ts file to data-source.js and using commonjs style importing and exporting:

const { DataSource } = require("typeorm");

const options = {
    type: "expo",
    database: 'TestProject',
    driver: require('expo-sqlite'),
    entities: [
        'entities/*.ts'
    ],
    //synchronize: true
    migrations: ['data/migrations/*.ts'],
    migrationsTableName: 'migrations'
};

const dataSource = new DataSource(options);
module.exports = dataSource;

but I am getting the following error

Error during migration run: Error: Unable to open file: "D:\Programming\TestProject\data-source.js". Unexpected token 'export' at CommandUtils.loadDataSource (D:\Programming\TestProject\node_modules\typeorm\commands\CommandUtils.js:22:19) at async Object.handler (D:\Programming\TestProject\node_modules\typeorm\commands\MigrationRunCommand.js:41:26)

What am I doing wrong?

0

There are 0 best solutions below