SyntaxError for @import when building with NextJS 9, not during development
See original GitHub issueBug report
SyntaxError for less @import statement during bundling.
> next build
Creating an optimized production build ...
> Using external babel configuration
> Location: "/workspace/.babelrc"
Compiled successfully.
> Build error occurred
{ /workspace/node_modules/antd/lib/style/index.less:1
@import './themes/index';
^
SyntaxError: Invalid or unexpected token
at Module._compile (internal/modules/cjs/loader.js:721:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/workspace/node_modules/antd/lib/time-picker/style/index.js:3:1)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10) type: 'SyntaxError', '$error': '$error' }
Describe the bug
When using next
in development, everything just works fine. The theme under ./assets/antd-theme.less is applied and can be changed, the antd-components render just fine.
Using next build
for production usage throws the error above.
To Reproduce
Demo repository here: https://github.com/Kombustor/next-import-bug-demo
- Clone repository
npm install
next
=> working finenext build
=> Error
Expected behavior
To create the production build without throwing an error.
System information
- OS: ArchLinux
- Version of Next.js: 9.0.2
Additional context
I know it’s the same issue as in #7957 and #8054 but it was working perfectly fine like this in NextJS 8 (check out the next8 branch), without any hacky CSS webpack configuration etc. Please check the repository and how simple it’s configured compared to the solutions mentioned in these issues before closing mine.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:19
- Comments:13 (2 by maintainers)
Top Results From Across the Web
module-not-found - Next.js
A module not found error can occur for many different reasons: The module you're trying to import is not installed in your dependencies;...
Read more >SyntaxError: Cannot use import statement outside a module
So, we'll add our first script, build, in file package.json. ... SyntaxError: Cannot use import statement outside a module. and who've tried ...
Read more >SyntaxError: unterminated string literal - JavaScript | MDN
String literals must be enclosed by single ( ' ) or double ( " ) quotes. JavaScript makes no distinction between single-quoted strings...
Read more >How to Dynamically Import JavaScript with Import Maps
Build tools are an important part of the development experience, ... In this tutorial, you'll use import maps and JavaScript modules to ...
Read more >Use Mapbox GL JS in a React app | Help
Familiarity with React and front-end development concepts. ... "not ie 11" ... to import two stylesheets and the Mapbox GL JS map that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You’ll need to use the webpack config here if you’re using less by setting the babel import antd style to true (see by babelrc above). https://github.com/zeit/next.js/blob/master/examples/with-ant-design-less/next.config.js
If you aren’t using less, I think you should be able to use
const antStyles = /antd\/.*?\/style\/css.*?/;
below instead. This threw me for a loop for such a long time as I was using this incorrect-for-my-use-case regex.If you ARE using less (i.e. setting style to true in babel), try the following:
I actually got it working by adding the
isServer
block from this example to my next.config.js! @taylorhayduk @farhadniazBut this is still only a workaround and not a fix, since it’s working without it in next 8.