Search code examples
reactjswebpackserverlessnext.jsaws-amplify

Trouble getting Next.js to work with aws-amplify


It does not seem that Next.js is able to read the required css file in node_modules.

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 */

Links that provide potential solutions:

https://github.com/aws-amplify/amplify-js/issues/1535

https://github.com/aws-amplify/amplify-js/issues/2230

https://github.com/zeit/next-plugins/issues/267

The proposed solution is to put this at the top of a next.config.js file:

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

I have not been able to get this proposed fix to work, and am wondering if anybody has a greater understanding of the actual issue/setting up a next.config.js file with the proposed solution.

Thanks in advance.


Solution

  • Create a next.config.js file at the root of your project directory with the following:

    const withCSS = require("@zeit/next-css");
    
    if (typeof require !== "undefined") {
      require.extensions[".less"] = () => {};
      require.extensions[".css"] = file => {};
    }
    
    // next.config.js is not transformed by Babel. So you can only use javascript features supported by your version of Node.js.
    
    module.exports = withCSS({
      webpack: (config, { buildId, dev, isServer, defaultLoaders }) => {
        // Perform customizations to webpack config
        // Important: return the modified config
        return config;
      },
      webpackDevMiddleware: config => {
        // Perform customizations to webpack dev middleware config
        // Important: return the modified config
        return config;
      }
    });
    

    This answer seems to be on par with the solution from https://github.com/aws-amplify/amplify-js/issues/1535 by the user ngocketit.