尝试将 typescript 与 cypress 一起使用时出现错误:“导入”和“导出”可能仅与“sourceType:模块”一起出现

问题描述 投票:0回答:1

尝试将 typescript 与 Cucumber 功能文件一起使用时出现以下错误。如果我将

.ts
更改为
.js
,一切都会正常。错误也显得毫无头绪。

SyntaxError: 'import' and 'export' may appear only with 'sourceType: module'
    at EventEmitter.handler (C:\Users\A527629\AppData\Local\Cypress\Cache\3.2.0\Cypress\resources\app\packages\server\lib\plugins\util.js:67:29)
    at emitOne (events.js:115:13)
    at EventEmitter.emit (events.js:210:7)
    at ChildProcess.<anonymous> (C:\Users\...\Local\Cypress\Cache\3.2.0\Cypress\resources\app\packages\server\lib\plugins\util.js:25:29)
    at emitTwo (events.js:125:13)
    at ChildProcess.emit (events.js:213:7)
    at emit (internal/child_process.js:768:12)
    at _combinedTickCallback (internal/process/next_tick.js:141:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

我参考了url来配置它。

有关设置的更多详细信息,请访问 - url

cypress
1个回答
0
投票

您遇到的错误表明环境未正确设置来处理 TypeScript 的模块系统。默认情况下,TypeScript 使用 ES 模块(导入/导出),而 Node.js 通常使用 CommonJS (require/module.exports),除非明确配置为使用 ES 模块。

要解决此问题,请尝试以下操作:

检查 tsconfig.json:确保您的 tsconfig.json 文件已正确配置 ES 模块。添加或修改以下选项:

{
  "compilerOptions": {
    "module": "commonjs", // or "esnext" if using native ESM in Node.js
    "target": "es6",      // or a later version like "es2020"
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.