So I'm using TypeORM in a TypeScript/Express application with ts-node.
I think the problem lies in the ormconfig.ts file. The entities array is not being transpiled correctly, and so the dist/.js version of this file is loading the .ts versions of the entities instead of the .js transpiled ones from the dist folder.
(note: I am not using an ornmconfig.json file instead because I want to dynamically change the config depending on ENV values)
Basically:
This ormconfig.ts gives the error on npm start:
module.exports = {
type: 'mssql',
host: process.env.DB_SERVER,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
port: Number(process.env.DB_PORT),
entities: ['entities/*.ts'],
logging: true,
synchronize: false,
options: { trustServerCertificate: true },
};
This one works:
module.exports = {
type: 'mssql',
host: process.env.DB_SERVER,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
port: Number(process.env.DB_PORT),
entities: ['dist/entities/*.js'],
logging: true,
synchronize: false,
options: { trustServerCertificate: true },
};
The thing is, I don't think it is appropiate to run transpiled code in development, plus I have a DEBUG setup with nodemon that I want to keep.
This is my package.json:
{
"name": "seguimiento-express-server",
"version": "0.1.0",
"description": "Server en express para exponer endpoints de seguimiento a apps móviles",
"main": "app.ts",
"scripts": {
"start": "tsc && node --unhandled-rejections=strict ./dist/app.js",
"prepare": "husky install",
"lint": "eslint . --ext .ts,.tsx",
"lint:fix": "eslint --fix --ext .ts,.tsx",
"serve": "nodemon app.ts",
"debug": "export DEBUG=* && npm run serve",
"test": "jest"
},
"author": "John Doe",
"license": "ISC",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.31.2",
"@typescript-eslint/parser": "^4.31.2",
"cz-conventional-changelog": "^3.3.0",
"cz-conventional-emoji": "^1.0.2",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^5.1.0",
"husky": "^7.0.0",
"jest": "^27.3.1",
"lint-staged": "^11.1.2",
"nodemon": "^2.0.13",
"prettier": "^2.4.1"
},
"lint-staged": {
"*.(js|ts)": "eslint --cache --fix"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-emoji"
}
},
"dependencies": {
"@types/jest": "^27.0.2",
"@types/cors": "^2.8.12",
"@types/debug": "^4.1.7",
"@types/express": "^4.17.13",
"@types/mssql": "^7.1.3",
"@types/node": "^16.9.6",
"ts-jest": "^27.0.7",
"cors": "^2.8.5",
"debug": "^4.3.2",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-winston": "^4.2.0",
"fetch": "^1.1.0",
"mssql": "^7.2.1",
"node": "^17.0.1",
"reflect-metadata": "^0.1.13",
"source-map-support": "^0.5.20",
"ts-node": "^10.2.1",
"typeorm": "^0.2.38",
"typescript": "^4.4.3",
"winston": "^3.3.3"
}
}
And my tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"rootDir": "./",
"outDir": "./dist",
"esModuleInterop": true,
"inlineSourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"types": ["jest", "node"]
},
"include": ["**/*.ts", "tests/*.ts"]
}
The error:
import { Entity, Column, PrimaryGeneratedColumn, BaseEntity, OneToMany } from 'typeorm';
^^^^^^
SyntaxError: Cannot use import statement outside a module