Maaz Zindani

Loading the index…

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

Next.js stops the build because a component imports a global stylesheet

ErrorGlobal CSS cannot be imported from files other than your Custom <App>

Short answer

In the Pages Router a plain .css file counts as global, and Next.js only accepts global CSS imported in pages/_app. The error names the file that imports one somewhere else. Move that import to _app.js, or rename the file to .module.css so its styles are scoped to the component that imports it.

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

Fix it fast

  1. 1Find out which cause you have

    Find every place that imports a stylesheet. The one outside pages/_app is the file the build is complaining about.

    grep -rn "import '.*\.css'" pages components src --include=*.js --include=*.jsx --include=*.ts --include=*.tsx
  2. 2Match what you see, then jump to its fix

    1. 1The import line sits in pages/index.js, a component file or a layout wrapperA page or component imports a global .css fileFix
    2. 2The stylesheet only styles a single component or pageThe styles belong to one component, not to the whole siteFix
    3. 3The import is from node_modules, for example a dialog or carousel packageA third-party component needs its own stylesheetFix

What does the error look like?

  • Global CSS cannot be imported from files other than your Custom <App>
  • Global CSS Must Be in Your Custom <App>

Cause 1 of 3

A page or component imports a global .css file

1Check

Global stylesheets apply to the whole site, so Next.js limits where they can be imported to avoid ordering problems and side effects. Look for imports of plain .css files outside pages/_app.

ls pages/_app.* 2>/dev/null || echo "no _app file yet"

2Fix

Create pages/_app.js if you have none, and import every global stylesheet there.

// pages/_app.js
import '../styles.css'

export default function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

3Verify

Remove the other import and run the build again.

npm run build

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

The styles belong to one component, not to the whole site

1Check

If the rules are meant for one component, a global file is the wrong tool. CSS Modules scope the class names to the file that imports them.

ls styles/

2Fix

Rename the file to name.module.css and import it as an object.

// components/Card.js
import styles from './Card.module.css'

export default function Card() {
  return <div className={styles.card}>Hello</div>
}

3Verify

The build passes and the class name in the HTML is generated, not the original.

npm run build

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

A third-party component needs its own stylesheet

1Check

Since Next.js 9.5.4 a CSS file from node_modules may be imported anywhere. Only your own global CSS is restricted. So the error means the path you import is inside the project, not in node_modules.

grep -rn "\.css'" components | grep -v node_modules

2Fix

Import the package's CSS from the component that uses it. Keep global libraries such as a CSS framework in pages/_app.

// components/ExampleDialog.js
import '@reach/dialog/styles.css'

3Verify

Build the project again.

npm run build

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Keep all global CSS imports in pages/_app.js, and use .module.css for everything else.
  • Name modules after the component they style, so the scoping is obvious in review.

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.