{"record":{"id":"66599aaf548aa9af","repo":"framework7io/framework7","slug":"framework7-is-already-initialized-and-can-t-be-ini","errorCode":null,"errorMessage":"Framework7 is already initialized and can't be initialized more than once","messagePattern":"Framework7 is already initialized and can't be initialized more than once","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/components/app/app-class.js","lineNumber":20,"sourceCode":"\nimport { getWindow, getDocument } from 'ssr-window';\nimport { extend, nextFrame } from '../../shared/utils.js';\nimport { getDevice } from '../../shared/get-device.js';\nimport { getSupport } from '../../shared/get-support.js';\nimport Framework7Class from '../../shared/class.js';\nimport EventsClass from '../../shared/events-class.js';\nimport ConstructorMethods from '../../shared/constructor-methods.js';\nimport ModalMethods from '../../shared/modal-methods.js';\nimport $ from '../../shared/dom7.js';\nimport loadModule from './load-module.js';\nimport $jsx from '../../shared/$jsx.js';\n\nclass Framework7 extends Framework7Class {\n  constructor(params = {}) {\n    super(params);\n    // eslint-disable-next-line\n    if (Framework7.instance && typeof window !== 'undefined') {\n      throw new Error(\"Framework7 is already initialized and can't be initialized more than once\");\n    }\n    const device = getDevice({ userAgent: params.userAgent || undefined });\n    const support = getSupport();\n\n    const passedParams = extend({}, params);\n\n    // App Instance\n    const app = this;\n\n    app.device = device;\n    app.support = support;\n\n    const w = getWindow();\n    const d = getDocument();\n\n    Framework7.instance = app;\n\n    // Default","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/framework7io/framework7/blob/655759126686766530a027b2f8f369c991d63d1c/src/core/components/app/app-class.js#L2-L38","documentation":"Framework7 enforces a singleton pattern: only one Framework7 app instance may exist per page. The constructor checks `Framework7.instance` and throws immediately if an instance is already stored. This prevents conflicting global state (root elements, event bindings) from multiple apps.","triggerScenarios":"Calling `new Framework7({...})` a second time while a previous instance exists (e.g. `Framework7.instance` set), typically during hot module reload, re-running an init script on navigation, or double-including the init code.","commonSituations":"Hot reload in dev without disposing the old app; calling init in both a global script and a page-specific script; re-executing an inline script on SPA route change; accidentally importing the bundle twice.","solutions":["Guard creation: reuse the existing instance instead of constructing a new one","If a fresh instance is truly needed, delete Framework7.instance first (window handling applies) before re-initializing","Move init code so it runs exactly once (module top-level, DOMContentLoaded guard, or a boolean flag)","In HMR setups, dispose/teardown the app on module unload before re-creating"],"exampleFix":"// before\nconst app = new Framework7({ ... });\n\n// after\nconst app = Framework7.instance || new Framework7({ ... });","handlingStrategy":"validation","validationCode":"if (!Framework7.instance) {\n  const app = new Framework7({ /* ... */ });\n}","typeGuard":"function isAppCreated() {\n  return typeof Framework7 !== 'undefined' && !!Framework7.instance;\n}","tryCatchPattern":"let app;\ntry {\n  app = new Framework7(params);\n} catch (e) {\n  if (/already initialized/.test(e.message)) app = Framework7.instance;\n  else throw e;\n}","preventionTips":["Initialize the app exactly once in a dedicated entry module","Guard with `Framework7.instance` before constructing","In HMR workflows, dispose the app on module unload","Avoid embedding init scripts in multiple included files"],"tags":["initialization","singleton","framework7"],"backgroundTag":"duplicate-initialization","analyzedSha":"655759126686766530a027b2f8f369c991d63d1c","analyzedAt":"2026-09-02T19:48:40.711Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}