6

Does anyone know if it is possible to pass node command line options (e.g. --expose-gc) directly into an npm executable.

I Have a node module that builds an executable (See here). I want that executable to have access to global.gc(). In order to do this, you need to start your node process with the --expose-gc flag.

I could force users to wrap my executable around a node command but then why do i even need an executable. Thoughts?

1 Answer 1

1

Assume your executable file is called ex. First, make sure it's executable by doing chmod a+x ex at the command line. Next, make sure the ex file begins with a line like:

#! /usr/bin/env -S node --expose-gc

env(1) will find the node executable on your path, and run it with the given arguments, passing the contents of ex into stdin of that process because of the #! "scratchbang" at the beginning of the line.

Run your program with just ex, or ./bin/ex (e.g.), rather than node ex.

Sign up to request clarification or add additional context in comments.

6 Comments

Adding the "--expose-gc" to the scratchbang throws the following error: /usr/bin/env: node --expose-gc: No such file or directory. Are you sure we can pass command line arguments on the scratchbang?
This does work. It didn't originally for me because of craziness in how we have node setup as an alias running through docker at my company. Thanks for your help @Joe Hildebrand!! I knew that my executable could run, that wasn't my issue. If anybody has issues here, the key is to add the command line options to the scratchbang!!
What do you do if you did not create ex? This is the typical case. E.g. running mocha, typescript, etc.
You can pass in node runtime command line arguments by setting the NODE_OPTIONS environment variable. So, NODE_OPTIONS="--expose-gc" ex. See nodejs.org/dist/latest-v18.x/docs/api/…
This doesn't seem to work with this specific option: --expose-gc is not allowed in NODE_OPTIONS
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.