ChatGPT解决这个技术问题 Extra ChatGPT

where is create-react-app webpack config and files?

I create a ReactJS project with the create-react-app package and that worked well, but I cannot find webpack files and configurations.

How does react-create-app work with webpack? Where are the webpack configuration files located in a default installation with create-react-app? I'm unable to find configuration files in my project's folders.

I have not created an override config file. I can manage config settings with other articles but I want to find the conventional config file(s).


V
Vikram Thakur

If you want to find webpack files and configurations go to your package.json file and look for scripts

https://i.stack.imgur.com/enyQL.png

You will find that scripts object is using a library react-scripts

Now go to node_modules and look for react-scripts folder react-script-in-node-modules

This react-scripts/scripts and react-scripts/config folder contains all the webpack configurations.


I saw but i have not react-scripts dir
I just created an app using create-react-app and in my case there is a folder react-scripts in node_modules. can you share your package.json
This answer is no longer correct, NPM is now flat meaning all the modules are on the root node_modules directory
The most common use case would be to customize the project's webpack config. But if it is in node_modules it will be overwritten in every npm install, won't it?
k
klugjo

The files are located in your node_modules/react-scripts folder:

Webpack config:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/webpack.config.js

Start Script:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/start.js

Build Script:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/build.js

Test Script:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/test.js

and so on ...

Now, the purpose of CRA is not to worry about these.

From the documentation:

You don’t need to install or configure tools like Webpack or Babel. They are preconfigured and hidden so that you can focus on the code.

If you want to have access to the config files, you need to eject by running:

npm run eject

Note: this is a one-way operation. Once you eject, you can’t go back!

In most scenarios, it is best not to eject and try to find a way to make it work for you in another way.

If you need to override some of the config options, you can have a look at https://github.com/gsoft-inc/craco


I know these are hidden, but i wanna know where are these and how react-create-app handle this ability ?
Then you will probably have to look at the source code. I am not sure
@Mohammad checkout the source for 'react-scripts' you can find all the config there
Doesn't answer the question, but helped me regardless
Looks like Craco is currently having some issues with create-react-app 5. They've been working on patching this since January, but 5 months later and still not quite done github.com/gsoft-inc/craco/issues/378
j
jjbskir

A lot of people come to this page with the goal of finding the webpack config and files in order to add their own configuration to them. Another way to achieve this without running npm run eject is to use react-app-rewired. This allows you to overwrite your webpack config file without ejecting.


Is it still the preferred solution? There is this warning on rewired project: github.com/timarney/react-app-rewired#rewire-your-app- : "As of Create React App 2.0 this repo is "lightly" maintained mostly by the community at this point. ... Note: I personally use next.js or Razzle which both support custom Webpack out of the box."
@AnthonyKong thanks for this update, the situation is really bad... central upstream discussion appears to be at: github.com/facebook/create-react-app/issues/6303
r
ravibagul91

Assuming you don't want to eject and you just want to look at the config you will find them in /node_modules/react-scripts/config

webpack.config.dev.js. //used by `npm start`
webpack.config.prod.js //used by `npm run build`

W
Willian Mitsuda

Webpack config used by create-react-app is here: https://github.com/facebook/create-react-app/tree/master/packages/react-scripts/config


this should be the answer
There's a lot going on in that canned webpack.config.js. Imagine trying to build that up from scratch!
@AndrewFielden imagine when it doesn't do what you expect and you have to dig into it !
A
Alfrex92

You can find it inside the /config folder.

When you eject you get a message like:

 Adding /config/webpack.config.dev.js to the project
 Adding /config/webpack.config.prod.js to the project

H
Hassan Ajaz

Webpack configuration is being handled by react-scripts. You can find all webpack config inside node_modules react-scripts/config.

And If you want to customize webpack config, you can follow this customize-webpack-config


The instructions at the link labeled customize-webpack-config depend on react-app-rewired, which is mostly unmaintained according to its GitHub README.
v
valk

Ejecting is not the best option as well as editing something under node_modules. react-app-rewired is not maintained and has the warning:

...you now "own" the configs. No support will be provided. Proceed with caution...

Solution - use craco.


craco does not work with react-scripts 5.
@tutiplain do you know a tool that works with cra 5?
@JanisJansen I do not. I needed this because my project uses some LESS that is embedded somewhere deep in node_modules. I ended up using the less compiler directly and embedding the resulting stylesheet directly in the react code.
V
Vinayak humberi

Try ejecting the config files, by running:

npm run eject

then you'll find a config folder created in your project. You will find your webpack config files init.


Be aware that this is a major step that you can't easily come back from. This would be a pretty drastic solution to getting rid of a warning.
t
therightstuff

I'm using create-react-app with craco, and I was able to override my webpack configuration when updating to webpack 5 with the craco.config.js:

module.exports = {
    style: {
        postcssOptions: {
            plugins: [
            require('tailwindcss'),
            require('autoprefixer'),
            ],
        },
    },
    webpack: {
        configure: (webpackConfig, { env, paths }) => {
            // eslint-disable-next-line no-param-reassign
            webpackConfig.resolve.fallback = {
                fs: false,
            };
            return webpackConfig;
        },
    },
}

H
Humbledore

I know it's pretty late, but for future people stumbling upon this issue, if you want to have access to the webpack config of CRA, there's no other way except you have to run:

$ npm run eject

However, with ejection, you'll strip away yourself from CRA pipeline of updates, therefore from the point of ejection, you have to maintain it yourself.

I have come across this issue many times, and therefore I've created a template for react apps which have most of the same config as CRA, but also additional perks (like styled-components, jest unit test, Travis ci for deployments, prettier, ESLint, etc...) to make the maintenance easier. Check out the repo.


Craco works well. There are other that do similar things. So, there IS another way for a lot of scenarios.
Link to repo is broken.