johnwalley / allotment

A React component for resizable split views

Home Page:https://allotment.mulberryhousesoftware.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SyntaxError: Cannot use import statement outside a module

mooijtech opened this issue · comments

Hello John,

I'm wanting to try allotment in a new React project but I'm getting the following:

SyntaxError: Cannot use import statement outside a module

At:

allotment (1:0) @ eval

> 1 | module.exports = require("allotment");

Any idea how to solve this issue?

Kind regards,
Marten Mooij

May be worth mentioning I'm also using NextJS.

Thanks for the feedback @mooijtech.

I have a few NextJS projects I maintain so I'll try to reproduce this error there. If I can reproduce it I'll fix it and publish a new version. If I can't I'll get back to you for some more details.

Alright, thank you I appreciate it!

Looks like a reproduction!

Screenshot 2021-10-29 at 15 41 00

I'll have a quick look now for anything obviously wrong I'm doing. Otherwise, I'd hope to get a fix out early next week.

I suspect it's something to do with the server-side generation/rendering in NextJS and the bundle Allotment exports (probably only works in a browser).

NextJS document a workaround. I can get this to work in my test project. Hopefully this is good enough. I'm reluctant to make the library work outside a browser as it is very much about measuring the size of HTML elements and interacting with browser APIs.

You will also need to import the bundled css that comes with allotment.

import "allotment/dist/style.css";

I will add both these points to the documentation. Apologies for the struggles getting started. You're the first user and I appreciate the feedback. Let me know how you get on.

Hey John,

Sorry for my late reply.
What's the syntax you are using for the workaround?
The following doesn't seem to work for me:

import dynamic from "next/dynamic"

const Allotment = dynamic(
    () => import("allotment"),
    { ssr: false }
)

The error:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `ForwardRef(LoadableComponent)`.

Got it to work using:

const Allotment = dynamic(() => import("allotment").then(mod => mod.Allotment), { ssr: false });

Might be a good idea to add that code to the FAQ, the referenced documentation doesn't contain .then(mod => mod.Allotment) which is required to make it work as far as I can see.

Thanks for your help!

Good point. I tend to import the problematic component in its own file. You can then use the dynamic import on that local file. But your way avoids the extra file. I'll have a go at explaining all this in the README. Thanks again for you help.