找不到模块:错误:无法解析“/Users/<user-name>/Documents/gift-test/client"中的“./node_modules/react" [英] Module not found: Error: Can&#39;t resolve &#39;./node_modules/react&#39; in &#39;/Users/&lt;user-name&gt;/Documents/gift-test/client&#39;

查看:39
本文介绍了找不到模块:错误:无法解析“/Users/<user-name>/Documents/gift-test/client"中的“./node_modules/react"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,对于我的项目(使用 React 和 Redux),我一直遇到找不到模块"的错误.我已经谷歌了 &stackoverflow 提供了几种解决方案,并且看到有人说我的 node_modules 有问题,所以我删除了它们,并且还 npm install (重新安装).错误仍然存​​在,我认为这与我的 webpack.config.js 文件有关...

So for my project (using React and Redux), I keep having errors of 'Module Not Found'. I've google'd & stackoverflow'd several solutions and have seen people say that there's something messed up with my node_modules so I've deleted them, and also npm install (reinstalled). The error still persists, and I think it has to do with my webpack.config.js file...

我只是希望构建能够正常工作,并且反应元素 &要渲染的组件.

I simply just want the build to work properly and the react elements & components to render.

错误

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

Because you need to install react as a dependency. To install just execute this command npm i react

ANSWER: import react like this ìmport React from 'react';

ANSWER: import react like this ìmport React from 'react';

这篇关于找不到模块:错误:无法解析“/Users/<user-name>/Documents/gift-test/client"中的“./node_modules/react"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆