Search code examples
typescriptbabeljstypeormproduction

Typeorm production vs development - import statement outside a module


I have a project setup with NodeJS, typescript and TypeORM using ts-node-dev. Currently development works without issues, but when I try building it with babel and running it I have the following error in runtime (after build):

SyntaxError: Cannot use import statement outside a module

this happens right after running node dist/server.js

My package.json is setup as follows:

{
  "main": "src/server.ts",
  "scripts": {
    "start": "node dist/server.js",
    "dev": "ts-node-dev --transpile-only --no-notify --ignore-watch node_modules .",
    "typeorm": "ts-node-dev -r tsconfig-paths/register ./node_modules/typeorm/cli.js",
    "test": "jest",
    "build": "babel src --extensions \".ts\" --out-dir dist --copy-files"
  },
  "author": "Bruno Mello",
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "pg": "^8.5.1",
    "reflect-metadata": "^0.1.13",
    "tsyringe": "^4.5.0",
    "typeorm": "^0.2.31",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@babel/cli": "^7.13.14",
    "@babel/core": "^7.13.14",
    "@babel/node": "^7.13.13",
    "@babel/plugin-proposal-class-properties": "^7.13.0",
    "@babel/plugin-proposal-decorators": "^7.13.5",
    "@babel/preset-env": "^7.13.12",
    "@babel/preset-typescript": "^7.13.0",
    "@types/cors": "^2.8.10",
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.22",
    "@types/jsonwebtoken": "^8.5.1",
    "@types/node": "^14.14.37",
    "@types/uuid": "^8.3.0",
    "@typescript-eslint/eslint-plugin": "^4.19.0",
    "@typescript-eslint/parser": "^4.19.0",
    "babel-plugin-module-resolver": "^4.1.0",
    "babel-plugin-transform-typescript-metadata": "^0.3.2",
    "eslint": "^7.23.0",
    "eslint-config-airbnb-base": "^14.2.1",
    "eslint-config-prettier": "^8.1.0",
    "eslint-import-resolver-typescript": "^2.4.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-prettier": "^3.3.1",
    "jest": "^26.6.3",
    "prettier": "2.2.1",
    "ts-jest": "^26.5.4",
    "ts-node-dev": "^1.1.6",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.2.3"
  }
}

my ormconfig.js:

if (process.env.NODE_ENV === 'development') {
  const dotenv = require('dotenv');
  dotenv.config();
}

module.exports = {
  name: 'default',
  type: 'postgres',
  host: process.env.DBHOST,
  port: process.env.DBPORT,
  username: process.env.DBUSER,
  password: process.env.DBPASSWORD,
  database: process.env.DBNAME,
  synchronize: false,
  logging: process.env.NODE_ENV === 'development',

  entities: ['dist/modules/**/infra/typeorm/models/*.js'],
  migrations: ['dist/shared/typeorm/migrations/*.js'],
  cli: {
    migrationsDir: 'dist/shared/typeorm/migrations',
  },
};

my babel.config.js:

module.exports = {
  presets: [
    ['@babel/preset-env', { targets: { node: 'current' } }],
    '@babel/preset-typescript',
  ],
  plugins: [
    'babel-plugin-transform-typescript-metadata',
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    ['@babel/plugin-proposal-class-properties', { loose: true }],
  ],
};

and my tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./dist",
    "strict": true,
    "strictPropertyInitialization": false,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

After some research I found some people suggesting that I replace entities and migrations on orm config to reference dist folder instead of src folder, changing from:

entities: ['src/modules/**/infra/typeorm/models/*.ts'],
  migrations: ['src/shared/typeorm/migrations/*.ts'],
  cli: {
    migrationsDir: 'src/shared/typeorm/migrations',
  },

to:

entities: ['dist/modules/**/infra/typeorm/models/*.js'],
  migrations: ['dist/shared/typeorm/migrations/*.js'],
  cli: {
    migrationsDir: 'dist/shared/typeorm/migrations',
  },

and this actually fixes the problem with the dist code, but breaks my development environment (the one that runs with ts-node-dev), so it is not a valid option.


Solution

  • You can change path dynamically:

    ormconfig.js

    const baseDir = process.env.NODE_ENV == 'production' ? 'dist/src' : 'src';
    
    module.exports = {
      ...
      entities: [baseDir + '/entity/**/*.{ts,js}'],
      migrations: [baseDir + '/migration/**/*.{ts,js}'],
      subscribers: [baseDir + '/subscriber/**/*.{ts,js}'],
      cli: {
        entitiesDir: baseDir + '/entity',
        migrationsDir: baseDir + '/migration',
        subscribersDir: baseDir + '/subscriber',
      },
    };