Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

webpack warns about importing a named export from a JSON file

ErrorShould 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.

  • Applies to webpack 5.103, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'" src
  2. 2Match what you see, then jump to its fix

    1. 1The message names an export such as 'version' and mentions a default-exporting module.A named import from a JSON fileFix
    2. 2Dozens of files import named fields from the same JSON file and a rewrite is not practical.You want to keep the named-import style across many filesFix

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 | head

2Fix

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-imports

3Verify

Rebuild. The warning no longer lists this file.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

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 --version

2Fix

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.