我在使用react-router时遇到了以下错误,这些错误似乎与我的应用程序代码无关,而是与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”语法时,一切正常,但是当我在我的webpack中添加了“babel-es2015-preset”并想要开始切换到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 import语法。如果有更好的加载器或者我配置webpack出了问题,请提供建议!
我的App.js中唯一的几行是:
import React from 'react';
import ReactDOM from 'react-dom';
import {Router, Route} from 'react-router';
如果我注释掉第三行,那么先前提到的错误就会消失,但是我需要使用react-router!
history
是react-router
的一个依赖项。如果您不导入它,它将不会抛出错误。 - loganfsmyth