This error occurred for us because our babel configuration for jest tests was using next/babel
, while nextjs 14 utilizes @babel/plugin-syntax-import-assertions. Jest, however, relies on the plugin "babel-preset-current-node-syntax" version "1.1.0", which also employs @babel/plugin-syntax-import-assertions.
The proposal to replace import assertions with import attributes offers greater flexibility:
To accommodate both old and new syntax, you can refer to this link:
The clash arises when Nextjs 14 incorporates import assertions, but by using next/babel preset in babel.config.js and applying jest-babel on top of it, import attributes are introduced through "babel-preset-current-node-syntax" version "1.1.0".
However, the transition from import assertions to babel-plugin-syntax-import-attributes with deprecatedassertsyntax has been featured in the latest next canary update:
We also noticed that resolutions property in yarn's package.json did not impact the installed version of @jest/core/node_modules/babel-preset-current-node-syntax.
To resolve this issue, we took the following steps:
yarn install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="15213c35171310233e38173e37171d33331b25">[email protected]</a>
We removed the caret operator and pinned the version like so:
"babel-preset-current-node-syntax": "1.0.0",
Subsequently, we implemented the following script:
import { exec } from '@utils/scripting';
import { existsSync } from 'node:fs';
const JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC = 'node_modules/@jest/core/node_modules/babel-preset-current-node-syntax';
const VERSION_ONE_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC = 'node_modules/babel-preset-current-node-syntax';
export const run = (): void => {
if (!existsSync(`${process.cwd()}/${JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC}`)) {
throw new Error(`The file ${JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC} does not exist.
You probably upgraded jest and now you have to downgrade or read the comments in deploy/bin/patch-jest-import-assertions.ts`);
}
exec(`rm -rf ${JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC}`);
exec(`cp -R ${VERSION_ONE_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC} ${JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC}`);
};
With these adjustments, next/babel will handle the addition of import attributes or import assertions as needed, without affecting our usage since we don't utilize either syntax.