找不到模块:错误:当我使用webpack时无法解析模块'反应'

时间:2016-05-03 22:26:57

标签: webpack babeljs react-jsx

非常感谢这方面的帮助: 我无法使用webpack构建,我得到一个错误的简短列表,以:

开头
Module not found: Error: Cannot resolve module 'react' in ../client/front_desk.jsx

然后以一个长列表结束:

Module not found: Error: Cannot resolve 'file' or 'directory' ../node_modules/process/browser.js

这是我的webpack.config.js:

const webpack = require('webpack');
const commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');

module.exports = {
  entry: {
    front_desk: './front/client/front_desk',
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  output: {
    path: 'front/public/js',
    filename: '[name].js', // Template based on keys in entry above
  },
  module: {
    loaders: [
      {
        test: /\.(jsx|js)?$/,
        loader: 'babel',
        query: {
          presets: ['es2015', 'react'],
        },
      },
    ],
  },
  plugins: [commonsPlugin],
};

我的.babelrc:

{
  "plugins": ["syntax-jsx"],
  "presets": ["react", "es2015"],
}

我的依赖项列表在package.json中:

  "dependencies": {
    "aguid": "*",
    "babel-plugin-syntax-jsx": "*",
    "babel-preset-es2015": "*",
    "babel-preset-react": "*",
    "babel-register": "*",
    "bcrypt": "*",
    "eslint": "*",
    "eslint-config-airbnb": "^8.0.0",
    "eslint-plugin-import": "^1.6.1",
    "eslint-plugin-jsx-a11y": "^1.0.4",
    "eslint-plugin-react": "^5.0.1",
    "hapi": "*",
    "hapi-react-views": "^7.0.0",
    "inert": "*",
    "isomorphic-fetch": "*",
    "mailparser": "*",
    "mandrill-api": "*",
    "mongodb": "*",
    "nodemon": "*",
    "react": "^15.0.2",
    "react-dom": "^15.0.2",
    "react-redux": "*",
    "redux": "*",
    "redux-thunk": "*",
    "twilio": "*",
    "vision": "*",
    "webpack-dev-server": "^1.14.1"
  },
  "devDependencies": {
    "babel-core": "*",
    "babel-loader": "*",
    "faucet": "*",
    "jsx-loader": "^0.13.2",
    "nodemon": "*",
    "tape": "*",
    "webpack": "*"
  }

1 个答案:

答案 0 :(得分:18)

想出来!我首先使用--display-error-details运行webpack,在我看来应该一直默认启用。 webpack --progress --color --watch --display-error-details

这告诉我,webpack遇到这么困难的原因是因为我告诉它要寻找的扩展有问题:

  resolve: {
    extensions: ['.js', '.jsx'],
  },

会寻找react.js.js和react.js.jsx而不仅仅是react.js。所以,我必须将其更新为:

  resolve: {
    extensions: ['', '.js', '.jsx'],
  },

哪个修好了! =)