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.

Unable to override webpack configuration for a react application

See original GitHub issue

Summary

I have bootstrapped a react application in my workspace using:

nx generate @nrwl/react:application

I want to override the webpack.config.js to add a few more plugins

I noticed that in the workspace.json for my project there is already webpackConfig option

"architect": {
        "build": {
          "builder": "@nrwl/web:build",
          "options": {
            "webpackConfig": "@nrwl/react/plugins/webpack",

I followed the instructions mentioned here (which may be for projects which use the @nrwl/node or @nrwl/web) and pointed it to a custom version of my webpack.config which looks like this

const WebpackNotifierPlugin = require('webpack-notifier');

module.exports = (config, context) => {
  console.log(config);
  return {
    ...config,
    plugins: [
      ...config.plugins,
      new WebpackNotifierPlugin({title: 'Project build completed'}),
    ]
  };
};

but I get the error when i try to build my project nx run frontend:build:

ERROR in ./main.tsx 4:16
Module parse failed: Unexpected token (4:16)
File was processed with these loaders:
 * ../../../node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| import ReactDOM from 'react-dom';
| import App from './app/app';
> ReactDOM.render(<App />, document.getElementById('root'));

Can I get some help with this? Is this the recommended way to override the webpack.config for a react based application?

Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

60reactions
niki4810commented, Jun 15, 2020

I have got it working by requiring @nrwl/react/plugins/webpack.js and calling the config function with-in my custom webpack function:

const WebpackNotifierPlugin = require('webpack-notifier'); 
const nrwlConfig = require("@nrwl/react/plugins/webpack.js"); // require the main @nrwl/react/plugins/webpack configuration function.

module.exports = (config, context) => {
  nrwlConfig(config); // first call it so that it @nrwl/react plugin adds its configs, 
 
// then override your config.
  return {
    ...config,
    plugins: [
      ...config.plugins,
      new WebpackNotifierPlugin({title: 'Frontend Project build completed'}),
    ]
  };
};

Thanks to @cdaringe for the pointers.

3reactions
cbrwizardcommented, Sep 7, 2022

In my opinion, @niki4810’s solution should be added to official docs as it helped me to solve a nasty bug

Read more comments on GitHub >

github_iconTop Results From Across the Web

I can't modify webpack.config in create-react-app to install ...
I put two scripts (start.js and build.js) in a scripts folder in root directory (i.e. outside src). Then in package.json change the start...
Read more >
Overriding the Create-React-App Webpack Configuration ...
Create-React-App is a great tool to bootstrap React apps, but it offers only limited access to the configuration of the production build.
Read more >
Customize webpack configuration of React App created with ...
After installing react-app-rewired package, manually create config-overrides. js file in the root directory. And the last modification in ...
Read more >
The best webpack configurations for React applications
If you've struggled to configure webpack for your React app, look no further. We cover both generic and specific config needs.
Read more >
Creating a React app with Webpack - JavaScript Ramblings
Tip: If npx serve public fails for you with Must use import to load ES Module error, check your Node version and make...
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