1. node-hook
Run source transform function on Node require
node-hook
Package: node-hook
Created by: bahmutov
Last modified: Tue, 21 Jun 2022 15:32:55 GMT
Version: 1.0.0
License: MIT
Downloads: 1,149,851
Repository: https://github.com/bahmutov/node-hook

Install

npm install node-hook
yarn add node-hook

node-hook

Run source transform function on Node require

NPM

Build status
dependencies
devdependencies
semantic-release

Install and use

 npm install --save node-hook

Before loading desired .js files, install hook

 var hook = require('node-hook');

function logLoadedFilename(source, filename) {
    return 'console.log("' + filename + '");\n' + source;
}
hook.hook('.js', logLoadedFilename);
require('./dummy');
// prints fulle dummy.js filename, runs dummy.js

hook.unhook('.js'); // removes your own transform

remember: Nodejs caches compiled modules, so if the transform is not
working, you might need to delete the cached entry in require.cache,
then call require(filename) again to force reload.

Related: Node require replacement really-need.

You can hook several transformers thanks to the code
submitted by djulien

Existing transform

You can get the current transform and run any source through it. For example
to see how the current source looks when loaded but before evaluated

 const filename = resolve('./call-foo.js')
const transform = Module._extensions['.js']
const fakeModule = {
  _compile: source => {
    console.log('transformed code')
    console.log(source)
  }
}
transform(fakeModule, filename)

Small print

Author: Gleb Bahmutov © 2013

License: MIT - do anything with the code,
but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code