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 🙏

© 2026 – Pkg Stats / Ryan Hefner

easy-node-threading

v1.0.9

Published

⚡ Run JavaScript functions or files in isolated Node.js worker threads with a single call. Simple, minimal, and modern.

Readme

easy-node-threading

  • ⚡ Run JavaScript functions or files in isolated Node.js worker threads with a single call. Simple, minimal, and modern.
  • ♻️ Works seamlessly with CommonJS, ESM and TypeScript

🤔 Why easy-node-threading is great

  • Zero boilerplate: Run any function or file in a worker thread with a single call. No manual Worker setup needed.
  • Isolated execution: Heavy computations run in a separate thread, keeping your main Node.js event loop fast and responsive.
  • Flexible & modern: Supports both function references and file paths, ESM or CJS, and optional logging.
  • Full Node.js WorkerOptions support: You can pass resource limits, execArgv, env, stdio and everything Node’s worker API allows.
  • Lightweight & minimal: Tiny wrapper, no unnecessary dependencies.

📦 Install via NPM

$ npm i easy-node-threading

💻 Usage

  • See examples below for each scenario!
  • For file tasks, always convert the path to a file URL using pathToFileURL for ESM compatibility.
  • By default, showLogs = true prints both parent and worker messages. Set it to false to silence logs.
  • For workerOptions object param, please see the list of available options here ➡ 🔗 https://nodejs.org/api/worker_threads.html#new-workerfilename-options

| Parameter | Type | Default | Description | |-----------|------|---------|-------------| | task | Function or string | — | The function to execute in the worker, or a path/URL to a JavaScript file. | | workerOptions | Object | {} | Optional options passed directly to the Node.js Worker constructor (e.g., resourceLimits, execArgv). | | showLogs | boolean | true | Whether to log messages from the parent and worker threads to the console. Set to false to hide logs. |

CommonJS

Task file example

// task.js (CommonJS)
module.exports = async () => {
    console.log('Running heavy calculation inside the worker...');

    let sum = 0;
    for (let i = 0; i < 1e7; i++) {
        sum += i;
    }

    return sum;
};

How to use in CommonJS

const easyNodeThreading = require('easy-node-threading');

const options = {
    // --| workerOptions here
    resourceLimits: {
        maxOldGenerationSizeMb: 64,         // --| Limit memory usage
        codeRangeSizeMb: 4
    },
    execArgv: ['--trace-warnings'],         // --| Node.js flags for worker
    env: { NODE_ENV: 'worker' }             // --| Set environment
};

(async () => {
    const result = await easyNodeThreading(
        './task.js',                                    // --| File to run
        options,                                        // --| Worker options object
        true                                            // --| Show logs
    );

    console.log('File task result:', result);

    // --| Or use a function directly from here, rather than an external file
    const resultFromFunction = await easyNodeThreading(taskFunction, {}, false);
    console.log('Function task result:', resultFromFunction);
})();

const taskFunction = () => {
    let sum = 0;
    for (let i = 0; i < 1e7; i++) {
        sum += i;
    }

    return sum;
};

ESM

Task file example

// --| task.mjs (ESM)
export default async () => {
    console.log('Running heavy calculation inside the worker...');

    let sum = 0;
    for (let i = 0; i < 1e7; i++) {
        sum += i;
    }

    return sum;
};

How to use in ESM

import easyNodeThreading from 'easy-node-threading';

const options = {
    // --| workerOptions here
    resourceLimits: {
        maxOldGenerationSizeMb: 64,         // --| Limit memory usage
        codeRangeSizeMb: 4
    },
    execArgv: ['--trace-warnings'],         // --| Node.js flags for worker
    env: { NODE_ENV: 'worker' }             // --| Set environment
};

(async () => {
    const result = await easyNodeThreading(
        './task.mjs',                                   // --| File to run
        options,                                        // --| Worker options object
        true                                            // --| Show logs
    );

    console.log('File task result:', result);

    // --| Or use a function directly from here, rather than an external file
    const resultFromFunction = await easyNodeThreading(taskFunction, {}, false);
    console.log('Function task result:', resultFromFunction);
})();

const taskFunction = () => {
    let sum = 0;
    for (let i = 0; i < 1e7; i++) {
        sum += i;
    }

    return sum;
};

TypeScript

Task file example - Note we are using the same ".mjs" extension to avoid complications

// --| task.mjs (ESM)
export default async () => {
    console.log('Running heavy calculation inside the worker...');

    let sum = 0;
    for (let i = 0; i < 1e7; i++) {
        sum += i;
    }

    return sum;
};

How to use in TypeScript

import easyNodeThreading from 'easy-node-threading';
import { WorkerOptions } from 'node:worker_threads';

const taskFunction = (): number => {
    let sum = 0;
    for (let i = 0; i < 1e7; i++) {
        sum += i;
    }

    return sum;
};

const options: WorkerOptions  = {
    // --| workerOptions here
    resourceLimits: {
        maxOldGenerationSizeMb: 64,         // --| Limit memory usage
        codeRangeSizeMb: 4
    },
    execArgv: ['--trace-warnings'],         // --| Node.js flags for worker
    env: { NODE_ENV: 'worker' }             // --| Set environment
};

(async () => {
    const result = await easyNodeThreading(
        './task.mjs',                               // --| File to run (NOTICE WE RUNNING A .mjs EXTENSION!)
        options,                                    // --| Worker options object
        true                                        // --| Show logs
    );

    console.log('File task result:', result);

    // --| Or use a function directly from here, rather than an external file
    const resultFromFunction = await easyNodeThreading(taskFunction, {}, false);
    console.log('Function task result:', resultFromFunction);
})();

Run multiple workers in parallel. This example can be used in CommonJS, ESM and TypeScript

// --| Example function tasks
const task1 = () => {
    let sum = 0;
    for (let i = 0; i < 1e7; i++) {
        sum += i;
    }

    return `Task1 result: ${sum}`;
};

const task2 = () => {
    let product = 1;
    for (let i = 1; i <= 10; i++) {
        product *= i;
    }

    return `Task2 result: ${product}`;
};

const task3 = () => `Task3 message at ${new Date().toISOString()}`;

(async () => {
    // --| Start 3 workers in parallel
    const workers = [
        easyNodeThreading(task1, {}, true),
        easyNodeThreading(task2, {}, true),
        easyNodeThreading(task3, {}, true)
    ];

    // --| Wait for all workers to complete
    const results = await Promise.all(workers);

    console.log('All worker results:');
    results.forEach(result => console.log(result));
})();