typescript (es6):语法错误:不能在模块外部使用import语句

bttbmeg0  于 2023-01-18  发布在  TypeScript
关注(0)|答案(1)|浏览(165)

我在import { browser } from "protractor";中遇到错误并抛出***语法错误:无法在命令行中的模块***外部使用import语句。
我用 typescript ,摩卡咖啡和量角器。
下面是mochaOpts

mochaOpts: {
    bail: false,
    ui: 'bdd', 
    reporter: 'spec',
    compiler: 'ts:ts-node/register'
},

下面是package.json中的依赖项:

"dependencies": {
   "@babel/cli": "^7.7.4",
   "@babel/preset-env": "^7.7.4",
   "@babel/register": "^7.7.4",
   "@types/mocha": "^7.0.1",
   "@types/node": "^13.7.6",
   "chai": "^4.2.0",
   "chai-as-promised": "^7.1.1",
   "graphviz": "0.0.9",
   "lodash": "^4.17.15",
   "minimist": "^1.2.0",
   "mocha": "^6.2.2",
   "npm-failsafe": "^0.4.1",
   "protractor": "^5.4.2",
   "rimraf": "^2.7.1",
   "ts-node": "^8.5.4",
   "tslint": "^5.20.1",
   "typescript": "^3.8.2",
   "typescript-logging": "^0.6.4",
   "webdriver-manager": "^12.1.7",
   "yargs": "^13.3.0"
 }

下面是tsconfig.json:

{
  "compileOnSave": true,
  "compilerOptions": {
    "baseUrl": ".",
    "module": "commonjs",
    "paths": { "*": ["types/*"] },
    "outDir": "./dist/out-tsc",
    "mapRoot": "./dist/out-tsc/",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "typeRoots": [
      "node_modules/@types"
    ]
  }
 }
pzfprimi

pzfprimi1#

这对我很有效:

beforeLaunch: function() {
        require('ts-node').register({
        project: 'tsconfig.json'
        });
    },

onPrepare: () => {
    let globals = require("protractor");
    global.browser = globals.browser;
    var chai = require("chai");
    var chaiAsPromised = require("chai-as-promised");
    chai.use(chaiAsPromised);
    global.chai = chai;
 }

相关问题