量角器/打字稿(es6):语法错误:无法在模块外部使用import语句

时间:2020-04-11 01:45:57

标签: typescript ecmascript-6 protractor mocha

我在import { browser } from "protractor";中出错,并抛出 SyntaxError:无法在命令行中的模块外部使用import语句

我正在使用打字稿,摩卡咖啡和量角器。

这里是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"
    ]
  }
 }

1 个答案:

答案 0 :(得分:1)

这对我有用:

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;
 }