使用打字稿的 Cucumber-JS 步骤定义中的“错误:无法在模块外部使用 import 语句”

Vic*_*tor 7 node.js typescript protractor cucumberjs angular

我收到以下错误:

command: npx cucumber-js .\cucumber-e2e\
import { Given, When, Then  } from '@cucumber/cucumber';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at C:\dev\FrontSystems.KeystonePortal\Keystone.Web\ClientApp\node_modules\@cucumber\cucumber\lib\cli\index.js:122:17
    at Array.forEach (<anonymous>)
    at Cli.getSupportCodeLibrary (C:\dev\xxxxx\xxxx.Web\ClientApp\node_modules\@cucumber\cucumber\lib\cli\index.js:120:26) 
    at Cli.run (C:\dev\xxxx\xxxx.Web\ClientApp\node_modules\@cucumber\cucumber\lib\cli\index.js:145:41)
    at async Object.run [as default] (C:\dev\xxxxx\xxxx.Web\ClientApp\node_modules\@cucumber\cucumber\lib\cli\run.js:25:18)codepath: C:\dev\xxxxx\xxxx.Web\ClientApp\cucumber-e2e\step-definitions\catalog.steps.ts
Run Code Online (Sandbox Code Playgroud)

步骤文件:

import { Given, When, Then  } from '@cucumber/cucumber';

Given('A bank account with starting balance of {int}', (balance: number) => {
    // Write code here that turns the phrase above into concrete actions
    return 'pending';
  });
Run Code Online (Sandbox Code Playgroud)

我的文件夹结构如下:

在此输入图像描述

黄瓜.js:

var common = [
  '--require ./cucumber-e2e/step-definitions/**/*.ts',
  '--publish-quiet',
].join(' ');

module.exports = {
  default: common,
};
Run Code Online (Sandbox Code Playgroud)

tsconfig.json:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/cucumber-e2e",
    "module": "commonjs",
    "target": "es5",
    "types": [
      "jasmine",
      "jasminewd2",
      "node"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

继承tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "resolveJsonModule": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "jszip": [
        "node_modules/jszip/dist/jszip.min.js"
      ]
    },
    "plugins": [
      {
        "name": "typescript-tslint-plugin",
        "alwaysShowRuleFailuresAsWarnings": false,
        "ignoreDefinitionFiles": true,
        "configFile": "./tslint.json",
        "suppressWhileTypeErrorsPresent": false
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我已将以下包添加到 package.json 中:

"@cucumber/cucumber": "^7.3.2",
"@types/chai": "^4.3.0",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"protractor-cucumber-framework": "^8.4.0",
"webdriver-manager": "^12.1.8"
Run Code Online (Sandbox Code Playgroud)

因此,功能文件和步骤定义正在被识别,但是它在不应该的情况下抛出了语法错误。我有一种感觉,它可能与 package.json 有关,但我尝试了不同软件包的多个版本,但没有得到积极的结果。

所有教程似乎都是这样做的或者非常相似。

有任何想法吗?

Ben*_*Ben 3

如果你没有type在你的 中指定 of 模块package.json,它将默认为 CommonJS。在这种情况下,您不能使用import语法,您必须依赖require.

有两种方法可以解决这个问题:

  1. 更改导入语法以使用 require:
const { Given, When, Then } = require('@cucumber/cucumber');
Run Code Online (Sandbox Code Playgroud)
  1. 将模块类型更改为 ES 模块:
const { Given, When, Then } = require('@cucumber/cucumber');
Run Code Online (Sandbox Code Playgroud)

请注意,在第二种情况下,如果您请求的模块是 CommonJS 模块,它可能不支持命名导出,您将不得不回退到以下语法:

import Cucumber from '@cucumber/cucumber';
const { Given, When, Then } = Cucumber;
Run Code Online (Sandbox Code Playgroud)

  • 您可以查看有关 [Javascript 模块](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) 的 MDN 文档,但如果您想了解更详细的历史记录,请查看在 Medium 上的那篇文章 [JavaScript 模块简史](https://medium.com/sungthecoder/javascript-module-module-loader-module-bundler-es6-module-confused-yet-6343510e7bde) (2认同)