Maaz Zindani

Loading the index…

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

Next.js says Module parse failed: you may need an appropriate loader

ErrorModule parse failed: Unexpected token

Short answer

The bundler found a file it cannot read as JavaScript. Either you import a non-code file (svg, pdf, txt) with no loader for it, or a package in node_modules ships raw TypeScript or JSX that Next.js does not compile. Add a loader rule for the file type, or list the package in transpilePackages.

  • Applies to Next.js 15, Next.js 16, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the first line of the error. It names the file the bundler could not parse. A path ending in .svg, .pdf or .txt points to cause 1 or 2. A path inside node_modules points to cause 3.

    npx next build 2>&1 | grep -B2 -A8 "Module parse failed"
  2. 2Match what you see, then jump to its fix

    1. 1The failing path ends in .svg, .pdf, .txt or another data format.A non-JavaScript file is imported and no loader is set (Turbopack)Fix
    2. 2The same import fails only under webpack, for example when next dev or next build is started with the webpack bundler.The build uses webpack and has no rule for the file typeFix
    3. 3The failing path is inside node_modules and the line shown is TypeScript or JSX, for example a type annotation.A package in node_modules ships TypeScript, JSX or other sourceFix

What does the error look like?

  • Module parse failed: Unexpected token
  • You may need an appropriate loader to handle this file type

Cause 1 of 3

A non-JavaScript file is imported and no loader is set (Turbopack)

1Check

Find the import. Turbopack handles CSS and JavaScript on its own, but any other file type needs a rule in next.config.

grep -rnE "import .* from '.*\.(svg|pdf|txt)'" app src components 2>/dev/null

2Fix

To import a file as a URL, give its extension the asset type. To turn SVG files into React components, add the @svgr/webpack loader. Loaders must return JavaScript, and rules run in order.

// next.config.js
module.exports = {
  turbopack: {
    rules: {
      '*.pdf': { type: 'asset' },
      '*.svg': { loaders: ['@svgr/webpack'], as: '*.js' },
    },
  },
}

3Verify

Restart the dev server, then build.

npx next build

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

The build uses webpack and has no rule for the file type

1Check

Custom webpack settings live in the webpack function of next.config. Check whether one exists and whether it covers the extension.

grep -n "webpack" next.config.*

2Fix

Add a rule for the extension inside the webpack function and return the changed config. Install the loader first.

// next.config.js
module.exports = {
  webpack: (config) => {
    config.module.rules.push({
      test: /\.svg$/,
      use: ['@svgr/webpack'],
    })
    return config
  },
}

3Verify

Run the build with the same bundler that failed.

npx next build

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

A package in node_modules ships TypeScript, JSX or other source

1Check

Next.js does not compile code inside node_modules unless you ask it to. Open the failing file and the package's package.json main and exports fields.

cat node_modules/PACKAGE/package.json | grep -E '"(main|module|exports|types)"'
head -20 node_modules/PACKAGE/path/from/the/error.ts

2Fix

List the package by name in transpilePackages. Paths and globs are not accepted. A package cannot be in both transpilePackages and serverExternalPackages.

// next.config.js
module.exports = {
  transpilePackages: ['package-name', '@scope/pkg'],
}

3Verify

Rebuild. The error for that package is gone.

npx next build

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Prefer packages that publish compiled JavaScript with a types file.
  • Keep loader rules in next.config next to a comment saying which import needs them.
  • Put plain downloads such as PDFs in public and link them by URL instead of importing them.

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

Next step

Want this handled for good?

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