Webpack babel es6 给我 react-router 1.0 "module not found" 的错误?

Webpack babel es6 giving me errors with react-router 1.0 "module not found"?

我在使用 react-router 时遇到以下错误,它所指的错误看起来像是在我的应用程序代码之外,但与 react-router 库本身有关:

ERROR in ./~/react-router/lib/Router.js
Module not found: Error: Cannot resolve module 'history/lib/createHashHistory' in /Users/kmartinez/apache/www/reactroutesample/node_modules/react-router/lib
 @ ./~/react-router/lib/Router.js 25:35-75

ERROR in ./~/react-router/lib/useRoutes.js
Module not found: Error: Cannot resolve module 'history/lib/Actions' in /Users/kmartinez/apache/www/reactroutesample/node_modules/react-router/lib
 @ ./~/react-router/lib/useRoutes.js 15:25-55

ERROR in ./~/react-router/lib/useRoutes.js
Module not found: Error: Cannot resolve module 'history/lib/useQueries' in /Users/kmartinez/apache/www/reactroutesample/node_modules/react-router/lib
 @ ./~/react-router/lib/useRoutes.js 17:28-61

ERROR in ./~/react-router/lib/match.js
Module not found: Error: Cannot resolve module 'history/lib/createMemoryHistory' in /Users/kmartinez/apache/www/reactroutesample/node_modules/react-router/lib
 @ ./~/react-router/lib/match.js 13:37-79

ERROR in ./~/react-router/lib/match.js
Module not found: Error: Cannot resolve module 'history/lib/useBasename' in /Users/kmartinez/apache/www/reactroutesample/node_modules/react-router/lib
 @ ./~/react-router/lib/match.js 17:29-63

当我使用 "JSX" 语法时一切正常,但是当我将 "babel-es2015-preset" 添加到我的 webpack 中并想开始切换到 ES6/ES2015 时,我得到了上面的错误。给出了什么?

module.exports = {
    entry: './app/App.js',
    output: {
        filename: 'public/bundle.js',
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel',
                query: {
                    presets: ['es2015','react']
                }

            }
        ]
    }
}

我正在使用最新版本的 react-router、react 和 babel。 Package.json 包含以下内容:

    "babel-core": "^6.2.1",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.1.18",
    "react-dom": "^0.14.3",
    "webpack": "^1.12.9",
"react": "^0.14.3",
    "react-dom": "^0.14.3",
    "react-router": "^1.0.0",
"babel-preset-react": "^6.1.18",

我当前的代码在某些文件中包含 JSX 语法,在其他文件中包含 ES6/ES2015 导入语法。如果有更好的loader集合或者我webpack配置错误,请指教!

我的 App.js 中仅有的几行是:

import React from 'react';
import ReactDOM from 'react-dom';
import {Router, Route} from 'react-router';

如果我注释掉第三行,那么第一个提到的错误就会消失,但我需要使用 react-router!

错误告诉你问题所在:

Cannot resolve module 'history/lib/createHashHistory'

react-router 依赖于 history 模块 here 并且您似乎没有安装它。

可能最容易

rm -rf node_modules
npm install

确保正确安装所有依赖项。

我假设您使用的是 npm 3+。以及 React Router 的安装文档状态:

Note that you need to also install the history package since it is a peer dependency of React Router and won't automatically be installed for you in npm 3+.

运行 npm install history 你应该很好。

节点错误。它已通过进入 project/node_modules/react-router/ 和 运行 npm install history.

修复