Is there a way to execute bundled Node.js CLI scripts?
See original GitHub issueThere are various issues related to executing bundled executables where the suggested solution appears to be to extract the executable to the host filesystem and run it there.
My question is: Is there a way to execute bundled node.js script binaries?
Use case: I need to run pm2’s pm2 binary to make use of the startup
and monitor
functionality. e.g.,
// Launch pm2 monit.
const options = {
env: process.env,
stdio: 'inherit' // Display output.
}
try {
childProcess.execSync(`sudo ${pm2Path} monit`, options)
} catch (error) {
console.log(`\n 👿 Failed to launch the process monitor.\n`)
process.exit(1)
}
process.exit(0)
This fails with node_modules/pm2/bin/pm2
not being found. That file is a Javascript binary:
#!/usr/bin/env node
'use strict';
var semver = require('semver');
if (semver.satisfies(process.versions.node, '>= 6.0.0'))
require('v8-compile-cache');
process.env.PM2_USAGE = 'CLI';
// etc.
So if I extract and run it, I would assume it would require Node to be installed (?)
Is there a way I can run this directly without extracting it? Or can such functionality be added?
Thanks in advance + thank you for Nexe; it’s excellent 😃
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Command-line API | Node.js v19.3.0 Documentation
Node.js comes with a variety of CLI options. These options expose built-in debugging, multiple ways to execute scripts, and other helpful runtime options....
Read more >Run Node.js scripts from the command line
Run Node.js scripts from the command line ... The usual way to run a Node.js program is to run the globally available node...
Read more >A guide to creating a NodeJS command-line package - Medium
1. Create a NodeJS package · 2. Create a NodeJS command-line script · 3. Map a command-line script to a command name ·...
Read more >How to build a CLI with Node.js - Twilio
Now that we have this done, we can test our script. To do so, the easiest way is to use the npm link...
Read more >Mastering the Node.js CLI & Command Line Options
Using the --eval option, you can run JavaScript code right from your terminal. The modules which are predefined in REPL can also be...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you’re going this route, there are options for that!
Something like:
nexe bootstrap.js -r app-deps.zip
. If bootstrap depends on your application code you can use--no-bundle
to prevent analysis (and inclusion) of its dependencies.<strike>Just a quick update: I finally got my app working. A quick overview of how in case it helps anyone else:
fs.readFileSync()
andfs.writeFileSync()
to copy the app’s source code (my-app.zip) to an external directory (e.g., ~/.my-app)Now that it works, I don’t see why the actual app needs to be wrapped at all. Instead, I am going to create a tiny bootstrap script that copies and extracts the source zip and then forks to the main script from the external directory. (Not sure if that will introduce any other issues but it should reduce the size of the binary considerably as the code won’t be included twice.)</strike>
I was getting false successful runs as I hadn’t deactivated NVM/node on the current shell so
execSync()
was using the installed node instance. 🤦♂️The problem was that I was using
execSync()
, which launches a shell, insted offork()
as you recommended, Caleb. The latter does error if I access theprocess
object after launching the fork (Error: setRawMode EIO
) but I’ve now refactored so I don’t do that and it seems to be working.So no need for the convoluted process I outlined above (which wasn’t really working anyway).
Related: #607