next.js: Compilation error - transform-react-jsx: pragma has been set but pragmaFrag has not been set
Bug report
Describe the bug
My build on Zeit is erroring with the following stack trace:
17:35:38.096
Creating an optimized production build...
17:35:38.601
[webpack] Building release "1.0.0-beta.1_zBuNtNkFEHR1aBTmOhBpQ"
17:35:55.427
Failed to compile.
17:35:55.428
./src/pages/_error.tsx
17:35:55.428
Error: /zeit/198ac8ea/src/pages/_error.tsx: transform-react-jsx: pragma has been set but pragmaFrag has not been set
17:35:55.428
> Build error occurred
17:35:55.429
Error: > Build failed because of webpack errors
17:35:55.429
at build (/zeit/198ac8ea/node_modules/next/dist/build/index.js:10:900)
17:35:55.466
error Command failed with exit code 1.
17:35:55.466
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
17:35:55.476
Error: Error: Exited with 1
17:35:55.477
at ChildProcess.<anonymous> (/zeit/2b7c515595ce4419/.build-utils/node_modules/@now/build-utils/dist/index.js:31347:24)
17:35:55.477
at ChildProcess.emit (events.js:223:5)
17:35:55.477
at ChildProcess.EventEmitter.emit (domain.js:475:20)
17:35:55.477
at maybeClose (internal/child_process.js:1021:16)
17:35:55.477
at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)
17:35:55.842
worker exited with code 20 and signal null
17:35:57.147
Done with "package.json"
I can’t explain it, I haven’t changed anything related to node modules. Here is the source code of pages/_error.tsx:
import * as Sentry from '@sentry/node';
import { NextPageContext } from 'next';
import Error, { ErrorProps as NextErrorProps } from 'next/error';
import React from 'react';
// XXX See https://github.com/zeit/next.js/blob/canary/examples/with-sentry-simple/pages/_error.js
const NRNError = (props: NRNErrorProps): JSX.Element => {
const { statusCode, isSSRReadyToRender, err, children = null } = props;
if (!isSSRReadyToRender && err) {
// getInitialProps is not called in case of
// https://github.com/zeit/next.js/issues/8592. As a workaround, we pass
// err via _app.js so it can be captured
Sentry.captureException(err);
}
return (
<>
{
// Render the children if provided, or return the native Error component from Next
children ? (
children
) : (
<Error statusCode={statusCode} />
)
}
</>
);
};
NRNError.getInitialProps = async (props: NextPageContext): Promise<ErrorProps> => {
const { res, err, asPath } = props;
// @ts-ignore
const errorInitialProps: ErrorProps = await Error.getInitialProps({ res, err });
// Workaround for https://github.com/zeit/next.js/issues/8592, mark when
// getInitialProps has run
errorInitialProps.isSSRReadyToRender = true;
if (res) {
// Running on the server, the response object is available.
//
// Next.js will pass an err on the server if a page's `getInitialProps`
// threw or returned a Promise that rejected
if (res.statusCode === 404) {
// Opinionated: do not record an exception in Sentry for 404
return { statusCode: 404, isSSRReadyToRender: true };
}
if (err) {
Sentry.captureException(err);
return errorInitialProps;
}
} else {
// Running on the client (browser).
//
// Next.js will provide an err if:
//
// - a page's `getInitialProps` threw or returned a Promise that rejected
// - an exception was thrown somewhere in the React lifecycle (render,
// componentDidMount, etc) that was caught by Next.js's React Error
// Boundary. Read more about what types of exceptions are caught by Error
// Boundaries: https://reactjs.org/docs/error-boundaries.html
if (err) {
Sentry.captureException(err);
return errorInitialProps;
}
}
// If this point is reached, getInitialProps was called without any
// information about what the error might be. This is unexpected and may
// indicate a bug introduced in Next.js, so record it in Sentry
Sentry.captureException(
// @ts-ignore
new Error(`_error.js getInitialProps missing data at path: ${asPath}`),
);
return errorInitialProps;
};
export type NRNErrorProps = {
err: Error;
statusCode: number;
isSSRReadyToRender: boolean;
children?: React.ReactElement;
}
export type ErrorProps = {
isSSRReadyToRender: boolean;
} & NextErrorProps;
export default NRNError;
To Reproduce
No idea, everything was working fine until then.
Since I didn’t change anything on my side, it may be related to some node module, most potentially with transform-react-jsx babel plugin.
I don’t know what’s causing the issue.
Expected behavior
Should compile.
Workarounds
Use React.Fragment
Using <React.Fragment></React.Fragment> instead of <></> - See https://github.com/zeit/next.js/issues/11230#issuecomment-601816427
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 20
- Comments: 27 (9 by maintainers)
Commits related to this issue
- https://github.com/zeit/next.js/issues/11230 — committed to confluenza/confluenza by marcinczenko 7 years ago
- https://github.com/zeit/next.js/issues/11230 — committed to confluenza/confluenza by marcinczenko 7 years ago
- Trialing work around for `transform-react-jsx: pragma has been set but pragmaFrag has not been set` error (see https://github.com/zeit/next.js/issues/11230) — committed to WestpacGEL/GEL by molomby 7 years ago
- Replace `<>` shorthands with `<React.Fragment>` (#277) * Trialing work around for `transform-react-jsx: pragma has been set but pragmaFrag has not been set` error (see https://github.com/zeit/next.js... — committed to WestpacGEL/GEL by molomby 7 years ago
- fix: add workaround for weird issue (see https://github.com/vercel/next.js/issues/11230#issuecomment-601870626) for more info — committed to tomglennhs/updates by jasonappah 6 years ago
I just ran into the same thing. It looks like there’s a problem rendering shorthand fragments
<>..</>from that package.My temporary fix was to switch it all to
<React.Fragment></React.Fragment>and that seems to work right now.Ok, we have identified the bug. We will release a fix soon.
I have the same issue. To reproduce the problem you can checkout this repo that I created.
https://github.com/itsmylife/nextjs-pragmafrag-error
My workaround is this:
Install
@babel/plugin-transform-react-jsxas development dependency.Then, create a .babelrc file in the root of the project. Inside
.babelrcput the following:And with those you can keep using shorthand React.Fragment. But I think we should not have to do those.
Edit: I think that is related to new version of @babel/plugin-transform-react-jsx https://www.npmjs.com/package/@babel/plugin-transform-react-jsx It was published 4 hours ago.
@Vadorequest Solved my problem with
/* @jsxFrag React.Fragment */in my top level in index.js i havein my component file i have these
i get the error react is defined but never used but it compiles fine with no errors. hope that helps
It works! Thank you very much!
Sorry for the trouble yall, that was a fun debug session with the team 😛
Thanks for the help. It worked for me.
Yes, it should. New projects don’t have a lockfile yet so they got the broken version, but I’m not sure how existing projects could have stopped working.
Btw, we’ll look into adding an e2e test for Babel with next.js!
This should be fixed in
@babel/preset-reactand@babel/plugin-trasnform-react-jsx7.9.1: https://github.com/babel/babel/releases/tag/v7.9.1, please test it 🙏We need help debugging this issue: did you have a
/* @jsxFrag React.Fragment */comment in your code?We have found a regression that is only triggered when
/* @jsxFrag React.Fragment */is set, but I don’t see it neither in the OP neither in the repo posted by @itsmylife.FWIW this particular issue doesn’t seem related to the “mess”.
I think this ~is~ might be related to all the Babel mess right now: