webpack warns about importing a named export from a JSON file
Should not import the named export 'version' (imported as 'version') from default-exporting module (only default export is available soon)Short answer
In webpack 5 a JSON file has one default export: the whole object. Named imports such as import { version } from './package.json' are not part of the specification, so webpack warns, and fails the build if the importing file is strict ESM. Import the default and read the property, or enable the json parser option namedExports.
Fix it fast
1Find out which cause you have
Find the import that points at a .json file and uses curly braces.
grep -rn --include=*.js --include=*.ts --include=*.jsx --include=*.tsx -E "import \{[^}]*\} from '.*\.json'" src2Match what you see, then jump to its fix
What does the error look like?
Should not import the named export 'version' (imported as 'version') from default-exporting module (only default export is available soon)from default-exporting module (only default export is available)
Cause 1 of 2
A named import from a JSON file
1Check
The webpack 5 migration guide says named exports from JSON modules are not supported by the new specification, and webpack warns.
grep -rn "package.json" src | head2Fix
Import the default export and use the property from it. The migration guide also lists a codemod that rewrites these imports for you.
// before
import { version } from './package.json';
// after
import pkg from './package.json';
console.log(pkg.version);
// optional codemod
// npx codemod webpack/v5/json-imports-to-default-imports3Verify
Rebuild. The warning no longer lists this file.
npx webpackConfirmed by webpack.js.org
Cause 2 of 2
You want to keep the named-import style across many files
1Check
Check your webpack version. The namedExports option of the JSON parser is available from 5.103.0.
npx webpack --version2Fix
Turn on named exports for JSON of object type in the module parser options.
// webpack.config.js
export default {
module: {
parser: {
json: {
namedExports: true,
},
},
},
};3Verify
Rebuild. The imports like import { myField } from './file.json' resolve.
npx webpackConfirmed by webpack.js.org
How do I stop it coming back?
- Import JSON through its default export in new code.
- Treat build warnings as errors in CI so an import like this is caught early.
- Upgrade webpack deliberately and read its release notes before the bump.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix