【问题标题】:I can't modify webpack.config in create-react-app to install react-pdf我无法在 create-react-app 中修改 webpack.config 来安装 react-pdf
【发布时间】:2022-01-16 10:11:34
【问题描述】:

帮我安装 react-pdf 包 (https://github.com/diegomura/react-pdf) 创建 react 应用程序。我无法对 webpack.config 进行更改。我按照我从用户 River Twilight 那里找到的说明进行操作:How to update webpack config for a react project created using create-react-app?

根据安装react-pdf的说明

  1. 我跑npm install process browserify-zlib stream-browserify util buffer assert
  2. 在项目根文件夹中创建config-override.js
  3. 接下来您需要将以下行插入到配置中:

const webpack = require("webpack");

module.exports = {
  /* ... */

  resolve: {
    fallback: {
      process: require.resolve("process/browser"),
      zlib: require.resolve("browserify-zlib"),
      stream: require.resolve("stream-browserify"),
      util: require.resolve("util"),
      buffer: require.resolve("buffer"),
      asset: require.resolve("assert"),
    }
  },
  plugins: [
    new webpack.ProvidePlugin({
      Buffer: ["buffer", "Buffer"],
      process: "process/browser",
    }),
  ]

  /* ... */
}

但是a) webstorm 发誓,b) 项目不是由"start": "react-app-rewired start" 启动的。

我在不更改配置的情况下得到的错误:

Compiled with problems:X

ERROR in ./node_modules/@react-pdf/pdfkit/lib/pdfkit.browser.es.js 2:0-28

Module not found: Error: Can't resolve 'stream' in 'C:\Users\schek\Desktop\react\snitch\node_modules\@react-pdf\pdfkit\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
    - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "stream": false }


ERROR in ./node_modules/@react-pdf/pdfkit/lib/pdfkit.browser.es.js 4:0-24

Module not found: Error: Can't resolve 'zlib' in 'C:\Users\schek\Desktop\react\snitch\node_modules\@react-pdf\pdfkit\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "zlib": require.resolve("browserify-zlib") }'
    - install 'browserify-zlib'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "zlib": false }


ERROR in ./node_modules/@react-pdf/png-js/lib/png-js.browser.es.js 1:0-24

Module not found: Error: Can't resolve 'zlib' in 'C:\Users\schek\Desktop\react\snitch\node_modules\@react-pdf\png-js\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "zlib": require.resolve("browserify-zlib") }'
    - install 'browserify-zlib'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "zlib": false }


ERROR in ./node_modules/blob-stream/index.js 1:21-47

Module not found: Error: Can't resolve 'stream' in 'C:\Users\schek\Desktop\react\snitch\node_modules\blob-stream'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
    - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "stream": false }


ERROR in ./node_modules/restructure/src/EncodeStream.js 23:11-28

Module not found: Error: Can't resolve 'stream' in 'C:\Users\schek\Desktop\react\snitch\node_modules\restructure\src'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
    - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "stream": false }

【问题讨论】:

    标签: reactjs webpack create-react-app react-pdf


    【解决方案1】:

    这些错误似乎是由于 react-scripts v5.在使用 react-router v5 时花了我一天的时间来找出解决方案。但目前看来最好的方法是继续使用,或者恢复到 v4.0.3,直到 v5 添加对节点内置插件的支持 #11764 被合并并发布。

    Github 上的This issue 可能会有所帮助。

    【讨论】:

    • 你的意思是react-scripts而不是react-router
    • @stephent 是的,已编辑,我很抱歉。
    【解决方案2】:

    您可以使用rewire 更改 webpack config 的设置而不弹出。

    npm i rewire
    

    我将两个脚本(start.js 和 build.js)放在根目录(即 src 外部)的脚本文件夹中。然后在 package.json 中更改启动和构建脚本以使用它们。

    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    

    start.js:

    const rewire = require('rewire');
    const webpack = require('webpack');
    const defaults = rewire('react-scripts/scripts/start.js');
    const webpackConfig = require('react-scripts/config/webpack.config');
    
    //In order to override the webpack configuration without ejecting the create-react-app
    defaults.__set__('configFactory', (webpackEnv) => {
      let config = webpackConfig(webpackEnv);
    
      //Customize the webpack configuration here.
      config.resolve.fallback = {
        ...config.resolve.fallback,
        process: require.resolve('process/browser'),
        zlib: require.resolve('browserify-zlib'),
        stream: require.resolve('stream-browserify'),
        util: require.resolve('util'),
        buffer: require.resolve('buffer'),
        asset: require.resolve('assert'),
      };
    
      config.plugins = [
        ...config.plugins,
        new webpack.ProvidePlugin({
          Buffer: ['buffer', 'Buffer'],
          process: 'process/browser',
        }),
      ];
    
      return config;
    });

    build.js:

    const rewire = require('rewire');
    const webpack = require('webpack');
    const defaults = rewire('react-scripts/scripts/build.js');
    
    //In order to override the webpack configuration without ejecting the create-react-app
    const config = defaults.__get__('config');
    
    //Customize the webpack configuration here.
    config.resolve.fallback = {
      ...config.resolve.fallback,
      process: require.resolve('process/browser'),
      zlib: require.resolve('browserify-zlib'),
      stream: require.resolve('stream-browserify'),
      util: require.resolve('util'),
      buffer: require.resolve('buffer'),
      asset: require.resolve('assert'),
    };
    
    config.plugins = [
      ...config.plugins,
      new webpack.ProvidePlugin({
        Buffer: ['buffer', 'Buffer'],
        process: 'process/browser',
      }),
    ];

    这是this post 和您已经提到的readme 的组合。

    ...当然,您还需要曾经拥有的:

    npm install process browserify-zlib stream-browserify util buffer assert
    

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 2020-10-22
      • 2019-05-08
      • 1970-01-01
      • 2020-05-15
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      相关资源
      最近更新 更多