ohlori
ohlori

Reputation: 310

Typescript (es6): SyntaxError: Cannot use import statement outside a module

I'm having an error in import { browser } from "protractor"; and throwing SyntaxError: Cannot use import statement outside a module in command line.

I'm using typescript, mocha and protractor.

Here's the mochaOpts:

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

Here's the dependencies in 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"
 }

Here's the 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"
    ]
  }
 }

Upvotes: 1

Views: 2286

Answers (1)

ohlori
ohlori

Reputation: 310

This worked for me:

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

Upvotes: 3

Related Questions