V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ZZZero
V2EX  ›  React

webpack 找不到模块的问题求助

  •  
  •   ZZZero · 2016-05-04 00:05:38 +08:00 · 16794 次点击
    这是一个创建于 3115 天前的主题,其中的信息可能已经有所发展或是发生改变。

    那个那个,看到最近 react 发展越发成熟了。 于是小生也开始蠢蠢欲动,便想试着用用写点小东西玩玩。 然而没想到出师未捷居然死在了 webpack 这一步。_(:з」∠)_ 不知道为何每次编译都说找不到 react 和 react-dom 求各位菊苣帮助的说……

    package 如下

    "devDependencies": {
        "babel-core": "^6.7.7",
        "babel-loader": "^6.2.4",
        "babel-preset-es2015": "^6.6.0",
        "babel-preset-react": "^6.5.0",
        "css-loader": "^0.23.1",
        "react-hot-loader": "^1.3.0",
        "redux-logger": "^2.6.1",
        "style-loader": "^0.13.1",
        "stylus": "^0.54.5",
        "stylus-loader": "^2.0.0",
        "webpack": "^1.13.0"
    },
    "dependencies": {
        "react": "^15.0.2",
        "react-dom": "^15.0.2",
        "react-redux": "^4.4.5",
        "redux": "^3.5.2"
    }
    

    然后是 webpack 的配置:

    var path = require('path')
    
    module.exports = {
        entry: './entry.js',
        output: {
            path: path.join(__dirname, '/dist'),
            filename: 'bundle.js'
        },
        resolve: {
            extensions: ['','js','jsx','styl']
        },
        module: {
            loaders: [
                {
                    test: /\.js|jsx$/,
                    loader: 'babel',
                    query:{
                        presets:['es2015','react']
                    }
                },
                {
                    test: /\.styl$/,
                    loader: 'style!css!stylus'
                }
            ]
        }
    }
    

    窝这边大概是通过以下方法引入 react 模块,难道不可以么??? QAQ

    import React from 'react';
    import { render } from 'react-dom';
    

    运行 webpack --display-error-details 返回以下信息:

    ERROR in ./~/react-dom/index.js
    Module not found: Error: Cannot resolve module 'react/lib/ReactDOM' in /Users/ZZZero/rainToDo/node_modules/react-dom
    resolve module react/lib/ReactDOM in /Users/ZZZero/rainToDo/node_modules/react-dom
      looking for modules in /Users/ZZZero/rainToDo/node_modules
        resolve 'file' or 'directory' lib/ReactDOM in /Users/ZZZero/rainToDo/node_modules/react
          resolve file
            /Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOM doesn't exist
            /Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOMjs doesn't exist
            /Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOMjsx doesn't exist
            /Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOMstyl doesn't exist
          resolve directory
            /Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOM doesn't exist (directory default file)
            /Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOM/package.json doesn't exist (directory description file)
    [/Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOM]
    [/Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOMjs]
    [/Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOMjsx]
    [/Users/ZZZero/rainToDo/node_modules/react/lib/ReactDOMstyl]
     @ ./~/react-dom/index.js 3:17-46
    
    ERROR in ./~/react/react.js
    Module not found: Error: Cannot resolve 'file' or 'directory' ./lib/React in /Users/ZZZero/rainToDo/node_modules/react
    resolve file
      /Users/ZZZero/rainToDo/node_modules/react/lib/React doesn't exist
      /Users/ZZZero/rainToDo/node_modules/react/lib/Reactjs doesn't exist
      /Users/ZZZero/rainToDo/node_modules/react/lib/Reactjsx doesn't exist
      /Users/ZZZero/rainToDo/node_modules/react/lib/Reactstyl doesn't exist
    resolve directory
      /Users/ZZZero/rainToDo/node_modules/react/lib/React doesn't exist (directory default file)
      /Users/ZZZero/rainToDo/node_modules/react/lib/React/package.json doesn't exist (directory description file)
    [/Users/ZZZero/rainToDo/node_modules/react/lib/React]
    [/Users/ZZZero/rainToDo/node_modules/react/lib/Reactjs]
    [/Users/ZZZero/rainToDo/node_modules/react/lib/Reactjsx]
    [/Users/ZZZero/rainToDo/node_modules/react/lib/Reactstyl]
     @ ./~/react/react.js 3:17-39
    
    4 条回复    2016-05-04 17:44:28 +08:00
    dearrrfish
        1
    dearrrfish  
       2016-05-04 07:39:28 +08:00
    试试
    ```
    {
    test: /\.jsx?$/,
    exclude: /node_modules/,
    ...
    }
    ```
    ZZZero
        2
    ZZZero  
    OP
       2016-05-04 10:22:52 +08:00
    @dearrrfish 改了试试…… 情况没有变化 Orz
    Niphor
        3
    Niphor  
       2016-05-04 15:14:06 +08:00   ❤️ 1
    ZZZero
        4
    ZZZero  
    OP
       2016-05-04 17:44:28 +08:00
    @Niphor thx 万分感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3524 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 04:42 · PVG 12:42 · LAX 20:42 · JFK 23:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.