-
-
Notifications
You must be signed in to change notification settings - Fork 127
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] NextJS: SyntaxError: Cannot use import statement outside a module #177
Comments
@YujiNNakashima Hi! Sorry for late response. I reproduced your problem by bootstrap nextjs app and yes, this is a problem with ESmodules. We are using TS in a component and have a module target set to But don't worry, I tried to use our npm i @asyncapi/web-component -s and then import and use the component in this way: export default function SomeYourComponent() {
// lazy import for component
React.useEffect(() => import("@asyncapi/web-component/lib/asyncapi-web-component"), []);
return (
<asyncapi-component schema={asyncapiSchema} cssImportPath="./asyncapi.css" />
);
} where It works in dev and also in prod (after build) environments. Please try it and give the feedback. Of course, if you have any question, feel free to ask :) |
This is actually a problem with many modules when NextJS tries to do the server-side rendering and they are components that rely on being rendered in the browser. There's a way around this documented in NextJS docs which consists on using a dynamic import disabling server-side rendering: import dynamic from 'next/dynamic'
const AsyncApi = dynamic(import('@asyncapi/react-component'), { ssr: false }) // Async API cannot be server-side rendered
export const FooComponent = () => {
return <AsyncApi {...} />
} |
This issue has been automatically marked as stale because it has not had recent activity 😴 |
Any suggestions where we should go with this issue, document a workaround, or any other ideas? |
I haven't found a fully working approach in pure react/nextjs, but @magicmatatjahu suggested using the web component instead. I haven't tried it though. |
I believe this issue is beyond the scope of just next. I'm finding the same error message when running our Mocha test suite. When running from the browser, everything works as intended. |
Also, I attempted to convert to the web-component and found the same error message. |
Has there been any updates, movement, or visibility on this, please? |
@SteveSonoa so far we just do not have an idea how to fix it and suggest web component. Since you say web component also causes issues we need to check, but for this, to move forward we need some sample project from your side, best on code sandbox, so we can see the problem in a reproducable environment |
The only solution I've found is to use |
Same issue found when I use |
hey, folks. The same problem when I use the |
also an issue when using |
@YujiNNakashima @acelaya @SteveSonoa @nandorojo Huge sorry for waiting, but we didn't forget about this issue. We worked on unification react-component with our html-template. You can read more here. We fixed mentioned problem in issue and also this issue in the npm i @asyncapi/react-component@next Here is a list with changes (with some breaking changes, and by this we go with // Import component using `dynamic` helper
import dynamic from 'next/dynamic';
import "@asyncapi/react-component/styles/default.min.css";
// Import component without SSR/SSG
const AsyncAPIComponent = dynamic(() => import('@asyncapi/react-component/browser'), { ssr: false });
export default function AsyncAPIPreview ({ schema, config }) {
// Render on the browser only
if (typeof navigator === 'undefined') return null;
return schema && <AsyncAPIComponent schema={schema} config={config} />;
} The main problem why we have The above solution isn't compatible with SSR and with SSG, because component is only rendered in browser. To have fully support for SSR and SSG you must do following things:
npm i @asyncapi/parser
import { parse } from "@asyncapi/parser";
// version of component without parser, fully tree shakable
import { AsyncApiComponentWP } from "@asyncapi/react-component";
import '@asyncapi/react-component/styles/default.min.css';
export default function Home({ asyncapi }) {
return (
<div className="container">
<AsyncApiComponentWP schema={asyncapi} />
</div>
)
}
// This function gets called at build time
export async function getStaticProps() {
// example AsyncAPI spec written as string in YAML, you can e.g. fetch schema from db
const doc = `
asyncapi: '2.0.0'
info:
title: Example
version: '0.1.0'
channels:
example-channel:
subscribe:
message:
payload:
type: object
properties:
exampleField:
type: string
exampleNumber:
type: number
exampleDate:
type: string
format: date-time
`;
// parse and stringify spec. NextJS writes the returned props from this function to separate `.json` file to use it in hydration on browser.
const asyncapi = await parse(doc);
// stringify doesn't work with circular references in spec. Please have it in mind.
const stringified = JSON.stringify(asyncapi.json());
return {
props: {
asyncapi: stringified,
},
}
} We don't have currently documented above solution for SSR and SSG, because we have problems with circular references, you can read about it here. But "standalone" solution is faster and only includes about 150kb from package in final rendered page in opposite with "dynamic" component with about 800kb onboard. Again, sorry for waiting and let us know if everything works, and of course, if you have any questions, feel free to ask :) EDIT: Anticipating the questions. Probably we won't solve mentioned problem in issues related to NextJS in old version of component (in 0.X.Y versions), because we want focus on "new" component. |
@dukeliberal @sstecnologiainformacao @glend1 Thanks for reporting issues, but it's not related to our component 😅 You can create issue for that in related repos. probably mentioned by you packages have only |
Hey @magicmatatjahu, I'm testing the new version and overall, it looks amazing. I love the new look and feel. Some feedback I can provide after starting to use it on my Next.JS app:
|
@acelaya Thanks for awesome feedback, I really appreciate that! 🤗
Yes, we know about that, but there is a problem that TS at the moment doesn't support
We also know about that but we we wanted to prerelease the component so people could give feedback and if founded bugs with logic and styles will be fixed then we will go to 1.0.0 (I don't know when this will happen). Of course 1.0.0 will also included custom theming etc. Sorry for reseting styles, I always tested new component on "fresh" projects, so I didn't notice this reseting. Most likely there is a flag in tailwind to disable reseting. Thanks for the information!
Awesome that you fixed problem. To be honest, I also tested component in NextJS (of course fresh project) and everything worked as expected, also build and serve this build. Maybe some bug in your version of NextJS? Also related to stylesheet, you can try add our stylesheet as first stylesheet in page, the maybe another will "disable" the reseting :) Thanks for feedback again! |
I close issue, if any of the commenters here will have problems, please reopen or create a new issue. We will also remember about feedback :) |
Description
I'm trying to render the AsyncApi react component in a NextJS app, but it crashes, displaying the error above:
Maybe there is a problem of compatibility between CommonJS modules and ESModules
Expected result
It should render the component
Steps to reproduce
you can easily reproduce the error by installing a simple next app:
Troubleshooting
I was able to compile the app by dynamically importing the component with ssr:false, but the component does not render.
import dynamic from 'next/dynamic'
const AsyncApiComponent = dynamic(() => import('@kyma-project/asyncapi-react'), { ssr:false})
The text was updated successfully, but these errors were encountered: