Skip to content

Instantly share code, notes, and snippets.

@vitalymak
Last active November 15, 2016 11:26
Show Gist options
  • Save vitalymak/56dcbbd041148db545701fba24674fde to your computer and use it in GitHub Desktop.
Save vitalymak/56dcbbd041148db545701fba24674fde to your computer and use it in GitHub Desktop.
Fork spawn/exec new instance of node difference
#!/usr/bin/env bash
. ~/.nvm/nvm.sh
nvm install 4.6.2 >/dev/null 2>&1
nvm install 6.9.1 >/dev/null 2>&1
echo 'console.log("Forked child: " + process.version)' > to-fork.js
# prints v6.9.1
node -v
# prints v4.6.2
~/.nvm/versions/node/v4.6.2/bin/node -v
# prints v4.6.2
~/.nvm/versions/node/v4.6.2/bin/node -e "require('child_process').fork('to-fork.js')"
echo "Start v4.6.2 node and spawn new instance of node, doesn't matter exec or spawn... Prints v6.9.1 two times."
~/.nvm/versions/node/v4.6.2/bin/node -p -e "require('child_process').execSync('node -v').toString()"
~/.nvm/versions/node/v4.6.2/bin/node -p -e "require('child_process').spawnSync('node',['-v']).stdout.toString()"
rm to-fork.js
# Full output:
# v6.9.1
# v4.6.2
# Forked child: v4.6.2
# Start v4.6.2 node and spawn new instance of node, doesn't matter exec or spawn... Prints v6.9.1 two times.
# v6.9.1
# v6.9.1
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment