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.

CJS plugins file (and ideally also allowing an ESM one)

See original GitHub issue

Especially with Node 10 reaching EOL, and native ESM being used increasingly in Node projects (type: module in package.json), including those which may also house browser code in need of testing, the apparent inability to use at least an index.cjs plugins file with Cypress seems rather problematic. (It complains about the extension; pointing pluginsFile in cypress.json to a .cjs file didn’t work either; only switching to .js works–or removing type: module but that causes problems for Node ESM execution.)

(Please note that I’m not speaking about a @babel/register problem–I mean one simply can’t use a cjs file at all.)

Are there any plans (or workarounds) about this? Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:20
  • Comments:21 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
giltayarcommented, Jan 18, 2022

A simple workaround, till Cypress enables *.cjs configuration scripts, is to add a package.json in the cypress/plugins and cypress/supportfolders that have the following text in them:

{"type": "commonjs"}

This tells Node.js that all “js” files in this folder are CJS, and Cypress will load them without any problem.

1reaction
dbocommented, Feb 22, 2022

One important detail though is that since the plugins file is commonJS, the only way to use your esm modules is via dynamic import, e.g.

const webpackPreprocessor = require("@cypress/webpack-preprocessor");

module.exports = async (on, config) => {
    const {default: webpackOptions} = await import("../../webpack.config.mjs");

    on("file:preprocessor", webpackPreprocessor({
        webpackOptions,
    }));
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

CJS vs ESM - Andrea Giammarchi - Medium
The biggest difference between CJS and ESM, is that the former, filesystem constrained, understands some file content based on its extension, ...
Read more >
Config Files - Babel.js
Because project-wide config files are separated from the physical location of the config file, they can be ideal for configuration that must apply...
Read more >
Error [ERR_MODULE_NOT_FOUND]: Cannot find module
This answer was the one that helped me! I was using import and the file was .js - Adding --es-module-specifier-resolution=node saved the day...
Read more >
[AskJS] Any way to support ESM outputs from a CJS ... - Reddit
My preferred ideal way would be if there was a babel plugin that outputs ... The ESM standards require the file extensions, and...
Read more >
cjstoesm - npm
API support, enabling you to convert a project from CJS to ESM ... You can also run cjstoesm along with its peer dependencies...
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