several compilation errors in Typescrip+express+typeorm

20 views Asked by At

the problem: I just finished an api in typescript with express and typeorm and everything worked fine when I ran it in ts-node, but once I transpiled the code to javascript and tried to run it with node it sent me this error from the inicialization of typeorm: datasource: SyntaxError: Cannot use import statement outside a module

what I expect it to work: the application runs in typescript with ts-node and once transpiled, also in javascript with node whitout problems.

what I've tried: I know is a tsconfig problem, but I can't find where it is, I tried puting "type":"module" in my package.json and configuring the target to ES6, the module to ES6 and the module resolution to nodenext, node16, bundler... nothing did work, only got errors like "module is not found" or "unknown file extension".

Here are the tsconfig and package.json that works for typescript only:

 "compilerOptions": {
    "lib": ["ES5", "ES6"],
    "module": "CommonJS",
    "target": "ES6",
    "moduleResolution": "Node",
    "outDir": "./dist",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "skipLibCheck": true,
    "strictPropertyInitialization": false,
    "rootDir": "./src",
    "allowSyntheticDefaultImports": true,
    "declaration": true
{
   "name": "maquina_facturadora",
   "version": "1.0.0",
   "main": "index.ts",
   "scripts": {
      "dev": "ts-node src/index.ts",
      "start": "node dist/index.js",
      "build": "tsc",
      "typeorm": "typeorm-ts-node-commonjs"
   },
   "license": "ISC",
   "devDependencies": {
      "@types/cookie-parser": "^1.4.7",
      "@types/cors": "^2.8.17",
      "@types/express": "^4.17.21",
      "@types/jsonwebtoken": "^9.0.5",
      "@types/morgan": "^1.9.9",
      "@types/node": "^16.11.10",
      "ts-node": "10.9.1",
      "typescript": "^5.4.3"
   },
   "dependencies": {
      "class-transformer": "^0.5.1",
      "class-validator": "^0.14.1",
      "cookie-parser": "^1.4.6",
      "cors": "^2.8.5",
      "dotenv": "^16.3.2",
      "express": "^4.18.2",
      "express-async-errors": "^3.1.1",
      "jsonwebtoken": "^9.0.2",
      "morgan": "^1.10.0",
      "pg": "^8.4.0",
      "reflect-metadata": "^0.1.13",
      "typeorm": "0.3.20"
   }
}
0

There are 0 answers