-1

I couldn't find a recent question talking about this so I decided to create a new one.

I'm building some azure functions with Typescript and I'm getting:

import { Entity, BaseEntity, PrimaryColumn, Column, ManyToOne } from "typeorm"; SyntaxError: Cannot use import statement outside a module

This is my tsconfig

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "dist",
    "rootDir": ".",
    "sourceMap": true,
    "esModuleInterop": true,
    "strict": true,
    "lib": [
      "esnext"
    ],
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": true
  }
}

Any ideas how to solve this? Thanks!

linex
  • 3
  • 1
  • Does this answer your question? ["Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6](https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import) – Liam Jan 27 '22 at 11:57

1 Answers1

0

Here are the 2 fixes you need to do to resolve the syntax error

  1. Change .js files to .cjs
  2. Also add "type" : "Module" to package.json

Also check the related SO threads and Documentation.

SaiSakethGuduru-MT
  • 1,444
  • 1
  • 5
  • 11
  • Thanks! That worked! However, I also had to change *.ts files to *.cts in order for it to work – linex Jan 27 '22 at 14:30