Module not found: Error: Can't resolve './node_modules/react' in '/Users/<user-name>/Documents/gift-test/client'
Module not found: Error: Can't resolve './node_modules/react' in '/Users/<user-name>/Documents/gift-test/client'
所以对于我的项目(使用 React 和 Redux),我一直有 'Module Not Found' 的错误。我已经 google 和 Whosebug 找到了几个解决方案,并且看到有人说我的 node_modules 有问题,所以我删除了它们,还删除了 npm install(重新安装)。错误仍然存在,我认为这与我的 webpack.config.js 文件有关...
我只是希望构建能够正常工作并且反应元素和组件能够呈现。
错误
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
因为你需要安装react作为依赖。要安装只需执行此命令 npm i react
答案:导入反应是这样的ìmport React from 'react';
所以对于我的项目(使用 React 和 Redux),我一直有 'Module Not Found' 的错误。我已经 google 和 Whosebug 找到了几个解决方案,并且看到有人说我的 node_modules 有问题,所以我删除了它们,还删除了 npm install(重新安装)。错误仍然存在,我认为这与我的 webpack.config.js 文件有关...
我只是希望构建能够正常工作并且反应元素和组件能够呈现。
错误
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
因为你需要安装react作为依赖。要安装只需执行此命令 npm i react
答案:导入反应是这样的ìmport React from 'react';