Open fijiwebdesign opened 1 year ago
@fijiwebdesign and For any Googler, I found some useful trick for use this library with dynamic way.
import DatePicker from "react-datepicker";
??
is Nullish Coalescing)const ReactDatePicker = DatePicker.default ?? DatePicker;
<ReactDatePicker />
It would be perfect for all you guy who stuck with ES module import issues.
for Thai people or Buddhist who need a date picker library which support completely about the leap year and be standalone which don't need to import any css files. I made a library which implemented from react-datepicker. It called thaidatepicker-react
. Anybody can check this out on NPM or GitHub. Thank you.
I struggled with this issue for a looooooong time.
I initially made a bunch of components in a project, but soon figured out that these would be better as a separate package. One of the components was a date input which wrapped DatePicker.
When part of the project, everything worked fine. When I separated the components to its own package and imported that to the main project, everything except the date input worked.
I eventually figured out that when importing react-datepicker
in the main project, I got a function, i.e. the component itself. However, when I imported my package which imported react-datepicker
, the result was an object, an ES module, even though the import statements were identical, and both should get the default export.
@buildingwatsize's answer turned out to solve the problem. It is a hack, but it works.
Adapted for TypeScript, with a nasty type hack:
import ReactDatePicker from "react-datepicker";
const DatePicker =
(ReactDatePicker as unknown as { default: typeof ReactDatePicker }).default ??
ReactDatePicker
...
<DatePicker ... />
To me it looks like this issue would be caused by the "browser": "dist/react-datepicker.min.js"
in react-datepicker's package.json
Diving deeper
Not sure if the bug here is webpack or the react-datepicker, but exposing es6 module as the browser export would make more sense here and probably would fix this?
vite + rollup seems to handle this same situation correctly
I can confirm the same issue after migration from vite@3.x
to vite@4.x
. A work-around suggested by @Suppen worked for me.
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 10 days.
Hello! In my case i have react-datepicker in /ui package in a monorepo, and using this ui package in a nextjs app.
When doing so, I have a related issue when react-datepicker imports date-fns:
Error: Module not found: ESM packages (date-fns) need to be imported. Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
../../packages/ui/dist/index.mjs
@Suppen workaround did not work for me, but adding
experimental: {
esmExternals: 'loose',
}
in next.config.js did workaround it.
This options breaks the new turbopack bundler tho, so fixing the root cause of this commonjs export would be lovely!
Hope it helps
Describe the bug
We switched our build to esbuild and now react-datepicker returns a commonjs module for the ES6 import.
Note: package.json is set to
"type": "module",
To Reproduce
The above will error as
ReactDatePicker
needs to beReactDatePicker.default
:Expected behavior
Should import
ReactDatePicker
as ES6 module.