# Error `#2`: `ViewModel` not found This happened because the [`vmLookup`](/api/other/view-model-lookup) provided for the [`useViewModel`](/react/api/use-view-model) hook was not found in [`ViewModelStore`](/api/view-model-store/overview). ## Explanation: Hook ```tsx const model = useViewModel(YourVM); ``` Will use [`ViewModelStore`](/api/view-model-store/overview) to find your instance of provided `YourVM` [`ViewModel`](/api/view-models/overview). It means that your `ViewModel` is not created yet and not registered in `ViewModelStore`, or you have not declared the creation of this `ViewModel` anywhere. ### HMR (Hot Module Replacement) This error can also happen during development with HMR. When you edit a `ViewModel` class (add/remove a property, change a method, etc.), HMR replaces it with a new class — but the instance in `ViewModelStore` still belongs to the old one. The lookup can't find it and throws. ## Potential solution Create and register your `ViewModel` using [`useCreateViewModel`](/react/api/use-create-view-model) hook or [`withViewModel`](/react/api/with-view-model) HOC. ```tsx const model = useCreateViewModel(YourVM); ``` You can also register an instance yourself via [`ViewModelStore.define()`](/api/view-model-store/interface#define-config) (or `create` + `connect` on [`ViewModelStoreBase`](/api/view-model-store/base-implementation)). `define()` already creates and connects the instance — you still need to start mounting (e.g. call `mount()`) and arrange teardown with [`unmount`](/api/view-model-store/interface#unmount-instance). Prefer the React / Solid hooks and HOCs when you can. ### Fixing HMR issues If the error only appears after editing a `ViewModel` class during development, use the [`mobx-view-model-vite-plugin`](https://github.com/js2me/mobx-view-model-vite-plugin) — it automatically reconciles the old and new constructors on HMR updates so the lookup continues to work without a full page reload. ## 🤖 Prompt for LLM agents Use the following prompt when asking an LLM agent to fix this error: ```text Fix Error #2 in the current repository: "ViewModel not found". Before editing anything, inspect the current repository and confirm that the error is actually present. Work only inside the current repository; never edit absolute paths or files in another project. If no affected usage is found, report that fact and do not create unrelated files. First check whether the user provided a stack trace, error log, or reproduction steps. If the trace is missing and the failing call cannot be identified from the repository, ask the user to provide the full stack trace and the smallest reproduction before editing. If the repository clearly contains the failing usage, continue without blocking on an additional trace. Trace the failing `useViewModel` call and verify how its ViewModel is expected to be found. Check whether the ViewModel is created and registered through `useCreateViewModel`, `withViewModel`, or the project's `ViewModelStore`. If it is not registered, use the repository's existing registration pattern. If the failure occurs only after editing a ViewModel during HMR, treat it as an HMR identity/reconciliation problem and use the project's existing HMR solution instead of adding duplicate registration or unrelated reload logic. Do not stop after explaining the root cause. If an affected usage is confirmed, edit the relevant source code and apply the smallest correct fix. Do not silence the exception, return a fake model, create duplicate ViewModel instances, or change unrelated components. Inspect the diff and confirm that the ViewModel is found through the intended registration path. If no safe code change can be made, state the specific blocker instead of claiming that the error is fixed. Preserve lifecycle and teardown behavior. Add or update a focused test only if the confirmed occurrence is covered by the repository's existing test structure; do not create a new test file merely because this prompt asks for one. Run the relevant typecheck, lint, and test commands and report the files changed, root cause, and verification results. The task is complete only after the confirmed occurrence has been fixed in the repository (or the concrete blocker has been reported) and the relevant checks have been run. ```