Skip to content

[RTK Alpha]: Invalid exports config for CJS consumption w/ Node #3208

Closed

Description

I skimmed the issues but couldn't find anything matching, so apologies if it's a duplicate and/or intentional.

The current config in the Alpha does not allow for consumption of the CJS bundle in modern version of Node/any tool that follows Node's module resolution spec, as it uses .js to refer to a CJS module despite the package setting "type": "module". If "type": "module" is set, .cjs is necessary in "exports".

Some bundlers do work around this and are more forgiving (whether or not that's a good thing is something else entirely), but this config will not work in Node and/or any environments that follow its resolution mechanism.

To reproduce:

// index.cjs
const { createSlice } = require('@reduxjs/toolkit');
console.log(typeof createSlice);

Attempting to execute this file (node index.cjs) will result in the following expected error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/ryun/Projects/example/node_modules/redux-thunk/lib/index.js
require() of ES modules is not supported.
require() of /home/ryun/Projects/example/node_modules/redux-thunk/lib/index.js from /home/ryun/Projects/example/node_modules/@reduxjs/toolkit/dist/cjs/redux-toolkit.cjs.development.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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/ryun/Projects/example/node_modules/redux-thunk/package.json.

Generally, it should be pretty safe to use .cjs in "exports". Both are relatively new, and any environment that supports consuming from "exports" should support .cjs. This may mean you'll want to duplicate your CJS bundles, however (provide as .js for "main" and the spec-adherent .cjs for "exports"), assuming you're not opposed to larger install size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions