Socket
Socket
Sign inDemoInstall

node-hook

Package Overview
Dependencies
0
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-hook

Run source transform function on Node require


Version published
Maintainers
1
Created

Readme

Source

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

Keywords

FAQs

Last updated on 24 Jul 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc