{"id":26861,"library":"create-history","title":"history","description":"A JavaScript library for managing session history in any JavaScript environment. Version 2.1.1 (stable) provides a minimal API to manage the history stack, navigate, confirm navigation, and persist state. It abstracts away differences between browser, Node, and other environments. Key differentiator: it is the core dependency behind React Router v4, offering createHistory, createHashHistory, and createMemoryHistory. Release cadence is infrequent; v2 is stable but superseded by v4 (ESM) and v5 (React Router). Note: This version uses CommonJS; ESM was introduced in later versions.","status":"maintenance","version":"2.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/Lucifier129/history","tags":["javascript","history","location"],"install":[{"cmd":"npm install create-history","lang":"bash","label":"npm"},{"cmd":"yarn add create-history","lang":"bash","label":"yarn"},{"cmd":"pnpm add create-history","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"v2 only has named exports. Default export is the module object.","wrong":"import createHistory from 'history' // default export is not createHistory","symbol":"createHistory","correct":"import { createHistory } from 'history'"},{"note":"Uses browser hash-based routing.","wrong":"var createHashHistory = require('history').createHashHistory // correct but not wrong","symbol":"createHashHistory","correct":"import { createHashHistory } from 'history'"},{"note":"For non-browser environments like React Native or testing.","wrong":"import createMemoryHistory from 'history' // wrong: no default export","symbol":"createMemoryHistory","correct":"import { createMemoryHistory } from 'history'"},{"note":"Deprecated in v2? Not available in v2.1.1 — no such export. Check documentation.","wrong":"const useQueries = require('history/useQueries') // wrong: path does not exist","symbol":"useQueries","correct":"import { useQueries } from 'history'"}],"quickstart":{"code":"const { createHistory, createMemoryHistory } = require('history');\n// or use import { createHistory } from 'history' with ESM bundler\n\nconst history = createHistory(); // browser history\n\n// Get the current location\nconst location = history.getCurrentLocation();\nconsole.log(location.pathname);\n\n// Listen for changes\nconst unlisten = history.listen((location) => {\n  console.log('New location:', location.pathname);\n});\n\n// Push a new entry\nhistory.push({\n  pathname: '/new-path',\n  search: '?query=example',\n  state: { user: 'jane' }\n});\n\n// Stop listening\nunlisten();\n\n// In Node.js, use memory history:\nconst memoryHistory = createMemoryHistory();\nmemoryHistory.push('/home');","lang":"javascript","description":"Shows how to create a browser history, listen for location changes, push a new entry, and use memory history in non-browser environments."},"warnings":[{"fix":"Use require() or configure bundler for CommonJS. For ESM, upgrade to v4+.","message":"v2 does not support ESM imports directly; use require or a bundler that handles CommonJS.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Avoid using them; they are not present in v2.1.1 final? Actually they were removed earlier. Check docs.","message":"useQueries and useBasename are deprecated in v2; they were removed in later versions.","severity":"deprecated","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Use named import: import { createHistory } from 'history'.","message":"Default export from 'history' is the module object, not createHistory. Named exports only.","severity":"gotcha","affected_versions":">=2.0.0 <3.0.0"},{"fix":"For server-side rendering, use createMemoryHistory or check if window exists.","message":"createHistory() uses window.history; fails in Node.js without polyfill. Use createMemoryHistory instead.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Update listeners when upgrading to v3: (location, action) => void.","message":"v2 listener signature: (location) => void. In v3+, includes action and other params.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use require('history').createHistory or configure bundler (e.g., webpack) to handle CommonJS named exports.","cause":"Trying to import named export from default CommonJS module without proper ESM interop in bundler.","error":"Attempted import error: 'createHistory' is not exported from 'history'."},{"fix":"Use createMemoryHistory() instead of createHistory() in Node.js.","cause":"Using createHistory() in Node.js environment without browser history API.","error":"history is not defined"},{"fix":"Ensure you use createHistory (or createMemoryHistory) to create the history object.","cause":"Calling listen on a history object that was not created by createHistory (e.g., using default import).","error":"TypeError: history.listen is not a function"},{"fix":"Check that location is defined: const location = history.getCurrentLocation(); or wait for listener to fire.","cause":"Trying to access location.pathname before location is defined or inside uninitialized listener.","error":"Cannot read property 'pathname' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}