react-dropzone: [BUG] "Cannot use import statement outside a module" when imported from another dependency in Next app
Describe the bug This is a follow-up to #1259, and in fact it was also reported by @CestDiego in that same issue but after it got closed. I thought I’d create a separate issue for it.
I am not sure why, but it looks like Next (latest version here, 14.2.14) expects transitive dependencies (i.e. dependencies of dependencies) that have both CJS and ESM exports to have "type": "module" in their package.json, otherwise we get the following error:
Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
unhandledRejection
/Users/[my-project]/node_modules/.pnpm/react-dropzone@14.2.9_react@18.3.1/node_modules/react-dropzone/dist/es/index.js:37
import React, { forwardRef, Fragment, useCallback, useEffect, useImperativeHandle, useMemo, useReducer, useRef } from "react";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (node:internal/modules/cjs/loader:1378:20)
at Module._compile (node:internal/modules/cjs/loader:1428:41)
at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
at Module.load (node:internal/modules/cjs/loader:1288:32)
at Module._load (node:internal/modules/cjs/loader:1104:12)
at cjsLoader (node:internal/modules/esm/translators:346:17)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:286:7)
at ModuleJob.run (node:internal/modules/esm/module_job:234:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:473:24) {
type: 'SyntaxError'
}
To Reproduce
https://codesandbox.io/p/devbox/next-react-dropzone-bug-mj3qdk
Notice I am importing from local-package, which itself is a ESM package that imports react-dropzone. That is what causes the issue; importing react-dropzone directly from the Next app seems to work.
Expected behavior There should not be any error.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 16
if using vite; (remix for me). adding to config under ssr/noExternal has just worked for me
If you are on Astrojs, this worked for me:
The snippet gave by @jakeymoreland should work for all the apps sitting on Vitejs.
This seems fixed in the latest version, I believe by https://github.com/react-dropzone/react-dropzone/commit/201687900724b45ec98d26cde3626a1c6687c9e1. Thank you!