nx: Global CSS cannot be imported from files other than your Custom .
## Current Behavior
Given a generated NextJs app that imports a generated shared react lib with
a default component that uses either css or cssModule imports fails throwing an error.
Expected Behavior
Given a default NextJs app that imports a shared react lib with a default component that uses either css or cssModule imports should just work, no need to adapt next.config.js as both imports are supported by default in NextJs.
Is this a regression? Not sure, I am able to reproduce with brand new workspace and latest version of every plugin.
Steps to Reproduce
- npx create-nx-workspace test-bug [choose empty setup]
- npm i -D @nrwl/next
- nx generate @nrwl/next:application --name=test-bug
- nx generate @nrwl/react:library --name=shared-library
- in the generated nextjs app add the shared component to the render function
- try run the app
nx serve test-bug - try renaming components css file to =>
xxx.module.cssand it’s corresponding import
Failure Logs
warn - Detected next.config.js, no exported configuration found. https://err.sh/vercel/next.js/empty-configuration
info - automatically enabled Fast Refresh for 1 custom loader
info - Using external babel configuration from /Users/danielq/Projects/test-workspace/apps/test-bug/.babelrc
event - compiled successfully
[ ready ] on http://localhost:3000
event - build page: /next/dist/pages/_error
wait - compiling...
event - compiled successfully
event - build page: /
wait - compiling...
error - /Users/danielq/Projects/test-workspace/libs/shared-components/src/lib/shared-components.css
Global CSS cannot be imported from files other than your Custom <App>. Please move all global CSS imports to pages/_app.tsx. Or convert the import to Component-Level CSS (CSS Modules).
Read more: https://err.sh/next.js/css-global
Location: ../../libs/shared-components/src/lib/shared-components.tsx
Environment
nx : Not Found @nrwl/angular : Not Found @nrwl/cli : 10.2.1 @nrwl/cypress : 10.2.1 @nrwl/eslint-plugin-nx : 10.2.1 @nrwl/express : Not Found @nrwl/jest : 10.2.1 @nrwl/linter : 10.2.1 @nrwl/nest : Not Found @nrwl/next : 10.2.1 @nrwl/node : Not Found @nrwl/react : 10.2.1 @nrwl/schematics : Not Found @nrwl/tao : 10.2.1 @nrwl/web : 10.2.1 @nrwl/workspace : 10.2.1 typescript : 3.9.7
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 11
- Comments: 30 (10 by maintainers)
This will come in 10.4, which will land before American Thanksgiving. We’ll cut a beta release before stable.
Got the similar error when selecting sass for styling while generating a component- nx g component PartyPopper --project=demo --style=scss i dont next.js let you do import ‘./party-popper.scss’; inside components! it has to be css modules .module.css
this is what i had to do to get it working-
code generator should be updated to updated the generate code.
I am happy to create a PR ,but I was not able to find the correct file to make the change inside packages/next folder. @jaysoo ?
workspace lib use importing CSS required by a third party component, you can do so in your component. For example:
But there is a bug, error log:
I moved
import '@reach/dialog/styles.css'to file pages/_app.tsx and there was no error, but I think this is not goodPR for the first part is ready – the second change for schematics isn’t required but more of a quality of life change.
For those that want a workaround before the next release, you can use this in your app’s
next.config.js.Hey folks, after a quick chat with @mandarini we’ve decided to do the following:
withNxfunction fornext.config.jsto use, which will allow workspace libs to use css modules. This config enhancer will also help with future modifications based on Nx’s needs.--cssModulesoption for React component schematic (this is used by Next.js schematics underneath the hood). This will allow app components/pages, as well as libs, to generate with*.module.cssor*.module.scssby default.It’s strange, but after I pulled down my repo and run npm install now it works. I will look when I will create new nx project again and if I will have mistake I will remove folder node_module and I will reinstall packages.
I see that this issue is closed, but there is a pending item to review the use of React component libraries which are not using CSS modules. Where can the status of that be tracked?
I see the code ais already in the latest build ,not sure why it is no working? @jaysoo ?
<%= style !== ‘styled-jsx’ ?
import styles from './${fileName}.module.${style}';: ‘’ %>https://github.com/nrwl/nx/blob/10.3.1/packages/next/src/schematics/application/files/pages/__fileName__.tsx__tmpl__