question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't import ES6 modules in plugins

See original GitHub issue

What would you like?

We discovered that we can’t import ES6 modules in the Cypress plugin file without this error:

 /Volumes/workplace/package/src/test_package/node_modules/test_module/es6/test_directory/test.js:10
import * as t from 'io-ts/es6';
^^^^^^
 
SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1051:16)
    at Module._compile (internal/modules/cjs/loader.js:1101:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1166:10)
    at Module.load (internal/modules/cjs/loader.js:981:32)
    at Module._load (internal/modules/cjs/loader.js:881:14)
    at Function.Module._load (electron/js2c/asar.js:769:28)
    at Module.require (internal/modules/cjs/loader.js:1023:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (/Volumes/workplace/package/src/test_package/cypress/plugins/index.ts:11:1)
    at Module._compile (internal/modules/cjs/loader.js:1145:30)
    at Module.m._compile (/Users/redacted/Library/Caches/Cypress/5.5.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/ts-node/src/index.ts:536:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1166:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/redacted/Library/Caches/Cypress/5.5.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/ts-node/src/index.ts:539:12)
    at Module.load (internal/modules/cjs/loader.js:981:32)
    at Module._load (internal/modules/cjs/loader.js:881:14)
    at Function.Module._load (electron/js2c/asar.js:769:28)

Why is this needed?

This is needed so that we can import ES6 modules that are necessary to be run as a node server process inside of the plugin index.ts. We would like the option to either modify the ts-node configuration, or be able to configure the babel transpilation before the plugin is loaded and accessed.

We have managed to find our own workaround for now. See my colleague’s comment on this issue.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

3reactions
JohnnyFuncommented, Mar 15, 2021

I think you need to use require syntax in the plugins file. So something like const t = require('io-ts/es6') should work.

2reactions
stefankainzcommented, Mar 31, 2022

Any update on this? I have the exact same problem as @DiegoMcDipster described

Read more comments on GitHub >

github_iconTop Results From Across the Web

'Directory import is not supported resolving ES modules' with ...
With ES6 modules you can not (yet?) import directories. Your import should look like this: import database from "./database/index.js".
Read more >
@rollup/plugin-commonjs - npm
A Rollup plugin to convert CommonJS modules to ES6, ... Some require calls cannot be resolved statically to be translated to imports, e.g.....
Read more >
How to Bundle JavaScript With Rollup — Step-by-Step Tutorial
rollup-plugin-commonjs , which coverts CommonJS modules to ES6, which stops them from breaking Rollup. Install both plugins with the following command: npm ...
Read more >
16. Modules - Exploring JS
3 Imports and exports must be at the top level #. As explained in more detail later, the structure of ES6 modules is...
Read more >
Upgrade your Node app: From CommonJS to ES Modules
4) Fix imports and exports ... cjs-to-es6 converts const package = require('package') to import package from 'package' and const localFile = ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found