Socket
Socket
Sign inDemoInstall

node-hook

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-hook

Run source transform function on Node require


Version published
Weekly downloads
334K
increased by0.29%
Maintainers
1
Weekly downloads
 
Created

What is node-hook?

The node-hook package allows you to hook into Node.js' require function to modify the behavior of module loading. This can be useful for tasks such as transpiling code on the fly, mocking modules for testing, or adding custom behavior to module loading.

What are node-hook's main functionalities?

Transpile Code on the Fly

This feature allows you to transpile ES6 code to ES5 on the fly using Babel. By hooking into the '.js' extension, you can transform the source code before it is executed.

const hook = require('node-hook');
const babel = require('@babel/core');

hook.hook('.js', (source, filename) => {
  const result = babel.transformSync(source, { filename });
  return result.code;
});

require('./your-es6-file');

Mocking Modules for Testing

This feature allows you to mock specific modules for testing purposes. By checking the filename, you can return a mocked version of the module.

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

hook.hook('.js', (source, filename) => {
  if (filename.endsWith('some-module.js')) {
    return 'module.exports = { mockedFunction: () => "mocked" };';
  }
  return source;
});

const someModule = require('./some-module');
console.log(someModule.mockedFunction()); // Outputs: mocked

Custom Behavior for Module Loading

This feature allows you to add custom behavior to the module loading process. In this example, it logs the filename of each module being loaded.

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

hook.hook('.js', (source, filename) => {
  console.log(`Loading module: ${filename}`);
  return source;
});

require('./your-module');

Other packages similar to node-hook

Keywords

FAQs

Package last updated on 11 Jan 2014

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc