Home > Back-end >  Azure Functions: Cannot use import statement outside a module
Azure Functions: Cannot use import statement outside a module

Time:01-28

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!

CodePudding user response:

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.

  •  Tags:  
  • Related