"Error: Cannot use import statement outside a module" in Cucumber-JS step definition w/ typescript
"Error: Cannot use import statement outside a module" in Cucumber-JS step definition w/ typescript
我收到以下错误:
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
步骤文件:
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';
});
我的文件夹结构如下:
cucumber.js:
var common = [
'--require ./cucumber-e2e/step-definitions/**/*.ts',
'--publish-quiet',
].join(' ');
module.exports = {
default: common,
};
tsconfig.json:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/cucumber-e2e",
"module": "commonjs",
"target": "es5",
"types": [
"jasmine",
"jasminewd2",
"node"
]
}
}
继承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
}
]
}
}
并且我已将以下包添加到 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"
因此,功能文件和步骤定义正在被识别,但是它在不应该的时候抛出语法错误。我感觉它可能与 package.json 有关,但我尝试了不同软件包的多个版本,但没有得到积极的结果。
所有教程似乎都是这样做的或非常相似。
有什么想法吗?
如果您没有在 package.json
中指定模块的 type
,它将默认为 CommonJS。在这种情况下,您不能使用 import
语法,您必须依赖 require
.
有两种方法可以解决这个问题:
- 更改导入语法以使用 require:
const { Given, When, Then } = require('@cucumber/cucumber');
- 将您的模块类型更改为 ES 模块:
// package.json
{
...
"type": "module",
...
}
请注意,在第二种情况下,如果您请求的模块是 CommonJS 模块,它可能不支持命名导出,您将不得不回退到以下语法:
import Cucumber from '@cucumber/cucumber';
const { Given, When, Then } = Cucumber;
我收到以下错误:
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
步骤文件:
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';
});
我的文件夹结构如下:
cucumber.js:
var common = [
'--require ./cucumber-e2e/step-definitions/**/*.ts',
'--publish-quiet',
].join(' ');
module.exports = {
default: common,
};
tsconfig.json:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/cucumber-e2e",
"module": "commonjs",
"target": "es5",
"types": [
"jasmine",
"jasminewd2",
"node"
]
}
}
继承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
}
]
}
}
并且我已将以下包添加到 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"
因此,功能文件和步骤定义正在被识别,但是它在不应该的时候抛出语法错误。我感觉它可能与 package.json 有关,但我尝试了不同软件包的多个版本,但没有得到积极的结果。
所有教程似乎都是这样做的或非常相似。
有什么想法吗?
如果您没有在 package.json
中指定模块的 type
,它将默认为 CommonJS。在这种情况下,您不能使用 import
语法,您必须依赖 require
.
有两种方法可以解决这个问题:
- 更改导入语法以使用 require:
const { Given, When, Then } = require('@cucumber/cucumber');
- 将您的模块类型更改为 ES 模块:
// package.json
{
...
"type": "module",
...
}
请注意,在第二种情况下,如果您请求的模块是 CommonJS 模块,它可能不支持命名导出,您将不得不回退到以下语法:
import Cucumber from '@cucumber/cucumber';
const { Given, When, Then } = Cucumber;