Cannot use import statement outside a module
In WebdriverIO, if you are using advanced Java script (ES6) features, you will see the following errors
0–0] import LoginPage from ‘../screenobjects/login.page’;
[0–0] ^^^^^^
[0–0]
[0–0] SyntaxError: Cannot use import statement outside a module
To fix the issue, use Babel to compile the JS files. Follow the steps below to configure Babel
STEP 1: Install Babel
Follow the steps in https://webdriver.io/docs/babel/ to install Babel node packages
STEP 2: Create ‘babel.config.js’
Once after you have installed the Babel node packages, create ‘babel.config.js’ file in your root directory with the following content
module.exports = {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "14"
}
}
]
]
}
Once this is set up WebdriverIO will take care of the rest.
If you are using Jasmine framework, configure Jasmine options in WebdriverIO configuration file.
Go to your config file and update the Jasmine options
// Options to be passed to Jasmine.
jasmineOpts: {
// Babel setup
helpers: [require.resolve('@babel/register')]
},
Hope this helps