Azure Functions:无法在模块外使用导入语句

Azure Functions: Cannot use import statement outside a module

我找不到最近讨论这个问题的问题,所以我决定创建一个新问题。

我正在使用 Typescript 构建一些 azure 函数,我得到:

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

这是我的 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
  }
}

有什么解决办法吗? 谢谢!

这是您需要执行的 2 项修复来解决语法错误

  1. .js 个文件更改为 .cjs
  2. 同时添加 "type" : "Module" 到 package.json

同时检查相关的 threads and Documentation