Sylver
Sylver

Reputation: 2453

react-router cannot resolve module history, missing lib folder

I'm creating a new React project and I have a dependency issue between react-router and history :

ERROR in ./~/react-router/lib/match.js
Module not found: Error: Cannot resolve module 'history/lib/Actions' in /app/node_modules/react-router/lib
@ ./~/react-router/lib/match.js 15:15-45

ERROR in ./~/react-router/lib/useRouterHistory.js
Module not found: Error: Cannot resolve module 'history/lib/useQueries' in /app/node_modules/react-router/lib
@ ./~/react-router/lib/useRouterHistory.js 6:18-51

ERROR in ./~/react-router/lib/createMemoryHistory.js
Module not found: Error: Cannot resolve module 'history/lib/useQueries' in /app/node_modules/react-router/lib
@ ./~/react-router/lib/createMemoryHistory.js 6:18-51

ERROR in ./~/react-router/lib/useRouterHistory.js
Module not found: Error: Cannot resolve module 'history/lib/useBasename' in /app/node_modules/react-router/lib
@ ./~/react-router/lib/useRouterHistory.js 10:19-53

ERROR in ./~/react-router/lib/createMemoryHistory.js
Module not found: Error: Cannot resolve module 'history/lib/useBasename' in /app/node_modules/react-router/lib
@ ./~/react-router/lib/createMemoryHistory.js 10:19-53

ERROR in ./~/react-router/lib/browserHistory.js
Module not found: Error: Cannot resolve module 'history/lib/createBrowserHistory' in /app/node_modules/react-router/lib
@ ./~/react-router/lib/browserHistory.js 5:28-71

ERROR in ./~/react-router/lib/hashHistory.js
Module not found: Error: Cannot resolve module 'history/lib/createHashHistory' in /app/node_modules/react-router/lib
@ ./~/react-router/lib/hashHistory.js 5:25-65

ERROR in ./~/react-router/lib/createMemoryHistory.js
Module not found: Error: Cannot resolve module 'history/lib/createMemoryHistory' in /app/node_modules/react-router/lib
@ ./~/react-router/lib/createMemoryHistory.js 14:27-69

I'm using react-router last release 3.0.0 which has history 3.0.0 as a dependency in its package.json. I'm using npm version 3.10.8 and even if history module is installed by react-router dependency, I added it in my package.json too, as stated in react-router docs.

Thing is react-router is searching its requires in history/lib/ while history module has its files in history/ (no lib folder).

Since it doesn't depend on my configuration/installation but on third parties modules, I don't see what to do and I'm surprised to not find any issue regarding this.

Upvotes: 11

Views: 25341

Answers (3)

Marina Dunst
Marina Dunst

Reputation: 870

The path needs to be updated it is no longer

history/lib/createHashHistory

It's just

history/createHashHistory

React Training Link

Also probably import HashRouter from react-route-dom. HashRouter replaces Router and no need to pass history as a prop anymore.

Upvotes: 3

Sylver
Sylver

Reputation: 2453

The problem is actually to follow react-router installation instructions, adding history module in our own package.json.

Installing the last version of history on your own instead of the one required by react-router is creating this conflict.

With react-router >= 3.0, history is a dependency with a fixed version requirement, installed along with react-router itself :

└─┬ [email protected]
  └── [email protected]

no need to install it manually in your package.json, it will create conflicts if you don't specify the right required version.

Thanks to @ShubhamKhatri for having pointing me in this direction.

Upvotes: 16

Shubham Khatri
Shubham Khatri

Reputation: 281726

If you are using npm version greater than 3.0.0 .

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 version freater than 3.0.0.

Run npm install history and you should be good.

Upvotes: 7

Related Questions