Registry / web-framework / vue-demi

vue-demi

JSON →
library0.14.10jsnpmunverified

Vue Demi (French for 'half') is a foundational utility library designed for authors building universal Vue libraries that must operate seamlessly across both Vue 2 and Vue 3 environments. It intelligently abstracts away the underlying Vue version, redirecting common Composition API imports (e.g., `ref`, `reactive`, `defineComponent`) to the appropriate Vue runtime (`vue@2` with `@vue/composition-api` or `vue@3`). The current stable version is `0.14.10`, with a consistent and active release cadence that focuses on compatibility fixes, minor feature enhancements, and addressing edge cases, often seeing several patch releases per month. Its key differentiator is providing a single, unified API surface for Composition API features and core Vue utilities, enabling developers to write code once and deploy it to both major Vue versions without needing conditional imports or complex build setups. It also exposes utilities like `isVue2` and `isVue3` for version-specific logic and a `Vue2` export for safely accessing global Vue 2 APIs (e.g., `Vue.config`, `Vue.set`).

npm install vue-demi
INSTALL
IMPORT
SIG · VUE-DEMI
V
vue-demi
web-frameworkjavascriptv0.14.10
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ref, reactive, defineComponent
import { ref, reactive, defineComponent } from 'vue-demi'
import { ref } from 'vue'; import { reactive } from '@vue/composition-api'
This is the primary way to import core Composition API utilities universally across Vue 2 and Vue 3.
isVue2, isVue3, version
import { isVue2, isVue3, version } from 'vue-demi'
if (Vue.version.startsWith('2'))
Provides reliable runtime environment detection for conditional logic within universal libraries. `version` maps to `Vue.version`.
Vue2
import { Vue2 } from 'vue-demi'
`Vue2` provides access to Vue 2's global API (e.g., `Vue2.set`). It is `undefined` in Vue 3 environments, so always check `if (Vue2)`.
install
import { install } from 'vue-demi'
Explicitly installs the Composition API plugin for Vue 2.x, which `vue-demi` often handles automatically. It is a no-op in Vue 3.

This quickstart demonstrates creating a universal composable function using `vue-demi`'s reactivity APIs and version detection (`isVue2`, `isVue3`), suitable for libraries supporting both Vue 2 and Vue 3.

import { ref, computed, isVue2, isVue3, version as vueVersion, getCurrentInstance } from 'vue-demi'; /** * A universal composable that provides a reactive counter. * Works across Vue 2 (with Composition API) and Vue 3. */ export function useUniversalCounter(initialValue: number = 0) { const count = ref(initialValue); const double = computed(() => count.value * 2); const increment = () => { count.value++; }; // Demonstrate version-specific logic if (isVue2) { console.log(`[Vue Demi] Running in Vue 2 (version: ${vueVersion}).`); // Example of accessing Vue 2 instance if needed (caution: not always universal) const vm = getCurrentInstance()?.proxy; if (vm) { console.log('Vue 2 VM detected:', vm.$options.name); } } else if (isVue3) { console.log(`[Vue Demi] Running in Vue 3 (version: ${vueVersion}).`); // Example of accessing Vue 3 instance (different structure) const instance = getCurrentInstance(); if (instance) { console.log('Vue 3 instance detected:', instance.type.name); } } return { count, double, increment, isVue2, isVue3, vueVersion }; } // To test in a Vue 3 environment: // npm install vue@3 vue-demi // npx vue-demi-switch 3 // Then integrate `useUniversalCounter` into a Vue 3 component. // To test in a Vue 2 environment (requires Composition API plugin): // npm install vue@2 @vue/composition-api vue-demi // npx vue-demi-switch 2 // Make sure @vue/composition-api is installed for Vue 2.6.x. // Then integrate `useUniversalCounter` into a Vue 2 component.
Debug
Known issues
breakingLibrary authors must correctly configure `vue` and `@vue/composition-api` as `peerDependencies` in their `package.json` to ensure `vue-demi` can correctly resolve the user's Vue environment and guide installations.
fix
Refer to the `vue-demi` README for the exact `peerDependencies` and `peerDependenciesMeta` configuration (e.g., `"vue": "^2.0.0 || >=3.0.0"`, `"@vue/composition-api": { "optional": true }`).
affects: >=0.1.0
gotchaWhen using `vue-demi` within a Vite project, it must be explicitly excluded from Vite's dependency pre-bundling to prevent module resolution issues and ensure dynamic version switching works correctly.
fix
Add `exclude: ['vue-demi']` to the `optimizeDeps` configuration in your `vite.config.js` or `vite.config.ts`.
affects: >=0.1.0
gotchaWhile `vue-demi` typically auto-installs the Composition API plugin for Vue 2 environments, in specific complex plugin setups or environments where auto-installation is unreliable, manual installation might be required.
fix
Call `install()` imported from `vue-demi` at your library's entry point or plugin registration. This function is a no-op in Vue 3, making it safe to call universally.
affects: >=0.1.0
gotchaThe `Vue2` export from `vue-demi` is provided to access global Vue 2 APIs (like `Vue.set` or `Vue.del`) but will be `undefined` when running in a Vue 3 environment.
fix
Always check `if (Vue2)` before attempting to use any properties or methods from the `Vue2` object to avoid runtime errors in Vue 3 applications.
affects: >=0.1.0
gotchaOlder browser environments (pre-Chrome 52 / Firefox 55) may not support `globalThis`, leading to `ReferenceError: globalThis is not defined`.
fix
Ensure you are using `vue-demi` version `0.14.9` or newer, as recent updates include fixes for broader compatibility regarding `globalThis`.
affects: <0.14.9
Errors
Common errors & fixes
[plugin:vite:dep-scan] Cannot find module 'vue-demi' or [Vue warn]: Failed to resolve component: ...
Vite's dependency pre-bundling interferes with `vue-demi`'s dynamic module resolution.
fix
Add `exclude: ['vue-demi']` to the `optimizeDeps` option in your `vite.config.js`.
TypeError: Vue.use is not a function (in Vue 2) or [Vue warn]: Failed to resolve plugin: CompositionApi
The `@vue/composition-api` plugin was not correctly installed in a Vue 2 environment.
fix
Ensure `@vue/composition-api` is correctly specified as a peer dependency and consider explicitly calling `install()` from `vue-demi` in your library's entry point.
TypeError: Vue.set is not a function (or Vue.delete is not a function)
Attempting to use Vue 2-specific global reactivity methods directly in a Vue 3 environment.
fix
For universal code, use reactive assignments or methods like `ref()` and `reactive()`. If Vue 2-specific global API access is needed, use `if (Vue2) { Vue2.set(...) }`.
ReferenceError: globalThis is not defined
Running `vue-demi` in an extremely old browser environment lacking `globalThis` support.
fix
Update to a browser version that supports `globalThis` (e.g., Chrome 52+, Firefox 55+, Edge 15+, Safari 10.1+). Ensure `vue-demi` is updated to at least `0.14.9` which includes compatibility fixes.
Upgrade
Version history
0.14.10latest on npm
Audit
Dependencies
@vue/composition-apioptionalRequired for Vue 2.x versions below 2.7 to provide the Composition API, as a peer dependency for libraries using vue-demi.
vuerequiredThe core Vue runtime, required as a peer dependency for any library consuming vue-demi.
Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources