Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loading from node import throws error #3849

Closed
stephencranedesign opened this issue Nov 6, 2023 · 1 comment
Closed

loading from node import throws error #3849

stephencranedesign opened this issue Nov 6, 2023 · 1 comment

Comments

@stephencranedesign
Copy link

We've been using this package for a while & it works great for us. we're currently looking at switching our projects build tasks in an attempt to get off of babel. Our old test script basically converted all of our import/export syntax back to commonjs before running our tests. Now that we're leaving those, we get errors consuming this package. here's a simplified example.

if i have this file

// index.mjs
import {createSlice} from '@reduxjs/toolkit';

console.log(createSlice);

& i run node index.mjs, I get the following error:

 import {createSlice} from '@reduxjs/toolkit';
        ^^^^^^^^^^^
SyntaxError: Named export 'createSlice' not found. The requested module '@reduxjs/toolkit' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@reduxjs/toolkit';
const {createSlice} = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:131:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:213:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12)

Node.js v20.9.0

I can get around that by doing what the error message says but looking at your package.json file, you already have an esm version of the script prepared exposed thru the "module" field but that's not understood by node. Have you considered adding exports to the package.json file? That allows me to get around around this error if I update your package.json to expose your dist/redux-toolkit.esm.js thru the package.json exports field & switch it's extension to .mjs.

so:

// package.json

exports: {
  "types": "./dist/index.d.ts",
  "node": {
     "import": "./dist/redux-toolkit.esm.mjs",
     "require": "./dist/index.js"
  },
  "default": "./dist/index.js"
}
@markerikson
Copy link
Collaborator

markerikson commented Nov 6, 2023

This is already fixed in our 2.0 beta releases - the latest is https:/reduxjs/redux-toolkit/releases/tag/v2.0.0-beta.4. Please try them out and let us know how they work!

Also see:

FYI we're hoping to release 2.0 final within the next month or so, and there's only a couple of items left to finish up - see https:/reduxjs/redux-toolkit/milestone/8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants