Closed michaeldawson closed 2 years ago
I have exactly the same issue, and I'm pulling my hair out. It's this idiotic decision of react-dnd to only ship ES Next code.
I see you are also using NEXT.js. The issue is that next for some reason is importing the common.js file:
@minoru/react-dnd-treeview/dist/index.js
which is then importing the ES next file
react-dnd-html5-backend/dist/index.js
I do not understand, why NEXT starts starts to import the Common.js version ,albeit this package clearly specified that it has ESNext versions of the files.
I spent several hours on this now but no solution ....
Hello guys a quick fix will be nextjs/dynamic
First install...
npm i react-dnd-html5-backend
import dynamic from 'next/dynamic'
const { Tree } = {
Tree: dynamic(() => import('@minoru/react-dnd-treeview').then((module) => module.Tree) , { ssr: false }),
}
const { DndProvider } = {
DndProvider: dynamic(() => import('react-dnd').then((module) => module.DndProvider) , { ssr: false }),
}
import { HTML5Backend } from 'react-dnd-html5-backend'
After that use...
export default function Page() {
const initialData = [
{
"id": 1,
"parent": 0,
"droppable": true,
"text": "Folder 1"
},
{
"id": 2,
"parent": 1,
"text": "File 1-1"
},
{
"id": 3,
"parent": 1,
"text": "File 1-2"
},
{
"id": 4,
"parent": 0,
"droppable": true,
"text": "Folder 2"
},
{
"id": 5,
"parent": 4,
"droppable": true,
"text": "Folder 2-1"
},
{
"id": 6,
"parent": 5,
"text": "File 2-1-1"
}
]
const [treeData, setTreeData] = useState(initialData)
const handleDrop = (newTreeData) => setTreeData(newTreeData)
return (
<DndProvider backend={HTML5Backend} >
<Tree
tree={treeData}
rootId={0}
onDrop={handleDrop}
render={(node, { depth, isOpen, onToggle }) => (
<div style={{ marginLeft: depth * 10 }}>
{node.droppable && (
<span onClick={onToggle}>{isOpen ? "[-]" : "[+]"}</span>
)}
{node.text}
</div>
)}
/>
</DndProvider>
)
}
Thank you all for your reports.
I don't know why next reads the CommonJS files, but I have found that there is a problem with this package as well.
This package depends on react-dnd
, react-dnd-html5-backend
, etc.,
but these only support ESM format, so it makes no sense for this package to support CommonJS format.
Therefore, I have removed the CommonJS files, leaving only the ESM files and partially modified package.json. The alpha version can be installed from
npm i @minoru/react-dnd-treeview@alpha
This worked for the my Next project. How about your environment?
However, this fix has prevented the Storybook from building properly, so I will officially release it as v2.0.1 as soon as the problem is resolved.
v2.0.1 is released today. In this version, the package type was changed to "module" and distributed only in ESM format. This package with Next.js can be built without any problem.
Thank you very much, works like a charm.
Report
Current behavior
This library outputs a main dist/index.js file that attempts to use
require
to import from thereact-dnd-html5-backend
andreact-dnd
packages. However, these packages use ES modules, and this results in an error:I'm currently using Next.JS, and I understand that next should be preferring ES modules where possible. However, in this case it is choosing the CJS export of react-dnd-treeview, which isn't working with the latest versions of react-dnd and react-dnd-html5-backend.
Usage:
Expected behaviour
I expect the dist/index.js file not to raise an error.
Reproduction method
Example: After logging out, log in again with another account.
Reference information
package.json: