因此,对于我的项目(使用React和Redux),我一直遇到“找不到模块”的错误。我用google和stackoverflow的几种解决方案,看到人们说我的node_modules搞砸了,所以我删除了它们,还安装了npm(重新安装)。错误仍然存在,我认为这与我的webpack.config.js文件有关...
我只是想让构建正常工作,并渲染react元素和组件。
错误
Module not found: Error: Can't resolve './node_modules/react' in '/Users/<user-name>/Documents/gift-test/client'
ERROR in ./client/index.js
Module not found: Error: Can't resolve './node_modules/react' in '/Users/<user-name>/Documents/gift-test/client'
@ ./client/index.js 1:0-41 15:21-26 17:16-21
ERROR in ./client/index.js
Module not found: Error: Can't resolve './node_modules/react-dom' in '/Users/<user-name>/Documents/gift-test/client'
@ ./client/index.js 2:0-50 15:0-6
ERROR in ./client/index.js
Module not found: Error: Can't resolve './node_modules/react-redux' in '/Users/<user-name>/Documents/gift-test/client'
@ ./client/index.js 3:0-54 15:41-49
ERROR in ./client/store.js
Module not found: Error: Can't resolve './node_modules/redux' in '/Users/<user-name>/Documents/gift-test/client'
@ ./client/store.js 1:0-68 7:12-23
@ ./client/index.js
ERROR in ./client/store.js
Module not found: Error: Can't resolve './node_modules/redux-devtools-extension' in '/Users/<user-name>/Documents/gift-test/client'
@ ./client/store.js 2:0-78 7:34-53
@ ./client/index.js
webpack.config.js
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
//start at entry
entry: './client/index.js',
// devtool: "eval-source-map",
mode: process.env.NODE_ENV,
//Run these rules on it & go through the loaders
module: {
rules: [
{ test: /\.svg$/, use: 'svg-inline-loader' },
{ test: /\.css$/, use: [ 'style-loader', 'css-loader' ] },
{ test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
// plugins: ['@babel/plugin-proposal-object-rest-spread']
}
}
},
]
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx'],
},
plugins: [
new HtmlWebpackPlugin({
template: 'client/index.html'
})
],
devServer: {
publicPath: "/build/",
proxy: {
'api':'http://localhost:3000',
},
},
resolve: {
extensions: ['.js', '.jsx'],
},
}
package.json的开始部分
{
"name": "gift-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "cross-env NODE_ENV=production webpack-dev-server --open",
"build": "cross-env NODE_ENV=production webpack",
"dev": "cross-env NODE_ENV=development concurrently \"nodemon server/server.js\" \"webpack-dev-server --open\" "
},
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
}
Index.js
import React from './node_modules/react';
import { render } from './node_modules/react-dom';
import { Provider } from './node_modules/react-redux';
import App from './App.jsx';
import store from './store.js';
// import styles from './index.css'
// import './index.css';
render(
<Provider store = {store}>
<App/>
</Provider>,
document.getElementById('app')
);
常规文件结构
Client
-Actions
-actions.js
-Components
-component1
-component2
-component3
-Constants
-actionTypes.js
-Containers
-Container1
-Container2
-Reducers
-Reducers (Reducer Logic)
-Index.js(Combined Reducers)
-App.jsx
-index.html
-index.css
-index.js (Provider & imported store).
-store.js
答案 0 :(得分:2)
因为您需要安装react作为依赖项。要安装,只需执行此命令npm i react
答案:导入的反应如下:ìmport React from 'react';