New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@bonton/exit-on-change

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bonton/exit-on-change

Kill your node process if any of the imported or required files change.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

exit-on-change

Kill your node process if any of the files that you require changes.

Installation

yarn add --dev @bonton/exit-on-change

# or
 
npm install --save-dev @bonton/exit-on-change

Basic Usage

node -r @bonton/exit-on-change/dist/exit-on-change .

This imports dist/exit-on-change.js before importing your own code and executing.

Import in index.js & Pass Environment Variables

import '@bonton/exit-on-change';

// OR

require('@bonton/exit-on-change');

Once this is added, pass an environment variable like

EXIT_ON_CHANGE=1 node .

Imperative

import {exitOnChange} from '@bonton/exit-on-change';
exitOnChange(true);

// OR

require('@bonton/exit-on-change').exitOnChange(true);

How It Works

This package uses pirates to hook into node's module import system and then users chokidar to watch for changes in required files.

The process is not killed immediately but rather after a 100ms delay by default. See below on hwo to change the delay.

This is done so that changes can accumulate so as not to trigger too many restarts.

Tracked Extensions

.js, .jsx, .ts, .tsx, .json, .mjs, .cjs

Controlling Exit Timeout

Default Timeout: 100ms

CLI / Main File Import

Just set the EXIT_ON_CHANGE_TIMEOUT environment variable to the amount of time in milliseconds.

# if CLI only
EXIT_ON_CHANGE_TIMEOUT=500 node -r @bonton/exit-on-change/dist/exit-on-change .

# if imported in main file
EXIT_ON_CHANGE_TIMEOUT=500 node .

Imperative

The second argument takes the time in milliseconds.

import {exitOnChange} from '@bonton/exit-on-change';
exitOnChange(true, 500);

// OR

require('@bonton/exit-on-change').exitOnChange(true, 500);

Restart The Process (like nodemon)

Please use your container orchestration tool's native restart functionality.

Example: Docker Compose

Add the following property to your service definition

version: '3'

services:
    your-service:
        ...

        restart: unless-stopped # <-- this one
        
        ...

Setting restart: always is also valid, if your use-case demands it.

Why exit-on-change?

nodemon is great, but what if you are tired of listing dependencies in nodemon.json because your dependencies are in the parent folder and that node_modules directory is shared among all the other projects in your yarn workspace? That's why.

Why Kill The Process? Why Not Just Restart?

Your container orchestration tool like docker compose is more than capable of restarting services, and it probably does it better and cleaner too.

License

MIT

Keywords

FAQs

Package last updated on 28 Sep 2022

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