使用webpack进行反应时出错

时间:2018-06-16 18:26:10

标签: javascript reactjs webpack mern

我正在研究Apress的Pro MERN Stack中的示例,并且遇到了webpack的问题。直到我开始使用它,一切都按预期工作。现在,当我运行npm run compile时,我得到了这个:

> pro-mern-stack@1.0.0 compile /Users/me/Documents/Books/Pro MERN Stack/pro-mern-stack
> webpack

Hash: 3869c18532e36184fca8
Version: webpack 4.12.0
Time: 1101ms
Built at: 2018-06-16 18:56:00
 1 asset
 [0] ./src/BorderWrap.jsx 2.33 KiB {0} [built]
 [2] ./src/IssueList.jsx 6.38 KiB {0} [built]
 [3] ./src/IssueFilter.jsx 2.25 KiB {0} [built]
 [4] ./src/IssueAdd.jsx 3.03 KiB {0} [built]
[17] ./src/App.jsx 882 bytes {0} [built]
    + 13 hidden modules

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

ERROR in ./node_modules/react-dom/cjs/react-dom.production.min.js
Module not found: Error: Can't resolve 'react' in '/Users/me/Documents/Books/Pro MERN Stack/pro-mern-stack/node_modules/react-dom/cjs'
 @ ./node_modules/react-dom/cjs/react-dom.production.min.js 13:53-69
 @ ./node_modules/react-dom/index.js
 @ ./src/App.jsx

ERROR in ./src/App.jsx
Module not found: Error: Can't resolve 'react' in '/Users/me/Documents/Books/Pro MERN Stack/pro-mern-stack/src'
 @ ./src/App.jsx 3:13-29

ERROR in ./src/IssueList.jsx
Module not found: Error: Can't resolve 'react' in '/Users/me/Documents/Books/Pro MERN Stack/pro-mern-stack/src'
 @ ./src/IssueList.jsx 9:13-29
 @ ./src/App.jsx

ERROR in ./src/BorderWrap.jsx
Module not found: Error: Can't resolve 'react' in '/Users/me/Documents/Books/Pro MERN Stack/pro-mern-stack/src'
 @ ./src/BorderWrap.jsx 9:13-29
 @ ./src/App.jsx

ERROR in ./src/IssueFilter.jsx
Module not found: Error: Can't resolve 'react' in '/Users/me/Documents/Books/Pro MERN Stack/pro-mern-stack/src'
 @ ./src/IssueFilter.jsx 9:13-29
 @ ./src/App.jsx

ERROR in ./src/IssueAdd.jsx
Module not found: Error: Can't resolve 'react' in '/Users/me/Documents/Books/Pro MERN Stack/pro-mern-stack/src'
 @ ./src/IssueAdd.jsx 9:13-29
 @ ./src/App.jsx
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! pro-mern-stack@1.0.0 compile: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the pro-mern-stack@1.0.0 compile 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!     /Users/me/.npm/_logs/2018-06-16T17_56_00_932Z-debug.log

我不相信任何事情随着反应发生了变化,所以无法理解为什么它会突然引发问题。我的想法是webpack存在问题,或者我使用的webpack版本与反应版本不兼容。我假设每个错误都有相同的问题。

有没有人表现出类似的东西,如果有,你是如何解决它的?

添加package.json

{
  "name": "pro-mern-stack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "compile": "webpack",
    "watch": "webpack --watch",
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon -w server.js server/server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "async": "^2.6.1",
    "body-parser": "^1.18.3",
    "co": "^4.6.0",
    "express": "^4.16.3",
    "mongodb": "^3.0.10"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "nodemon": "^1.17.5",
    "react-dom": "^16.4.1",
    "webpack": "^4.12.0",
    "webpack-cli": "^3.0.7",
    "whatwg-fetch": "^2.0.4"
  }
}

1 个答案:

答案 0 :(得分:0)

正如@tarzenchug所指出的那样,没有安装反应。当我跑npm i react时,它运作了。