Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
GuardProvider
✓ import { GuardProvider } from 'react-router-guards'
✗ const GuardProvider = require('react-router-guards').GuardProvider
Default export is not available; use named imports only.
GuardedRoute
✓ import { GuardedRoute } from 'react-router-guards'
✗ import GuardedRoute from 'react-router-guards'
Named export, not default.
next function
✓ const requireLogin = (to, from, next) => { next(); }
✗ const requireLogin = (to, from, callback) => { callback(); }
The third parameter must be named 'next' for the guard API to function; renaming breaks middleware.
Sets up guard middleware with a loading component, an auth guard that redirects unauthenticated users, and meta data on routes.
import React from 'react';
import { BrowserRouter, Switch } from 'react-router-dom';
import { GuardProvider, GuardedRoute } from 'react-router-guards';
const Home = () => <div>Home</div>;
const Login = () => <div>Login</div>;
const Loading = () => <div>Loading...</div>;
const Error = () => <div>Error</div>;
const requireAuth = (to, from, next) => {
const isLoggedIn = localStorage.getItem('token');
if (to.meta.auth && !isLoggedIn) {
next.redirect('/login');
} else {
next();
}
};
function App() {
return (
<BrowserRouter>
<GuardProvider guards={[requireAuth]} loading={Loading} error={Error}>
<Switch>
<GuardedRoute exact path="/login" component={Login} />
<GuardedRoute exact path="/" component={Home} meta={{ auth: true }} />
</Switch>
</GuardProvider>
</BrowserRouter>
);
}
export default App;
Errors
Common errors & fixes
TypeError: Cannot read property 'redirect' of undefined
The 'next' parameter in guard function is undefined due to incorrect signature (using wrong parameter count or names).
fixDefine guard as (to, from, next) => ... and ensure exactly three parameters.
Warning: React version mismatch. Expected >=16.8.0.
Installed package with React version older than 16.8.0 (hooks not supported).
fixUpdate React to >=16.8.0 using 'npm install react@latest'.
Module not found: Can't resolve 'react-router-dom'
Missing peer dependency react-router-dom.
fixInstall react-router-dom v5: 'npm install react-router-dom@5.0.0'.
Audit
Dependencies
reactrequiredPeer dependency - uses hooks (>=16.8.0)
react-router-domrequiredPeer dependency - designed to work with React Router v5