Start nodemon with --max-old-space-size - node.js

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

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"

Related

How do i run one script on development and another on production

"scripts": {
"start": "$env:NODE_ENV=\"development\"; nodemon server.js",
"start:prod": "$env:NODE_ENV=\"production\"; nodemon server.js"
},
I want to set the NODE_ENV variable to development when i run 'start' script and to production when i run 'start:prod' script but i get error that says:
$env:NODE__ENV="development"; nodemon server.js
The filename, directory name, or volume label syntax is incorrect.

nodemon --ignore not recognized

According to the documentation, ignoring the Data.json file requires this command to be executed:
nodemon --ignore Data.json
I tried exactly that and it didn't work. The console kust returned:
Usage: nodemon [nodemon options] [scripts.js] [args]
See "nodemon --help" for more.
Other commands i tried:
nodemon --ignore 'Data.json'
nodemon --ignore "Data.json"
nodemon --ignore '/Data.json'
nodemon --ignore /Data.json
You need to pass your Server.js to nodemon prior to the ignore
nodemon Server.js --ignore 'Data.json'
Alternatively you can create a nodemon.json file
{
"verbose": true,
"ignore": ["Data.json"]
}
and then pass this as a commandline argument
nodemon Server.js --config nodemon.json

Setting Path for Mocha with --config

Mocha does not find the path to .mocharc.js file with the config.
The file is located at: app/test/.mocharc.js
I have tried setting relative as well as full paths and it always throws an error. Used quotation marks, double quotations, escaped quotations... Still I get the same error.
In package.json:
"scripts":{
test": "nodemon --exec \"mocha --config \"./test/.mocharc.js\""
},
Command line Error log:
throw new Error(`failed to parse ${filepath}: ${err}`);
^
Error: failed to parse ./test/.mocharc.js: Error: Cannot find module './test/.mocharc.js'
Your test script command is in incorrect format (unnecessary double quote).
"scripts":{
test": "nodemon --exec \"mocha --config ./test/.mocharc.js\""
},
You want to run mocha --config ./test/.mocharc.js.
And to wrap that into nodemon, you do:
nodemon --exec \"<command here>\"
Replace <command here> with your command like so.
nodemon --exec \"mocha --config ./test/.mocharc.js\"

Why does the node inspector not start when I am using nodemon and ts-node?

I have a simple node server written in typescript. My package.json is configured as:
"scripts": {
"build": "tsc",
"dev": "nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts",
"debug": "nodemon --verbose --watch src/**/* -e ts,json --exec ts-node --inspect ./src/server.ts"
},
When I run npm run dev nodemon will launch the server and restart it when any changes are made.
[02/28/18 20:45:53] npm run dev
> pq-api#1.0.0 dev C:\Users\joe\pq\pq-api
> nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts
[nodemon] 1.15.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src/**/*
[nodemon] starting `ts-node ./src/server.ts`
initializing config to development
info: PQ-API running on port 3000
However, when I run npm run debug (so I can attach a debugger) It looks like it begins to start, but just hangs forever
[02/28/18 20:39:30] npm run debug
> pq-api#1.0.0 debug C:\Users\joe\pq\pq-api
> nodemon --verbose --watch src/**/* -e ts,json --exec ts-node --inspect ./src/server.ts
[nodemon] 1.15.1
[nodemon] to restart at any time, enter `rs`
[nodemon] or send SIGHUP to 10156 to restart
[nodemon] watching: src/**/*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node --inspect ./src/server.ts`
[nodemon] spawning
[nodemon] child pid: 13344
[nodemon] watching 12 files
That is all the output has. The script is never executed; the server never starts up, and the inspector is never available to connect to.
node 8.94
nodemon 1.15.1
ts-node 5.0.0
typescript 2.7.2
With ts-node 5.0.0 you no longer pass the --inspect flag the same way. The suggested way is node --inspect -r ts-node/register path/to/ts. For example:
nodemon --watch src/**/* -e ts,json --exec node --inspect-brk -r ts-node/register src/app.ts
see https://github.com/TypeStrong/ts-node/issues/537
Provide location and port to the inspect option like:
--inspect=0.0.0.0:9200
I am using PHP Storm and the previous answer of #user60456 works like a charm for me.
With some changes, I was able to run debug mode using dotenv with multiple env files in PHP Storm as well.
package.json
"start:dev": "nodemon --watch src/**/* -e ts,json --exec node --inspect-brk -r ts-node/register -r dotenv/config local.ts dotenv_config_path=./.env.development",
where local.ts is the file where my app.listen() is.
Then, I had to create a new Run configuration (Attach to Node.js/Chrome) with:
host: localhost
port: 9229
Attach to: Chrome or Node.js > 6.3 started with --inspect
Then select the root folder in Remote URLs of local files tab and set the Remote URL to /usr/src/app.
Now, you can run npm run start:dev. You will see the console output:
Debugger listening on ws://127.0.0.1:9229/...
You have to run the Nodej.js debug configuration now and wait, until you see the console output:
Debugger attached.
The application is now running in debug mode.
I made changes using some of the info above as mine just didn't seem to work and the change I made did resolve the problem.
From "start": "tsnd --inspect -- src/app.local.ts"
To: "start": "node --inspect -r ts-node/register src/app.local.ts"
I just fixed this problem by writing nodemon.json file like this :
{
"restartable": "rs",
"ignore": [".git", "node_modules/**/node_modules"],
"verbose": true,
"execMap": { // [A]
"ts": "node --require ts-node/register"
},
"watch": ["src/"],
"env": {
"NODE_ENV": "development"
},
"ext": "js,json,ts"
}
ref : https://dev.to/oieduardorabelo/nodejs-with-typescript-debug-inside-vscode-and-nodemon-23o7
I had a different issue resulting in the debugger never running. I was running this command,
node index.js --inspect=5005
instead of this,
node --inspect=5005 index.js
The flag is supposed to be before the source file.

Nodemon not reloading. What's wrong with this nodemon.json file

This is my nodemon.json
{
"watch": ["src/**/*.ts"],
"exec": "node out/index.js"
}
I run the nodemon by executing:
nodemon
In root nodejs directory
This is output:
% nodemon
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src/**/*.ts
[nodemon] starting node out/index.js
Yay! Started app!
But when I edit any ts file in src nodemon doesn't restart the app.
UPDATE
Running nodemon --watch src/index.ts --exec 'node out/index.js'
Works and reloads the app on modifying index.ts
However, running with wildcard
nodemon --watch 'src/**/*.ts' --exec 'node out/index.js'
or
nodemon --watch src --exec 'node out/index.js'
Doesn't reload the app.
Solved!
By running nodemon in verbose mode I have discovered that by default it watches only *.js files, regardless of what wildcard you are watching. Therefore adding -e ts to the command fixes the problem:
nodemon --watch src/ --exec 'node out/index.js' -e ts
If someone uses nodemon.json here is mine after fix:
{
"watch": ["src"],
"exec": "tsc && node out/index.js" ,
"ext": "js, json, ts, proto"
}
I didn't have any luck with src/ watching either. I'm watching files via nodemon --watch '**/*' this will finds any changes in the nested files
Use single quotation for multi-value args like `--exec' in the package.json script.
e.g. I changed "nodemon --exec npm run build-langs" to "nodemon --exec 'npm run build-langs'" and worked.

Resources