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-fork-string

v1.0.0

Published

You can now exec nodejs from a string, and use fork-like command to set this up.

Downloads

1,178

Readme

Child_process.forkString

NPM Version travis

Yeah. You can now exec nodejs/javascript from a string, and use fork-like command to set this up. This is really similar to Child_process.fork, it's use a string instead of a file.

Install

npm install child-process-fork-string --save

Example

let script = `
function jsHello(i) {
  if (--i > -1) {
    setTimeout(function () { jsHello(i); }, 2000);
    done = false;
  } else {
    done = true;
  }
  console.log(done);
}
jsHello(5);`

var { forkString } = require('child-process-fork-string');

const js = forkString(script, { silent: true })

// with silent: true, you can see and use stdout/stderr
js.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

js.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

// You can send a callback if you want, and it's simple !
js.on('close', (code) => {
  if (code === 0) {
    console.log('No err');
  } else {
    console.log('Err !');
  }
});

/* 

Output :

stdout: false
stdout: false
stdout: false
stdout: false
stdout: false
stdout: true
No err

*/

child_process.forkString(script[, args][, options])

  • script {string} The code to run in the child.
  • args {Array} List of string arguments.
  • options {Object}
    • type {string} Kind of process used. It can be 'vm'. Default: 'eval'
    • cwd {string} Current working directory of the child process.
    • env {Object} Environment key-value pairs.
    • execPath {string} Executable used to create the child process.
    • execArgv {Array} List of string arguments passed to the executable. Default: process.execArgv
    • silent {boolean} If true, stdin, stdout, and stderr of the child will be piped to the parent, otherwise they will be inherited from the parent, see the 'pipe' and 'inherit' options for [child_process.spawn()][]'s [stdio][] for more details. Default: false
    • stdio {Array|string} See [child_process.spawn()][]'s [stdio][]. When this option is provided, it overrides silent. If the array variant is used, it must contain exactly one item with value 'ipc' or an error will be thrown. For instance [0, 1, 2, 'ipc'].
    • uid {number} Sets the user identity of the process (see setuid(2)).
    • gid {number} Sets the group identity of the process (see setgid(2)).
  • Returns: {ChildProcess}

The child_process.fork() method is a special case of [child_process.spawn()][] used specifically to spawn new Node.js processes. Like [child_process.spawn()][], a [ChildProcess][] object is returned. The returned [ChildProcess][] will have an additional communication channel built-in that allows messages to be passed back and forth between the parent and child. See [subprocess.send()][] for details.

It is important to keep in mind that spawned Node.js child processes are independent of the parent with exception of the IPC communication channel that is established between the two. Each process has its own memory, with their own V8 instances. Because of the additional resource allocations required, spawning a large number of child Node.js processes is not recommended.

By default, child_process.fork() will spawn new Node.js instances using the [process.execPath][] of the parent process. The execPath property in the options object allows for an alternative execution path to be used.

Node.js processes launched with a custom execPath will communicate with the parent process using the file descriptor (fd) identified using the environment variable NODE_CHANNEL_FD on the child process. The input and output on this fd is expected to be line delimited JSON objects.

Note: Unlike the fork(2) POSIX system call, child_process.fork() does not clone the current process.

Note: The shell option available in [child_process.spawn()][] is not supported by child_process.fork() and will be ignored if set.

Licence

CC-BY-NC-SA-4.0 - Arthur Lacoste