Skip to content
This repository has been archived by the owner on Jul 6, 2021. It is now read-only.

SyntaxError when importing 3rd party component (with CSS file) #267

Closed
mtree opened this issue Sep 19, 2018 · 12 comments
Closed

SyntaxError when importing 3rd party component (with CSS file) #267

mtree opened this issue Sep 19, 2018 · 12 comments

Comments

@mtree
Copy link

mtree commented Sep 19, 2018

I'm just trying some basic integration with AWS Amplify. I don't really need these styles so I consider ability to ignore them as a problem solver - but I haven't found any working solution yet... Error occurs when compiling for the client.

$ npm list next @zeit/next-css
/home/ec2-user/environment/tm-nextjs-starter
├── @zeit/next-css@0.2.1-canary.2 
└── next@7.0.0-canary.20
module.exports = withCSS({
  cssModules: false
});
import { Authenticator, Greetings } from 'aws-amplify-react'
// removing below line does not fix the problem
require('@aws-amplify/ui/dist/style.css')
...
/home/ec2-user/environment/ter/node_modules/@aws-amplify/ui/dist/style.css:1
(function (exports, require, module, __filename, __dirname) { :root {
                                                              ^

SyntaxError: Unexpected token :
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/ec2-user/environment/tm-nextjs-starter/node_modules/aws-amplify-react/dist/Amplify-UI/Amplify-UI-Components-React.js:18:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
@romainquellec
Copy link

any news on this ?

@timneutkens
Copy link
Member

The code is open-source, feel free to investigate issues yourself too.

@phoenixbox
Copy link

phoenixbox commented Oct 4, 2018

@mtree I think when on next 7 you will need to use a next-css version >= 1.*

If you are on next 6 you can use the css 0.2 version, remove that cssModules: false param and include this in the _document

<link rel="stylesheet" href="/_next/static/style.css" />

I had a similar issue recently when using react-dates and trying to import its css for the datepicker

import "react-dates/lib/css/_datepicker.css";

@louiskhenghao
Copy link

louiskhenghao commented Oct 23, 2018

I'm having the exact same issue, did anyone managed to overcome this?
Im using next@^7.0.2 with @zeit/next-css@^1.0.1 and @zeit/next-sass@^1.0.1

Thanks in advance!

@mrcoles
Copy link

mrcoles commented Nov 7, 2018

I found this? https://spectrum.chat/thread/ba3668b1-f0b1-42a6-9c71-d7d9d3b67f04?m=MTU0MDMxMjAyODUyMA==

Put this somewhere near the top of your next.config.js to fix the issue:

if (typeof require !== "undefined") {
 require.extensions[".less"] = () => {};
 require.extensions[".css"] = (file) => {};
}

@sakhmedbayev
Copy link

@mrcoles thanks for posting your solution! Can you help me here? Here is how my next.config.js look like


module.exports = {
  webpack: config => {
    if (typeof require !== "undefined") {
      require.extensions[".less"] = () => {};
      require.extensions[".css"] = file => {};
    }
    // Fixes npm packages that depend on `fs` module
    
    config.node = {
      fs: "empty",
    };
    return config;
  }
};

and I still get the following error:

./node_modules/@aws-amplify/ui/dist/style.css 13:0
Module parse failed: Unexpected token (13:0)
You may need an appropriate loader to handle this file type.
|  * and limitations under the License.
|  */
> :root {
| 
|   /* Colors */

@mrcoles
Copy link

mrcoles commented Jan 11, 2019

@sakhmedbayev at the top of the file, not the top of your exports. For example, with the project I was doing, my full next.config.js file looked like this:

const withCSS = require("@zeit/next-css");
const withSass = require("@zeit/next-sass");
const withMDX = require("@zeit/next-mdx")();

// https://spectrum.chat/thread/ba3668b1-f0b1-42a6-9c71-d7d9d3b67f04
if (typeof require !== "undefined") {
  require.extensions[".less"] = () => {};
  require.extensions[".css"] = file => {};
}

const _cfg = (cfg, extra) => Object.assign(cfg || {}, extra);

const _css = cfg => withCSS(_cfg(cfg, {}));
const _sass = cfg => withSass(_cfg(cfg));
const _mdx = cfg =>
  withMDX(
    _cfg(cfg, {
      webpack: (config, options) => {
        config.module.rules.push({
          test: /\.ya?ml$/,
          use: "js-yaml-loader"
        });
        return config;
      }
    })
  );

// combine uses reduce, so left to right fns are inner to outer wrapping
const _combine = fns => fns.reduce((result, fn) => fn(result), {});

module.exports = _combine([_mdx, _sass, _css]);

I haven’t looked at this in a while, but I was also unhappy with how you have to wrap everything in a very nested way and tried setting up something with the _cfg and _combine functions, so I could just put each one in a list. This is a huge digression, but looking at this right now, I think I should have written combine to accept an initial config, then I wouldn’t need to do the silly nesting about with _mdx, e.g.,

const _combine = (fns, cfg) => fns.reduce((result, fn) => fn(result), cfg || {});

module.exports = _combine([_mdx, _sass, _css], {
  webpack: (config, options) => {
    config.module.rules.push({
      test: /\.ya?ml$/,
      use: "js-yaml-loader"
    });
    return config;
  }
});

You can ignore all this extra stuff and just move the relevant code to the top of our file outside of the export 😉

@sakhmedbayev
Copy link

Thank you a lot, @mrcoles! It works great even with next.js with Typescript!

@ngocketit
Copy link

@mtree Does the fix @mrcoles posted above help you? It didn't seem to work for me.

@chmelevskij
Copy link

chmelevskij commented Mar 28, 2019

@ngocketit @mrcoles solution with require only partially solves the problem, at least for me when using amplify it would load the components but not the styles.

I've got it working eventually by matching the names of the classes.

const withTypescript = require('@zeit/next-typescript')
const withOffline = require('next-offline')
const withCSS = require('@zeit/next-css');
const withPlugins = require('next-compose-plugins');

if (typeof require !== "undefined") {
  require.extensions[".css"] = () => {};
}

module.exports = withPlugins([
  [withCSS, {
    cssModules: false, // setting to false to prevent any extra stuff added to the class name apart of localIdentName
    cssLoaderOptions: {
      importLoaders: 1,
      localIdentName: "[name]__[local]___[hash:base64:5]", // this was taken from amplify ui webpack config
    }
  }],
  [withTypescript],
  [withOffline]
]);

@ruxandrafed
Copy link

ruxandrafed commented Mar 30, 2019

Hello! I ran into the a similar problem and was able to make it work by either using the solution from the spectrum thread, or adding require('ignore-styles') at the top of my server.js.

Everything seems to work well locally in either dev or prod mode, however I would like to be able to export this to static HTML. When I try to export though, this happens:


Creating an optimized production build ...

Compiled successfully.

 ┌ /
 ├ /_app
 ├ /_document
 ├ /_error
 └ /about

> using build directory: /<redacted>/.next
  copying "static" directory
  copying "static build" directory
> No "exportPathMap" found in "next.config.js". Generating map from "./pages"
  launching 3 threads with concurrency of 10 per thread
/<redacted>/node_modules/@tds/core-box/dist/index.css:1
(function (exports, require, module, __filename, __dirname) { .TDS_Box-modules__inline___jTHcz {
                                                              ^

SyntaxError: Unexpected token .
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/<redacted>/node_modules/@tds/core-box/index.cjs.js:1:63)
/<redacted>/node_modules/@tds/core-box/dist/index.css:1
(function (exports, require, module, __filename, __dirname) { .TDS_Box-modules__inline___jTHcz {
                                                              ^

SyntaxError: Unexpected token .
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/<redacted>/node_modules/@tds/core-box/index.cjs.js:1:63)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! create-next-example-app@ export: `NODE_ENV=production next build && next export -o out`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the create-next-example-app@ export script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /<redacted>/.npm/_logs/2019-03-30T20_35_16_855Z-debug.log

Error [ERR_IPC_CHANNEL_CLOSED]: channel closed
    at process.target.send (internal/child_process.js:588:16)
    at work (/<redacted>/node_modules/next/dist/export/worker.js:39:21)
    at <anonymous>
(node:56183) UnhandledPromiseRejectionWarning: Error [ERR_IPC_CHANNEL_CLOSED]: channel closed
    at process.target.send (internal/child_process.js:588:16)
    at process.on (/<redacted>/node_modules/next/dist/export/worker.js:47:17)
    at <anonymous>
(node:56183) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:56183) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The minute I remove the component as a dependency, export works just fine ..

I have tried all the things, nothing works. Any tips, please?

@timneutkens
Copy link
Member

Hi, thanks for creating an issue. We currently recommend using nextjs.org/docs/basic-features/built-in-css-support as the plugins have been deprecated in favor of the built-in support.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants