Skip to content
Run source transform function on node require call
Branch: master
Clone or download
Latest commit eb06f40 Jul 24, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore simple test is working Jan 11, 2014
.npmrc chore(ci): added semantic release, closes #4 Apr 19, 2016
.travis.yml fix(ci): fixing semantic release publishing May 4, 2017
DockerTemplate chore(test): testing in Docker containers using autochecker May 2, 2016 added travis file, badges Jan 11, 2014 fix(badges): use svg badges for higher resolution May 4, 2017
index.js feat(unhook): warn if trying to unhook non-registered hook, close #9 May 4, 2017
package.json refresh tokens Jul 24, 2017


Run source transform function on Node require


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);
// 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')
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

You can’t perform that action at this time.