ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

Pass --max-old-space-size flag through to Node

Open danvk opened this issue 9 years ago • 16 comments

This controls the amount of memory that the node process is able to use.

danvk avatar Jan 03 '17 20:01 danvk

Huh, it's --max-old-space-size (with hyphens) for the node command, but --max_old_space_size (with underscores) for ts-node.

danvk avatar Jan 04 '17 15:01 danvk

Is this information still accurate? Maybe this should be added to the documentation?

juhoojala avatar May 07 '18 09:05 juhoojala

See https://github.com/TypeStrong/ts-node#programmatic.

blakeembrey avatar May 08 '18 06:05 blakeembrey

I'd like to put the answer right at here because this thread is on top of the google search ''ts-node max-old-space-size"

node -r ts-node/register --max-old-space-size=3049

Update

If you are running with os.arch() === 'ia32', the max value you can set is 3049

under my testing with node v11.15.0 and windows 10

  • if you set it to 3050, then it will overflow and equal to be set to 1.
  • if you set it to 4000, then it will equal to be set to 51 (4000 - 3049)

huan avatar Jul 03 '18 10:07 huan

I'd like to put the answer right at here because this thread is on top of the google search ''ts-node max-old-space-size"

node -r ts-node/register --max-old-space-size

Ok, this makes it possibe to pass node options. However, with this solution I wonder how to pass ts-node options (like --project, -P).

aanno2 avatar Nov 20 '19 13:11 aanno2

since there is no obvious answer in this thread, here's how i ended up adding the flag:

node --max-old-space-size=4096 -- node_modules/.bin/ts-node -P tsconfig.json index.ts

mrbar42 avatar Dec 26 '19 10:12 mrbar42

why not just add a flag to ts-node and have it work as a passthrough?

jpike88 avatar Apr 07 '20 04:04 jpike88

ts-node does not actually invoke a node process. By the time ts-node is executing, it is already within a node process, and we don't want to invoke another one. So passing CLI flags to node is not something we can do. If we were to conditionally spawn a node process based on certain flags, this would add a bunch of complexity.

The recommended solution, mentioned here, is also explained in our README. https://github.com/TypeStrong/ts-node#programmatic

Use node -r ts-node/register which allows passing any node arguments and specifying any ts-node options via environment variables and/or a "ts-node" sub-object in tsconfig.json.

cspotcode avatar Apr 07 '20 04:04 cspotcode

Basically what @cspotcode said. We used to do this, then there were numerous requests for other flags to pass through. I believe it also created some obscure issues. Honestly just wasn't something we had bandwidth to maintain, and node's --require is already pretty great.

blakeembrey avatar Apr 07 '20 04:04 blakeembrey

node --max-old-space-size=4096 -- node_modules/.bin/ts-node -P tsconfig.json index.ts

guys, I had an error with that solution:

SyntaxError: missing ) after argument list

for me it works fine with that fix:

node --max-old-space-size=4096 -- node_modules/ts-node/dist/bin -P tsconfig.json index.ts

ts-node: 8.10.1

KEMBL avatar May 06 '20 02:05 KEMBL

@KEMBL in your case, node_modules/.bin/ts-node maybe a shell script

beenotung avatar Jun 30 '20 10:06 beenotung

node --max-old-space-size=4096 -- node_modules/ts-node/dist/bin.js -P tsconfig.json index.ts

It works for some case, but if you used advanced features, may get the error of Error: Cannot find module 'tslib'

beenotung avatar Jun 30 '20 10:06 beenotung

Without seeing a reproduction, this sounds like a problem with your code, not ts-node.

On Tue, Jun 30, 2020, 6:55 AM Beeno Tung [email protected] wrote:

node --max-old-space-size=4096 -- node_modules/ts-node/dist/bin.js -P tsconfig.json index.ts

It works for some case, but if you used advanced features, may get the error of Error: Cannot find module 'tslib'

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/TypeStrong/ts-node/issues/261#issuecomment-651719565, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC35OGYU4FVHU4JUEVJZHTRZHACFANCNFSM4C3IASOA .

cspotcode avatar Jun 30 '20 12:06 cspotcode

See https://github.com/TypeStrong/ts-node#programmatic.

I see you have posted this several times, however it's not clear where to put max_old_space_size

sgentile avatar Jan 28 '21 11:01 sgentile

ts-node does not actually invoke a node process. By the time ts-node is executing, it is already within a node process, and we don't want to invoke another one. So passing CLI flags to node is not something we can do. If we were to conditionally spawn a node process based on certain flags, this would add a bunch of complexity.

The recommended solution, mentioned here, is also explained in our README. https://github.com/TypeStrong/ts-node#programmatic

Use node -r ts-node/register which allows passing any node arguments and specifying any ts-node options via environment variables and/or a "ts-node" sub-object in tsconfig.json.

do you have an example of this ?

I tried this but I get no output to confirm it when i run ts-node

{
    "ts-node": {
        "maxOldSpaceSize": 8196
    },

sgentile avatar Jan 28 '21 11:01 sgentile

If you use ESM for ts-node

node --loader ts-node/esm -r ts-node/register --max-old-space-size=8192 ./server/boot.ts

FireworksX avatar Nov 23 '23 08:11 FireworksX