Require Extension Hooks Vue
`require-extension-hooks-vue` is a Node.js package designed to enable the server-side parsing and compilation of Vue 2.x single-file components (SFCs). It works in conjunction with `require-extension-hooks` to integrate into Node's module `require` mechanism, allowing developers to import `.vue` files directly within Node.js environments. This functionality is particularly useful for server-side rendering, browserless unit testing, or other Node.js-based build processes that need to consume Vue SFCs. The current stable version is 3.0.0. The package automatically converts `<template>` blocks into Vue render functions, supports external template and script sources, and handles various `lang` attributes (e.g., `pug`, `ts`) by dynamically looking for additional `require-extension-hooks` or compatible external libraries. It also provides a flexible mechanism for processing custom blocks within SFCs. The release cadence appears infrequent, with significant internal compiler changes typically occurring between major versions.
Common errors
-
Error: Cannot find module 'require-extension-hooks'
cause The peer dependency `require-extension-hooks` is not installed in the project.fixRun `npm install require-extension-hooks --save-dev`. -
Error: Cannot find module 'vue-template-compiler'
cause The peer dependency `vue-template-compiler` is not installed, or the installed version is incompatible with the `2.5.x` requirement.fixRun `npm install vue-template-compiler@2.5.x --save-dev` to ensure the correct version is present. -
Error: Cannot find module 'pug' (or 'typescript', 'node-sass', etc.)
cause A `.vue` file contains a `<template lang="pug">` or `<script lang="ts">` (or similar) block, but the corresponding language compiler library is not installed in the project.fixInstall the missing language compiler, e.g., `npm install pug --save-dev` for Pug templates, or `npm install typescript --save-dev` along with a TypeScript hook for TS scripts. -
TypeError: YourComponent is not a constructor / Cannot read property 'render' of undefined
cause `require-extension-hooks-vue` processes the SFC into its options object, not a Vue constructor or directly runnable component.fixThe `require()` call returns the component's options object. You typically pass this object to `Vue.extend()` or a testing utility (e.g., `@vue/test-utils`) for instantiation or mounting. Example: `const ComponentOptions = require('./MyComponent.vue'); // then use ComponentOptions`.
Warnings
- breaking Version 2.0.0 switched its internal compiler from `vue-template-compiler`'s `parseComponent` to `@vue/component-compiler-utils`. This change potentially reverses the behavior for scriptless components introduced in v1.1.0 and might affect specific custom functional component implementations, leading to different compilation results.
- gotcha This package has critical peer dependencies (`require-extension-hooks` and `vue-template-compiler@2.5.x`) that must be installed alongside it. Failure to install these will result in runtime errors when attempting to use the hook.
- gotcha When using alternative languages for templates or scripts (e.g., `<template lang="pug">`, `<script lang="ts">`), `require-extension-hooks-vue` will look for a corresponding hook or library for that language. These compilers/hooks (e.g., `pug`, `typescript` + a TypeScript hook) must be separately installed and configured.
- gotcha This library is designed for Vue 2.x projects due to its reliance on `vue-template-compiler@2.x`. It is not compatible with Vue 3.x SFCs or their associated compilation tools (`@vue/compiler-sfc`).
Install
-
npm install require-extension-hooks-vue -
yarn add require-extension-hooks-vue -
pnpm add require-extension-hooks-vue
Imports
- plugin instance / configure
import vuePlugin from 'require-extension-hooks-vue';
const vuePlugin = require('require-extension-hooks-vue'); - Activate Vue hook
require('require-extension-hooks-vue');require('require-extension-hooks').get('vue').plugin('vue'); - CLI registration
require('require-extension-hooks-vue').register();require('require-extension-hooks-vue/register'); - COMPONENT_OPTIONS (for custom blocks)
import { COMPONENT_OPTIONS } from 'require-extension-hooks-vue';const { COMPONENT_OPTIONS } = require('require-extension-hooks-vue');
Quickstart
const hooks = require('require-extension-hooks');
const vuePlugin = require('require-extension-hooks-vue');
const path = require('path');
const fs = require('fs');
// Ensure required peer dependencies are installed:
// npm install --save-dev require-extension-hooks require-extension-hooks-vue vue-template-compiler
// Optional: Configure the plugin (e.g., enable source maps)
vuePlugin.configure({ sourceMaps: true });
// Register the Vue hook to enable .vue file parsing in Node.js
hooks('vue').plugin('vue');
// Create a dummy .vue file for demonstration
const vueFilePath = path.join(__dirname, 'MyExampleComponent.vue');
const vueContent = `
<template>
<div class="my-component">
<h1>Hello from Vue!</h1>
<p>{{ message }}</p>
</div>
</template>
<script>
module.exports = {
name: 'MyExampleComponent',
data() {
return {
message: 'This is a .vue component loaded in Node!'
};
},
created() {
console.log('MyExampleComponent created in Node context!');
}
};
</script>
<style scoped>
.my-component h1 {
color: purple;
}
</style>
`;
fs.writeFileSync(vueFilePath, vueContent);
// Now you can require .vue files directly in Node.js
try {
const ComponentOptions = require(vueFilePath);
console.log('Successfully loaded Vue component options:');
console.log('Component name:', ComponentOptions.name);
// In a test scenario, you might inspect its render function or data
if (typeof ComponentOptions.data === 'function') {
const data = ComponentOptions.data();
console.log('Component data:', data);
}
// You could then use these options with a Vue instance or testing utility
// For example, if you had Vue installed:
// const Vue = require('vue');
// const instance = new Vue(ComponentOptions).$mount();
// console.log(instance.$el.innerHTML);
} catch (error) {
console.error('Error loading Vue component:', error.message);
console.error('Ensure all peer dependencies are installed, and your .vue file is valid.');
} finally {
// Clean up the dummy file
if (fs.existsSync(vueFilePath)) {
fs.unlinkSync(vueFilePath);
}
}