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

child-process-require

v1.2.0

Published

Require a function that returns a promise and run it in a child process.

Downloads

10

Readme

Child Process Require

Build Status npm

Require a function and run it in a child process.

This is a way to take lengthy, cpu intensive tasks and run them in a child process. Allowing you to take advantage of multiple cpu cores. Child Process Require (cpr) will let you require a function from a file and run it in a new node process each time you call it. Almost as if it were all in a single process.

  • Each execution of a required function starts a new node process
  • The function's return value will be wrapped in Promise.
  • Arguments can be any JSON data type or callback function.
  • Environment variables are shared
  • stdin, stdout and stderr are shared (so console.log, console.error, etc will work)
  • Crashes will become promise rejections
  • Rejections of error objects will be recreated with stack traces
  • If you process.exit or crash an attempt will be made to stop the child process

It works like this

// work.js should have a single function exported that returns a value or a Promise.
// It can have data or functions for arguments
module.exports = function add(a, b) {
  return Promise.resolve(a + b)
}
// main.js can now start a child processes to do the work.
// `require.resolve` returns the absolute path of `work.js`
// the absolute path is required for for child process require to work
const cpr = require('child-process-require')
const absolutePath = require.resolve('./work')
const work = cpr(absolutePath)

Promise.all([
  work(1, 2),
  work(2, 3),
  work(3, 4)
]).then(console.log)
// [3, 5, 7]

Arguments and return values are passed between node processes using NodeJs's child process IPC channel and the dnode protocol. This means all objects will be JSON stringified and parsed. Rejected Error objects will be converted back into error objects with their message, stack traces and all iterable properties preserved. The dnode protocol allows for circular references and callback functions. (Functions who's return value isn't needed.)

API

  • childProcessRequire(path, [opts]) returns a function that takes any arguments and passes them to the default export or module.export of file at the path. Returns a promise that resolves to the return of the remote function
  • opts.nodeBin optional binary to run for nodejs, defaults to process.argv[0]. This is a perfect place to specify the binary for an alternate version of node. When this issue closes you'll be able to use babel-node here but for now it doesn't work.
  • opts.requires An array of strings of package names to require before requiring your function's file. This is the perect place for babel-register.
  • opts.env An object of env vars to copy into the child process's copy of process.env

Todo

  • Better API docs
  • Expose execa's options including timeouts
  • Test to see if alternative nodejs runtimes work (etc, babel-node)
  • See if we can extend dnode-protocol to handle promises, dates and error objects
  • See if we can support some subset of arbituary exports of the requied file
  • See what people think