node-hook is a low-level utility for intercepting and transforming JavaScript source code before it is loaded by Node.js's CommonJS `require` mechanism. It allows developers to register a synchronous function that receives the raw source and filename, returning a modified source string for immediate evaluation. The current stable version is 1.0.0, released in 2017. Due to its age and reliance on Node.js's CommonJS module loader, it is largely incompatible with modern ES Modules (ESM) environments. Its release cadence was infrequent, with the last update several years ago. Key differentiators include its direct manipulation of `Module._extensions['.js']` for simple, synchronous source transforms, unlike more complex bundlers or build tools, making it suitable for runtime code instrumentation or on-the-fly transpilation in legacy CommonJS applications.
npm install node-hookVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to register a source code transformation using `node-hook` for `.js` files. It shows how to apply a function that logs the processed filename and modifies the module's export, then unhooks the transform. It also highlights Node.js's module caching behavior.
Review the commit history and issue tracker for '#11' if migrating from very old versions. For new projects, treat 1.0.0 as the baseline.
To force a module to reload and re-apply transforms, you must delete its entry from `require.cache` before calling `require()` again, e.g., `delete require.cache[require.resolve('./your-module')]`.Use alternative tools like custom loaders (`--experimental-loader`) for Node.js ESM transformation, or transpile your code to CommonJS before runtime if `node-hook` is essential for a specific part of your application.
Evaluate whether an active, maintained alternative can fulfill the required functionality. If `node-hook` is critical, consider forking and maintaining it yourself, or thoroughly vetting it for compatibility and security in your specific environment.
Use the CommonJS `require` syntax: `const hook = require('node-hook');`Access the `hook` function as a property: `const { hook } = require('node-hook');` or `const nodeHook = require('node-hook'); nodeHook.hook(...);`To ensure your transform is applied, clear the module from the `require.cache` before re-requiring: `delete require.cache[require.resolve('./your-module')];`No dependency data recorded yet.