'react npm run build fails with Unexpected '/'

The react app is completely fine when I run "npm start" and everything working without errors. But when I try to build the app, it gives the following error:

redhat-server % npm run build

> [email protected] build
> react-scripts build && gulp licenses

Creating an optimized production build...
Failed to compile.

static/css/main.340bdb3c.css from Css Minimizer plugin
Error: Unexpected '/'. Escaping special characters with \ may help.

I found out that error coming from my index.js file which is the starting point. I have imported CSS files like this:

import './assets/css/animate.min.css';
import "./assets/scss/light-bootstrap-dashboard-react.scss?v=2.0.0";
import "./assets/css/demo.css";

The "/" is the problem here. At the beginning of each import's path. I have tried ".//" or "/" or "//" - with any of these, it would give me the error file not found error.

I also tried without the "/".Then it gives this error.

Module not found: Error: Can't resolve 'assets/css/animate.min.css' in '/local/home/app/src'
Did you mean './assets/css/animate.min.css'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules, /local/home/app/node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.


Solution 1:[1]

This problem happened for me also, and i solved it by checking which css file is generating this issue ( the main.css in my case), then i found that it was all about a mistake in a comment( instead of putting /**/, i put **/, and for that it said "Unexpected /" ).
Try this method if npm build steal fails, or if you've found the solution share it with us to help others.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Moussa Riad