npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bonton/exit-on-change

v1.1.0

Published

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

Downloads

11

Readme

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

Recommended: On The CLI

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