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
[BUG] react-dropzone cannot be imported in a Node.js ESM environment #1259
Comments
@cdauth thanks for the report. We will look into it. Also if you feel up to it we are also open to a contribution PR to add this 😁. Otherwise I'll take a look later after the holidays. |
I am also having the same issue. |
I am also having this issue! |
Yep this is going to continue being an issue with the prevalence of Next. {
[...]
"type": "module",
"main": "./dist/index.js",
"module": "./dist/es/index.js",
"types": "./typings/react-dropzone.d.ts",
"exports": {
".": {
"import": "./dist/es/index.js",
"require": "./dist/index.js"
}
},
[...]
} EDIT: After the below discourse I admit I gave bad info here, it really should just be what OP suggested so an edit like this: {
[...]
"main": "./dist/index.js",
"module": "./dist/es/index.mjs",
"types": "./typings/react-dropzone.d.ts",
"exports": {
".": {
"import": "./dist/es/index.jsm",
"require": "./dist/index.js"
}
},
[...]
} |
This will not work in a Node.js environment. See https://nodejs.org/api/packages.html#determining-module-system. |
@cdauth I agree .mjs is more clear but read the second point in that link - it can be .js when coupled with I'm by no means a veteran at this stuff though and I don't know the full implications of setting type:module but so far the above approach has worked for me with modules i've made that get consumed by node and bundled via commonjs (also being used in both frontend/backend parts of a Next app). Honestly keen to hear more insight on the topic though because it really is confounding stuff.. |
@jacobclarke92 It's true that
I think/hope the future will be all ESM, so |
@cdauth That's some really good, concise info. Thanks! Hope I've not derailed this issue too much haha.. I'll show myself out now. |
Here's a patch-package snippet in the meantime in case it helps others.
diff --git a/node_modules/react-dropzone/dist/es/index.js b/node_modules/react-dropzone/dist/es/index.mjs
similarity index 99%
rename from node_modules/react-dropzone/dist/es/index.js
rename to node_modules/react-dropzone/dist/es/index.mjs
index 39d9345..5deaac3 100755
--- a/node_modules/react-dropzone/dist/es/index.js
+++ b/node_modules/react-dropzone/dist/es/index.mjs
@@ -37,7 +37,7 @@ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) r
import React, { forwardRef, Fragment, useCallback, useEffect, useImperativeHandle, useMemo, useReducer, useRef } from "react";
import PropTypes from "prop-types";
import { fromEvent } from "file-selector";
-import { acceptPropAsAcceptAttr, allFilesAccepted, composeEventHandlers, fileAccepted, fileMatchSize, canUseFileSystemAccessAPI, isAbort, isEvtWithFiles, isIeOrEdge, isPropagationStopped, isSecurityError, onDocumentDragOver, pickerOptionsFromAccept, TOO_MANY_FILES_REJECTION } from "./utils/index";
+import { acceptPropAsAcceptAttr, allFilesAccepted, composeEventHandlers, fileAccepted, fileMatchSize, canUseFileSystemAccessAPI, isAbort, isEvtWithFiles, isIeOrEdge, isPropagationStopped, isSecurityError, onDocumentDragOver, pickerOptionsFromAccept, TOO_MANY_FILES_REJECTION } from "./utils/index.mjs";
/**
* Convenience wrapper component for the `useDropzone` hook
*
@@ -383,7 +383,7 @@ var initialState = {
* @function useDropzone
*
* @param {object} props
- * @param {import("./utils").AcceptProp} [props.accept] Set accepted file types.
+ * @param {import("./utils/index.mjs").AcceptProp} [props.accept] Set accepted file types.
* Checkout https://developer.mozilla.org/en-US/docs/Web/API/window/showOpenFilePicker types option for more information.
* Keep in mind that mime type determination is not reliable across platforms. CSV files,
* for example, are reported as text/plain under macOS but as application/vnd.ms-excel under
@@ -969,4 +969,4 @@ function reducer(state, action) {
function noop() {}
-export { ErrorCode } from "./utils";
\ No newline at end of file
+export { ErrorCode } from "./utils/index.mjs";
\ No newline at end of file
diff --git a/node_modules/react-dropzone/dist/es/utils/index.js b/node_modules/react-dropzone/dist/es/utils/index.mjs
similarity index 99%
rename from node_modules/react-dropzone/dist/es/utils/index.js
rename to node_modules/react-dropzone/dist/es/utils/index.mjs
index 8e5abd5..f155743 100644
--- a/node_modules/react-dropzone/dist/es/utils/index.js
+++ b/node_modules/react-dropzone/dist/es/utils/index.mjs
@@ -26,7 +26,9 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
-import accepts from "attr-accept"; // Error codes
+import _accepts from "attr-accept"; // Error codes
+
+const accepts = typeof _accepts === "function" ? _accepts : _accepts.default;
export var FILE_INVALID_TYPE = "file-invalid-type";
export var FILE_TOO_LARGE = "file-too-large";
diff --git a/node_modules/react-dropzone/package.json b/node_modules/react-dropzone/package.json
index 9409558..97b8175 100644
--- a/node_modules/react-dropzone/package.json
+++ b/node_modules/react-dropzone/package.json
@@ -2,7 +2,13 @@
"name": "react-dropzone",
"description": "Simple HTML5 drag-drop zone with React.js",
"main": "dist/index.js",
- "module": "dist/es/index.js",
+ "module": "dist/es/index.mjs",
+ "exports": {
+ ".": {
+ "import": "./dist/es/index.mjs",
+ "require": "./dist/index.js"
+ }
+ },
"sideEffects": false,
"scripts": {
"cz": "git-cz", |
Is this bug fixed in the newer versions ? |
Unfortunately still broken as of 14.2.3 |
I was able to use this work around based on a tip from pingdotgg/uploadthing#42. import dropzone from "react-dropzone";
const { useDropzone } = dropzone; It appears react-dropzone project hasn't been updated since Oct-2022. Is there anything we can do to help the authors maintain this project? It appears a PR #1293 is available to fix this problem. |
Hi everyone, I've created a temporary package The package can be used as a drop-in replacement for |
I am experimenting with React SSR in a project that uses ESM (
"type": "module"
is set inpackage.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:
The combination of two problems leads to this error:
package.json
specifies amain
andmodule
field. Node.js only interprets themain
field, which leads to the CJS build. To fix this problem, anexports
field should be defined whoseimport
property points to the ESM build.dist/es/index.js
) is interpreted by Node.js as a CJS file, since notype
is set inpackage.json
and the file has a.js
extension. To fix this problem, an ESM build with.mjs
file extensions should be provided.To fix the problem in a backwards-incompatible way (making a major release necessary):
dist/es
from.js
to.mjs
exports
field topackage.json
:"exports": { "import": "dist/es/index.mjs", "default": "dist/index.js" }
To fix the problem in a backwards-compatible way:
.mjs
extensions, for example indist/mjs
exports
field to package.json:
"exports": { ".": { "import": "dist/mjs/index.mjs", "default": "dist/index.js" }, "./": "./" }`The text was updated successfully, but these errors were encountered: