vite-plugin-ts-nameof
raw JSON → 3.0.0 verified Mon Apr 27 auth: no javascript
Vite plugin that resolves nameof calls (e.g., nameof(MyClass)) at build time, providing type-safe string references to member names. v3.0.0 supports Vite 2–5 and ships TypeScript types. Requires peer dependencies @typescript-nameof/common-types and @typescript-nameof/types. Alternative to babel-plugin-ts-nameof for Vite-based workflows.
Common errors
error Cannot find name 'nameof'. ↓
cause Missing global type declaration from @typescript-nameof/types.
fix
Add './node_modules/@typescript-nameof/types/index.d.cts' to the 'files' array in tsconfig.json.
error Error: Transform failed with error: Unexpected token 'nameof' ↓
cause Vite is not using the plugin or the plugin is misconfigured.
fix
Ensure vite-plugin-ts-nameof is added to the plugins array in vite.config.ts and that the plugin comes before other plugins if needed.
error TypeError: tsNameof is not a function ↓
cause Using CommonJS require instead of ESM import.
fix
Change require to import: import tsNameof from 'vite-plugin-ts-nameof'.
error Module not found: Can't resolve '@typescript-nameof/common-types' ↓
cause Missing dev dependency @typescript-nameof/common-types.
fix
Install the missing peer dependency: npm add --save-dev @typescript-nameof/common-types.
Warnings
deprecated The peer dependency @typescript-nameof/common-types version '^0.0.7' is deprecated; consider pinning to exact version or checking for updates. ↓
fix Update @typescript-nameof/common-types to latest compatible version or remove if not needed.
gotcha The plugin is only active during Vite's serve and build; it will not transform code in IDEs or other environments. ↓
fix Use the TypeScript language service plugin or a separate transform for IDE support.
gotcha nameof is a compile-time transformation; the nameof variable is not available at runtime. Ensure you are not calling nameof in a dynamic context. ↓
fix Only use nameof at the top level or within a function body that will be tree-shaken; do not assign nameof to a variable.
gotcha The plugin relies on a specific version range of Vite (^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0). Using an unsupported Vite version will cause errors. ↓
fix Ensure your Vite version matches one of the supported major versions.
breaking Version 3.0.0 drops support for Vite 1.x and may have incompatible internal changes from v2. ↓
fix If migrating from v2, check the plugin options and update Vite to >=2.0.0.
Install
npm install vite-plugin-ts-nameof yarn add vite-plugin-ts-nameof pnpm add vite-plugin-ts-nameof Imports
- default (tsNameof) wrong
const tsNameof = require('vite-plugin-ts-nameof')correctimport tsNameof from 'vite-plugin-ts-nameof' - tsNameof (named) wrong
import { tsNameof } from 'vite-plugin-ts-nameof/index.js'correctimport { tsNameof } from 'vite-plugin-ts-nameof' - nameof wrong
import { nameof } from 'vite-plugin-ts-nameof'correct// global type definition imported via tsconfig files; // no import needed at runtime, just use nameof() in TS
Quickstart
// vite.config.ts
import { defineConfig } from 'vite';
import tsNameof from 'vite-plugin-ts-nameof';
export default defineConfig({
plugins: [tsNameof()],
});
// tsconfig.json (add files array)
{
"compilerOptions": { ... },
"files": ["./node_modules/@typescript-nameof/types/index.d.cts"]
}
// In your TS code:
class MyClass {
myMethod() {}
}
const methodName: string = nameof<MyClass>(x => x.myMethod); // resolves to "myMethod"
console.log(methodName); // "myMethod"