如何在带有 Webpack 的 React 中使用 CommonJS 模块?

How to use a CommonJS module in React with Webpack?

我正在尝试安装这个 React 库: html-2-jsx

问题是,我不知道如何将它导入 React。描述说:

To use the Node.js module, require('htmltojsx') and create a new instance.

当我尝试在 React 组件中只需要它时,ESlint 告诉我:

File is a CommonJS module. It may be converted to an ES6 module.

当然,当我尝试 运行 Webpack 时,出现错误。

我在 Webpack 中遇到的错误:

 Can't resolve 'child_process' in 
 'C:\xampp\htdocs\codeigniter_cms\public\node_modules\xmlhttprequest\lib'

很少有这种错误:

 Can't resolve 'fs' in 
 'C:\xampp\htdocs\codeigniter_cms\public\node_modules\cssstyle\lib'

如何做到这一点?老实说,我觉得有点奇怪,为 React 制作的库不支持 ES6 导入功能。

尝试将此添加到您的 webpack 配置中:

module.exports = {
  ...
  node: {
    fs: 'empty',
    child_process: 'empty'
  }
};

您看到的警告与这些错误无关。如果您想修复警告,则可能需要一些关于警告发生位置的上下文。