Next.js stops the build because a component imports a global stylesheet
Global 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.
Fix it fast
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=*.tsx2Match what you see, then jump to its fix
- 1
The import line sits in pages/index.js, a component file or a layout wrapperA page or component imports a global .css fileFix - 2
The stylesheet only styles a single component or pageThe styles belong to one component, not to the whole siteFix - 3
The import is from node_modules, for example a dialog or carousel packageA third-party component needs its own stylesheetFix
- 1
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 buildConfirmed by nextjs.org
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 buildConfirmed by nextjs.org
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_modules2Fix
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 buildConfirmed by nextjs.org
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
Web apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 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
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fix