webdriverio: [๐Ÿ› Bug]: Cannot use import statement outside of module with V8 and babel setup.

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

8.16.5

Node.js Version

v18.17.1

Mode

WDIO Testrunner

Which capabilities are you using?

No response

What happened?

Hi, I have upgraded to V8 latest version and I see โ€œCannot use import statement outside a moduleโ€ for import statements.

My project uses commanjs and in V7 we used babel to compile the files that uses import statements. But now in V8 that is not working.

if I set type=module in my package.json, my whole test codebase has require statements which are not working. In our test code we import Dev constants file which uses es6 i.e imports

My wdio config has autoCompile section as below in V7, which use to take care of my imports as well. But now after upgrade to V8 that is not working.

autoCompileOpts: { babelOpts: { presets: [ [ โ€˜@babel/preset-envโ€™, { targets: { node: โ€˜18โ€™ } } ] ], plugins: [], ignore: [] } },

What is your expected behavior?

No response

How to reproduce the bug.

donโ€™t set type=module in package.json but try to use a simple import in wdio config file.

Have a autocompileOpts section as below: autoCompileOpts: { babelOpts: { presets: [ [ โ€˜@babel/preset-envโ€™, { targets: { node: โ€˜18โ€™ } } ] ], plugins: [], ignore: [] } },

Run any test you have, import will fail after upgrade to V8 but works in V7.

Relevant log output

(node:65307) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
2023-09-09T08:17:54.637Z ERROR @wdio/config:ConfigParser: Failed loading configuration file: file:///Users/lkamineni/huey/libs/testUtils/wdio.local.e2e.conf.js: Cannot use import statement outside a module
/Users/lkamineni/huey/libs/testUtils/wdio.local.e2e.conf.js:8
import _ from 'lodash';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._compile (/Users/lkamineni/huey/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Object.newLoader [as .js] (/Users/lkamineni/huey/node_modules/pirates/lib/index.js:104:7)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Process finished with exit code 1

Code of Conduct

  • I agree to follow this projectโ€™s Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Comments: 21 (9 by maintainers)

Most upvoted comments

yw! Thanks for followups on this. We can close the bug now!

sounds good.I will try and update here!