React Router无法解析模块

时间:2016-11-21 22:49:42

标签: javascript reactjs router

获取此错误...

编辑routes.js以反映变化

   ./app/config/routes.js中的错误   找不到模块:错误:无法在/ Users / sam / Desktop / battle / app / config中解析模块'react-rounter'    @ ./app/config/routes.js 2:19-43

我有webpack并做出反应 - react-dom - react-router - babel等全部安装到我的npm包中。当我运行npm start时出现上述错误。这是我的package.json / webpack config / router.js文件

的package.json

{
  "name": "battle",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "production": "webpack -p",
    "start": "webpack-dev-server"
  },
  "author": "Sam Schaefer <smmschaefer@gmail.com>",
  "license": "ISC",
  "dependencies": {
    "react": "^15.4.0",
    "react-dom": "^15.4.0",
    "react-router": "^3.0.0"
  },
  "devDependencies": {
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.8",
    "babel-preset-react": "^6.16.0",
    "html-webpack-plugin": "^2.24.1",
    "webpack": "^1.13.3",
    "webpack-dev-server": "^1.16.2"
  }
}

webpack.config

var HtmlWebpackPlugin = require('html-webpack-plugin')
var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
  template: __dirname + '/app/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = {
  entry: [
    './app/index.js'
  ],
  output: {
    path: __dirname + '/dist',
    filename: "index_bundle.js"
  },
  module: {
    loaders: [
      {test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"}
    ]
  },
  plugins: [HTMLWebpackPluginConfig]
};

routes.js

var React = require('react');
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var IndexRoute = ReactRouter.IndexRoute;
var Main = require('../components/Main');
var Home = require('../components/Home');

var routes = (
    <Router>
        <Route path='/' component={Main}>
            <Route path='/home' component={Home} />
        </Route>
    </Router>
);

module.exports = routes;

感谢您的帮助。

3 个答案:

答案 0 :(得分:5)

错误指向问题:

   ./app/config/routes.js中的错误找不到模块:错误:不能   在/ Users / sam / Desktop / battle / app / config中解析模块'react- rou n ter '   @ ./app/config/routes.js 2:19-43

routes.js 文件中:

react-rounter
         ^--- this is probasbly the issue.

不应该是'react- 路由器'吗?纠正你的routes.js文件中的拼写错误,你应该好好去。

答案 1 :(得分:3)

错误是:

  

找不到模块:无法解析'E:\ Redux \ media-library \ src'中的' react-router '。

解决方案是:

在您的目录中运行以下命令。

  

$ npm install --save react-router-dom

答案 2 :(得分:2)

我有一个类似的问题从react-router 3.x迁移到4.x

我通过安装4.x

中添加的两个缺少的依赖项来修复此问题
npm install react-router-dom
npm install react-router-config

请参阅react-router CHANGELOG