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.

Loading .cjs config files on Node 13

See original GitHub issue

🚀 Feature Proposal

If you have Node 13 and jest.config.js file in a package with type=module then Node will emit a warning:

(node:8906) Warning: require() of ES modules is not supported.
require() of /home/spyke/Projects/undercut/packages/undercut/jest.config.js from /home/spyke/Projects/undercut/node_modules/jest-config/build/readConfigFileAndSetRootDir.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename jest.config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/spyke/Projects/undercut/packages/undercut/package.json.

In case of manually specifying the config fole with --config jest.config.cjs it fails with an error:

The --config option requires a JSON string literal, or a file path with a .js or .json extension.
Example usage: jest --config ./jest.config.js

Using ES Modules syntax for config file gives an error too:

SyntaxError: Unexpected token 'export'

The issue is the same as babel/babel#10595. .cjs is mandatory, but being able to load an ESM from .js or .mjs is nice to have too.

Motivation

Support ES Modules workflows in Node13+

Example

Configuring Jest in packages with type=module and config file named jest.config.cjs

Pitch

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:22 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
kirill-konshincommented, Feb 13, 2020

The problem is that after upgrade of Node from 12.15 to 12.16 warning become an error.

I have a type=module, jest.config.js and .babelrc.js in my setup, it worked with warning, now it does not. Just putting a reference here for anyone who’ll look for more info just in case.

So in essence this ticket also applies to Node 12, not just 13 as mentioned in title.

2reactions
emma-borhaniancommented, Feb 16, 2020

@SimenB Yup, jest 25.

Maybe you missed a spot? https://github.com/facebook/jest/blob/v25.1.0/packages/jest-cli/src/cli/args.ts#L55

  if (
    argv.config &&
    !isJSONString(argv.config) &&
    !argv.config.match(/\.js(on)?$/)
  ) {
    throw new Error(
      'The --config option requires a JSON string literal, or a file path with a .js or .json extension.\n' +
        'Example usage: jest --config ./jest.config.js',
    );
  }

undercut test log: https://gist.github.com/emma-borhanian/94a233217b2f8551157291aa3adcc956

Read more comments on GitHub >

github_iconTop Results From Across the Web

Load a CJS module config inside another file - Stack Overflow
I'm trying to load a config inside a node module but I'm not sure which is a good practice to do it, I...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Node.js has two module systems: CommonJS modules and ECMAScript modules. Authors can tell Node.js to use the ECMAScript modules loader via the .mjs...
Read more >
Lukas Taegert-Atkinson on Twitter: "We keep extending Node ...
2020-03-29 Features Do not transpile config files with .mjs extension in Node 13+ or .cjs extension in any Node version and load them...
Read more >
Ecosystem File - PM2
Advanced process manager for production Node.js applications. Load balancer, logs facility, startup script, micro service management, at a glance.
Read more >
postcss-load-config - npm
ℹ️ When using an {Object} , the key can be a Node.js module name, a path to a JavaScript file that is relative...
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