question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SyntaxError for @import when building with NextJS 9, not during development

See original GitHub issue

Bug 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

  1. Clone repository
  2. npm install
  3. next => working fine
  4. next 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:closed
  • Created 4 years ago
  • Reactions:19
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
taylorhaydukcommented, Aug 14, 2019

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:

  webpack: (config, { isServer }) => {
    if (isServer) {
      const antStyles = /antd\/.*?\/style.*?/
      const origExternals = [...config.externals]
      config.externals = [
        (context, request, callback) => {
          if (request.match(antStyles)) return callback()
          if (typeof origExternals[0] === 'function') {
            origExternals[0](context, request, callback)
          } else {
            callback()
          }
        },
        ...(typeof origExternals[0] === 'function' ? [] : origExternals),
      ]

      config.module.rules.unshift({
        test: antStyles,
        use: 'null-loader',
      })
    }
    return config
  },
10reactions
Kombustorcommented, Aug 2, 2019

I actually got it working by adding the isServer block from this example to my next.config.js! @taylorhayduk @farhadniaz

But this is still only a workaround and not a fix, since it’s working without it in next 8.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found