Configuring and debugging build systems is often time consuming, stressful, and the end is often unsatisfying. If your webpack build is failing but not telling you why, then hopefully I can help you get back to coding. Tl;dr: use the --display flag with the CLI if you need to see output.
In my case, I was building a new React app from scratch, but when I tried to finally build the bare bones of it, I saw this:
|
> node node_modules/webpack/bin/webpack.js npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! poker-react@1.0.0 deploy: `node node_modules/webpack/bin/webpack.js` npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the poker-react@1.0.0 deploy 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! /home/ks/.npm/_logs/2018-05-08T14_18_25_667Z-debug.log |
That's not very helpful! A bundle.js file was being created, even though webpack failed; of course I had to get to the bottom of this.
The community's workarounds
I came across this discussion on GitHub, Webpack fails only when run through npm run
command. The original poster said that the build actually succeeds if run directly from the command line. I tried it, and it seemed to solve the problem - it would create a bundle.js just like before, but this time no error messages!
|
node node_modules/webpack/bin/webpack.js |
Additionally, @michaelalbers(https://github.com/michelalbers) had this idea:
"I just added -f to the npm run command. It seems webpack outputs to stderr for some reason and npm interprets that as exit code 2. Weird."
This worked as well, npm happily executed webpack, with just an ominious warning at the beginning,
|
npm WARN using --force I sure hope you know what you are doing. |
Though it seems that we're now able to build without errors, both with and without npm, something is amiss. We shouldn't have to force npm to continue through errors, and it still doesn't make sense that webpack would only fail when run through npm. Accepting the use of --force to workaround the webpack build issue would be very dangerous, even more so because we don't even know <i>what</i> errors we are skipping.. Even if you are happy to skip the errors you see today, you may introduce another error in the future, and you might not know right away because npm isn't reporting errors to you anymore.
Finding a flag for build information
I was confused why webpack wasn't giving any kind of output at all, no errors or error log to be found. Luckily, there is a --display flag for the CLI with following options: verbose, detailed, normal, minimal, errors-only, none
Running with the --display flag
|
node node_modules/webpack/bin/webpack.js --display=verbose |
Showed me that webpack actually had a good reason for exiting with status 2 before!
|
ERROR in ./src/index.jsx Module not found: Error: Can't resolve './store/configureStore' |
After resolving this and other reported errors, webpack was building without complaint from npm. It's important as a developer to have the desire to get to the bottom of an issue, even if you have a solution that seems to work around the issue. Of course, it's typical not to have the freedom to follow your desires. Sometimes these rabbit holes of curiosity can endure longer than a manager's patience.