react-dropzone: [BUG] react-dropzone cannot be imported in a Node.js ESM environment
I am experimenting with React SSR in a project that uses ESM ("type": "module" is set in package.json). The project uses react-dropzone, and although it will not be part of the server-side render, the app still imports it.
Importing react-dropzone in such an environment fails with the following error:
import { useDropzone } from "../node_modules/react-dropzone/dist/index.js";
^^^^^^^^^^^
SyntaxError: Named export 'useDropzone' not found. The requested module '../node_modules/react-dropzone/dist/index.js' is a CommonJS module, which may not support all module.exports as named exports.
The combination of two problems leads to this error:
- react-dropzone’s
package.jsonspecifies amainandmodulefield. Node.js only interprets themainfield, which leads to the CJS build. To fix this problem, anexportsfield should be defined whoseimportproperty points to the ESM build. - The ESM build (under
dist/es/index.js) is interpreted by Node.js as a CJS file, since notypeis set inpackage.jsonand the file has a.jsextension. To fix this problem, an ESM build with.mjsfile extensions should be provided.
To fix the problem in a backwards-incompatible way (making a major release necessary):
- Rename the files in
dist/esfrom.jsto.mjs - Add an
exportsfield topackage.json:"exports": { "import": "dist/es/index.mjs", "default": "dist/index.js" }
To fix the problem in a backwards-compatible way:
- Create an additional (identical) ESM build using
.mjsextensions, for example indist/mjs - Add an
exportsfield to package.json:“exports”: { “.”: { “import”: “dist/mjs/index.mjs”, “default”: “dist/index.js” }, “./": "./” }`
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 13
- Comments: 33
Hi everyone, I’ve created a temporary package
react-dropzone-esmwhich works correctly with modern frameworks. The package provides both esm and cjs bundles. Exports in package.json are properly configured (you can check the difference between react-dropzone and react-dropzone-esm).The package can be used as a drop-in replacement for
react-dropzone– the source code was not modified, I’ve changed only the build system.Hey I’m using remix with vite. I fix this by adding
ssr.noExternalin vite.config.ts. I think it just tells vite to bundle with ssr processing.Here’s a patch-package snippet in the meantime in case it helps others.
react-dropzone+14.2.3.patchI’m still getting this issue in 14.2.6:
Thanks for confirming. I’m glad you found a workaround for now. Hopefully, our ESM compatible version will be out soon if you’d still like to use our lib at that point.
Can confirm this issue using https://github.com/Nic13Gamer/better-upload with Tanstack start using version 14.3.5
fix for Tanstack start is
@bertho-zero we’re slowly working towards an fully ESM package.
@rolandjitsu I still have the issue with the latest version
Lifesaver @gjc14, I’m also using Remix and a google search led me straight here - works great!
I think that there are still some issues with the config in package.json. As pointed out by someone, there are lint errors when using publint.
I will try to look into it next week after I fix my notebook (screen just got damaged). Unless someone already has a PR to fix that by then.
On Sat, Oct 5, 2024, 16:50 Hasan Baki Kucukcakiroglu < @.***> wrote:
I’ve spent some time trying to change only the build tool, but had to:
For everything to work correctly, exports in package.json must look something like this:
It is also worth checking publint to verify that everything can be interpreted correctly (currently there are several issues) – https://publint.dev/react-dropzone@14.2.6
You are also welcome to take any part of the source code that I’ve written for
react-dropzone-esmpackage as is or as reference – https://github.com/rtivital/react-dropzone-esm