Nodejs Express - how to set max-old-space-size in package.json? - node.js

I was getting Javascript heap out of memory error.
So I made this change to my package.json file.
"scripts": {
"start": "node --max-old-space-size=4096 app.js && node ./bin/www"
},
which used to be
"scripts": {
"start": "node ./bin/www",
},
Now if I run npm start, the server is not up and running. If I try to access any page, I get unable to connect to server error message.
What am I doing wrong?
Thanks.

This works for you:
"scripts": {
"start": "node ./bin/www",
},
You need to pass the --max-old-space-size=4096 param like this:
"scripts": {
"start": "node --max-old-space-size=4096 ./bin/www",
},

Related

Nestjs/swagger open browser automatically like in REACT

I'd like my Nestjs/swagger application to start up as soon as bootstrap is finished
Initially I thought of using the callback of
async function bootstrap(): Promise<void> {
console.clear();
console.log("Starting and validating");
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
cors: true,
});
await app.listen(PORT, () => someOpenBrowserFuncion("/docs")`));
}
bootstrap();
But I didn't find anything like that, so I thought
When we start a REACT app, as soon as it is compiled, it opens the default browser automatically.
and
This option can be disabled with the following command:
"scripts": {
"start": "env BROWSER=none react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Is there a similar function in the Nestjs/swagger framework?
"scripts": {
"build": "nest build",
"dev": "nest start --watch",
"start": "env BROWSER=true nest start",
"production": "node dist/main",
},
Or some configuration to launch browser on certain endpoint?
You can install a npm package cross-env : npm install cross-env
And update this command in the package.json file under 'scripts';
"start": "cross-env BROWSER='chrome' nest start"
BROWSER is an environment variable, and you can use the cross-env package to properly handle it.
Linux:
BROWSER='google-chrome-stable'
Windows:
BROWSER='chrome'
OS X:
BROWSER='google chrome'
If these don't work, you can update the script:
Windows:
"start": "start http://localhost:3000 & nest start"
Mac:
"start": "open http://localhost:3000 && nest start"
Linux:
"start": "xdg-open http://localhost:3000 && nest start"
You can change your own port number.

how to change the path in dotenv with various script commands (in package.json) such as : start , test , etc

well my problem is when I want to switch my script command in package.json like from "start" to "test" for running my Jest test which its commands like :
"scripts": {
"start": "nodemon express/***",
"serve": "node express/***",
"dev": "node express/***",
"test": "jest --watch"
},
and I call dotenv in my project like this
require("dotenv").config({
path: "express/config/.env",
});
The code above, help my to using my environment file like .env
but the problem is that when I want to test my project and I want to switch my script command (in package.json) from like "start" to "test" and change the main path of dotenv environment to something like test.env
You could pass the environment type as an environment variable into your program like so. Note: you will need to use cross-env if you require multi-platform support.
Unix version:
"scripts": {
"start": "NODE_ENV=production nodemon express/***",
"serve": "NODE_ENV=production node express/***",
"dev": "NODE_ENV=dev node express/***",
"test": "NODE_ENV=test jest --watch"
}
cross-env version:
"scripts": {
"start": "cross-env NODE_ENV=production nodemon express/***",
"serve": "cross-env NODE_ENV=production node express/***",
"dev": "cross-env NODE_ENV=dev node express/***",
"test": "cross-env NODE_ENV=test jest --watch"
}
And then access them using the normal method of process.env.NODE_ENV
const envVariablePaths = {
"production": "/path/here",
"dev": "path/here",
"test": "path/here",
}
require("dotenv").config({
path: envVariablePaths[process.env.NODE_ENV],
})
More documentation can be found here

How to define custom nodemon configuration in nodemonConfig in package.json?

I tried implementing custom nodemon configuration in package.json as shown below:
"nodemonConfig": {
"watch": ["server", "bin/www"],
"ext": "ts",
"ignore": ["*.test.ts"],
"delay": "3000",
"execMap": {
"ts": "ts-node"
}
}
Yet, it didn't work. Nodemon doesn't restart when ./bin/www is edited, nor does ignoring the files that restart the server works.
Can anyone suggest me the correct nodemonConfig?
I think these settings only work when npm is starting nodemon, something like
// package.json
"scripts": {
"start:dev": "DEBUG=app:* nodemon app.js"
}
then use
$> npm run start:dev
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
Terminal
npm run dev

Nodemon ''npm' is not recognized as an internal or external command

I realize that this is most likely a duplicate question. I'm new to nodemon and I'm trying to establish a server for a Vue JS project with nodemon. I'm trying to run eslint with nodemon and can't figure out why I keep getting the error message. If I remove npm after --exec it will tell me ''run' is not recognized, and if I remove that I will get ''lint' is not recognized and so on.
My package.json file:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon src/app.js --exec 'npm run lint && node'",
"lint": "eslint **/*.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"eslint": "^4.16.0",
"nodemon": "^1.14.12"
}
}
I have also tried this code in my start scripts:
"scripts" : {
"start": "./node_modules/nodemon/bin/nodemon.js src/app.js --exec 'npm run lint && node'",
"lint": "./nodemodules/.bin/eslint **/*.js"
}
Where is tells me that "." is not recognized as an internal external command.
I've installed nodemon into my server folder and in the project directory as well as globally. I have done the same with eslint as well.
i had the same problem today. did some google stuff and found that this is not working anymore. so i tried this
"scripts": {
"prestart": "npm run lint ",
"start": "nodemon src/app.js ",
"lint": "./node_modules/.bin/eslint src/*.js"
},
when you npm start node will run the pre-start script before the start script.Once a file being updated this pre-start wont run by the nodemon.So for that we have to call the nodemon events.So create a nodemon.json on root folder and paste following.
{
"events": {
"restart": "npm run lint"
}
}
you can read more nodemon config options from here nodemon config .There are more nodemon events.you can read them from here event restart
PS:im very new to this. :)
EDIT1:
You can use as follows. this dont need a nodemon config;
"scripts": {
"start": "node src/app.js",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/app.js --exec \"npm run lint --fix && node\"",
"lint": "eslint --fix **/*.js "
}
for run use npm run dev it will run es lint + nodemon. this is for windows cmd command.if you are using bash terminal, remove \ in "dev" ;
"dev": "nodemon src/app.js --exec "npm run lint --fix && node""
I had the same problem.
For some reason you can't use simple quotes in npm scripts.
Use escaped double quotes instead. This should work:
"start": "nodemon src/app.js --exec \"npm run lint && node\""
install it globally for making it available on path.
npm i -g nodemon
or if using yarn
yarn global add nodemon
and if you tried this approach and it didn't work.
you should try running it locally..
you have to create a script in your package.json like this
"script": {
"server" : "nodemon scriptFile.js" //name of the file you want to run
}
then use,
npm run server
but before it,
install nodemon locally.
check it, if it is available on package.json

Nodemon start script and running eslint

I'm starting a project in Vue.JS and I'm a little new to nodemon.
Here is my package.json file
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon src/app.js --exec 'npm run eslint'",
"lint": "eslint **/*.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"eslint": "^4.16.0",
"nodemon": "^1.14.12"
}
}
I can get nodemon to run through app.js with "nodemon src/app.js". I've tried a whole bunch of combinations after --exec and haven't had any luck.
The correct way is (in package.json and windows):
"scripts": {
"start": "node index",
"start-dev": "nodemon --exec \"npm run lint && node index\"",
},
This works pretty fine for your use-case.
nodemon src/app.js --exec "npm run lint && node"
or you can write nodemon.json file in your root directory
{
"watch": ["src"],
"ignore": ["**/*.test.js", "**/*.spec.js", ".git", "node_modules"],
"exec": "npm run lint && node src/app.js"
}
Im using nodemon version 1.19.4. You just missed the "events" key. The right way would be to create a nodemon.json in your root folder like that, then a lint script in your package.json with your lint command:
{
"watch": [ "src" ],
"ignore": ["**/*.test.js", "**/*.spec.js", ".git", "node_modules"],
"events": {
"restart": "npm run lint"
}
}
Here you can check about Nodemon Events.
When using events you don't need to manually handle your application state (restart, crash, node execution, etc), just put what you want to happen when nodemon refreshes.
I've been using a custom script for a while now which I finally published to npm.
Check it out here: https://github.com/theoephraim/lint-fix-nodemon
This helps avoid double restarts when eslint fixes your files as well as not failing on the initial run if eslint has fatal errors.
Hope it helps!

Resources