【问题标题】:Unexpected token export es6 dependency in ts projectts 项目中意外的令牌导出 es6 依赖项
【发布时间】:2021-10-03 09:10:31
【问题描述】:

我试图在本地类型脚本项目中使用库 bpmn.io 来扩展它。

所以我尝试使用语句import {BpmnModeler, BpmnViewer} from 'bpmn-js' 导入其组件。该库显然在内部使用 es6 模块来导入和导出内容。

当我尝试使用 ts-node <path/to/my/index.ts>(我的 typescript 项目的入口点)运行此代码时,我在终端中从 bpmn.io 库的默认导出处获得了一个意外的令牌“导出”。

下面是我的 tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": [
      "es2020"
    ],

    "allowJs": true,
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "./dist",
    "composite": true,
    "strict": true,
    "moduleResolution": "node",
    "rootDirs": [
      "."
    ],

    "esModuleInterop": true,
   
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "references": [],
  "watchOptions": {
    "fallbackPolling": "dynamicPriority",
    "excludeDirectories": [
      "**/node_modules",
      "build"
    ]
  }
}

我相信这是因为我在上面的 tsconfig.json 中设置了 module: "commonjs" 选项。但是,如果我将其更改为 module: "es6",那么我必须在我的 package.json 中设置 type="module" 选项,这会导致 unknonwn 文件扩展名 .ts 错误。这里有点问题

【问题讨论】:

  • "unknonwn 文件扩展名 .ts" - 但是您的 package.json 应该引用已编译的 .js 文件,而不是 typescript 源?
  • 当我使用节点时,当然。不过我正在使用 ts-node。

标签: typescript ecmascript-6 es6-modules ts-node


【解决方案1】:

您是否尝试将目标设置为 ES5?

检查这个答案:

unexpected token import/export - typescript

我遇到了同样的问题,但我已经解决了。

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 2018-11-24
    • 2019-03-21
    • 1970-01-01
    • 2017-02-01
    • 2016-07-16
    • 2017-02-13
    • 2020-05-11
    • 2018-01-20
    相关资源
    最近更新 更多