【问题标题】:React Typescript ERROR when running npm run build with webpack使用 webpack 运行 npm run build 时反应 Typescript 错误
【发布时间】:2020-03-21 06:02:20
【问题描述】:

我正在使用带有反应的打字稿。我想运行 npm run build。应该创建一个带有 bundle.js 文件的 dist 文件夹(我将在下面分享我的 webpack 配置)。相反,即使我已经做了一些研究来尝试解决它,我也会收到这个错误。这是错误:

npm run build

> react_tutorial@1.0.0 build C:\Users\21650\-----------------------
> webpack
Hash: ------------
Version: webpack 4.41.2
Time: 2954ms
Built at: 2019-11-25 22:18:06
1 asset
Entrypoint main = bundle.js
[1] ./src/index.tsx 253 bytes {0} [built]
    + 6 hidden modules

ERROR in ./node_modules/react-dom/cjs/react-dom.production.min.js
Module not found: Error: Can't resolve 'object-assign' in 'C:\Users\21650\Desktop\-------------\node_modules\react-dom\cjs'      
  @ ./node_modules/react-dom/cjs/react-dom.production.min.js 13:39-63
  @ ./node_modules/react-dom/index.js
  @ ./src/index.tsx

ERROR in ./node_modules/react/cjs/react.production.min.js
Module not found: Error: Can't resolve 'object-assign' in 'C:\Users\21650\-----------\node_modules\react\cjs'
  @ ./node_modules/react/cjs/react.production.min.js 10:19-43
  @ ./node_modules/react/index.js
  @ ./src/index.tsx

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! react_tutorial@1.0.0 build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the react_tutorial@1.0.0 build 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!     C:\Users\21650\AppData\Roaming\npm-cache\_logs\2019-11-25T21_18_06_349Z-debug.log  

这是我的 webpack 配置文件:

const path = require('path');

module.exports = {
 entry: './src/index.tsx',
 output: {
   filename: 'bundle.js',
   path: path.resolve(__dirname, 'dist')
 },
 module: {
   rules: [
     {
       test: /\.tsx?$/, 
       use: 'ts-loader',
       exclude: /node_modules/
     }
   ]
 },
 resolve: {
   extensions: ['.ts', '.tsx']
 }
};

如果我把描述写得太长了,我很抱歉,但这真的让我很沮丧。 对于这段代码,我尝试遵循 Chris Hawkes 在 Youtube 上的教程:Total React Tutorial - For Beginners - Using TypeScript。

更新:问题已解决 感谢@Brian Thompson 的评论,问题不再存在。 这是我的 webpack.config.js 文件的更新:

resolve: {
    extensions: ['.ts', '.tsx', '.js', '.jsx']
  }

【问题讨论】:

  • 我也会尝试完全删除node_modules 文件夹并执行npm install。已经很久了,但我之前已经看到过这个确切的错误,并认为它可能与包版本不匹配有关。
  • 您使用的 Node.js 版本是多少?
  • 非常感谢您提供的有用链接,问题出在我的 webpack 配置文件上

标签: reactjs typescript npm webpack


【解决方案1】:

更新:问题已解决感谢@Brian Thompson 的评论,问题不再存在。这是我的 webpack.config.js 文件的更新:

resolve: {
    extensions: ['.ts', '.tsx', '.js', '.jsx']
  }

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 2017-01-18
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多