Search code examples
node.jsnodemon

Start nodemon with --max-old-space-size


Previously, I was starting a nodejs sever with:

"start": "node --max-old-space-size=1024 index.js",

But now it was migrated to typescript, and some change the start script to:

"start:ts": "nodemon server.ts",

But with that I'm losing the option --max-old-space-size=1024.

How can I start the server using nodemon but with --max-old-space-size=1024 ?

I tried:

"start:ts": "nodemon --max-old-space-size=1024 server.ts",

but says:

[nodemon] starting `ts-node --max-old-space-size=1024 server.ts` /MyProject//node_modules/arg/index.js:90
                    throw err;
                    ^

Error: Unknown or unexpected option: --max-old-space-size

Solution

  • I just discovered it's quite complicated to pass arguments to Node using ts-node. As per the docs, you can call Node and register ts-node in the command. You can use the --exec flag on nodemon to execute a custom command. Here's how I would frame the entire command:

    nodemon --exec "node --max-old-space-size=1024 -r ts-node/register ./server.ts"
    

    You can alternatively call ts-node directly and specify the NODE_OPTIONS environment variable:

    nodemon --exec "NODE_OPTIONS='--max-old-space-size=1024' ts-node ./server.ts"