How to make a node.js server run with a specific command - node.js

I want to start my index.js file with node --max-old-space-size=1024 index.js but I don't want to type the command manually everytime, any Idea how can I make a .js file that executes the command for me? Like start.js and when I launch it, it immediately runs node --max-old-space-size=1024 index.js. This might be super straight forward but I'm still new to node.js 😅

You can modify your package.json to create a command that execute node --max-old-space-size=1024 index.js
In your package.json
"scripts": {
"start": "node --max-old-space-size=1024 index.js"
}
And then run npm run start.

Related

Notify app.js if it run from "npm run dev" or "npm start"

Is that possible to notify app.js file or any other file that I am using a specific script from package.json?
my relevant scripts
"dev":"nodemon app.js",
"start":"node app.js"
In my case i have cron functions that run on background, its a problem when I using nodemon and some of those function invoked I kill them before them ended.
on other way , its more convenient to use nodemon on development, so i want to know if there is a way to notify my app.js if it run from "npm start" or "npm run dev"
What about using arguments ?
"dev":"nodemon app.js dev",
"start":"node app.js production"
process.argv holds a list of all arguments, including dev and production now which can then be used to distinguish the two cases.

webpack script in package.json

I have a question regarding the script property in package.json of my app. If I type npm start, the app gets compiled successfully. And it seems that by typing the above command, www file starts running.
However, if I type npm webpack, nothing happens. I have a react/express app. And the server runs on localhost:3000 on npm start. I want to know, is webpack serving both react and express? I am able to send requests from the client to the server and show the result on the client-side. I just wanted to know how things are working.
"scripts": {
"start": "NODE_ENV=development nodemon ./bin/www",
"webpack": "webpack-dev-server --config ./webpack.config.js --mode development"
}
for other scripts than "start", you have to type npm run [scriptname].
in your case: npm run webpack

Transpiling ES6 and launching Express app upon file change

I'm developing an Express app. The code uses ES6 import statements, so it needs to be transpiled by Babel in ES5 code so it can be run with Node. The ES6 source code is stored in my src directory, and the compiled code is placed in my dest directory.
I would like an npm command that I can run that will watch for any changes in my src directory, transpile the project into dest, and then restart the Express app.
Below is my solution so far (I run npm run server:watch). It uses chokidar-cli to execute the server:build and server:run scripts whenever any js file changes within my src directory. This works perfectly except that the Express app does not die when the code is transpiled again, so I get a 'port in use' exception when I try to run server:watch again.
// package.json
"scripts": {
"server:clean": "rm -f dist/*",
"server:build": "npm run server:clean && ./node_modules/.bin/babel ./src --experimental --source-maps-inline -d ./dist",
"server:run": "nodemon dist/app.js --watch",
"server:watch": "chokidar 'src/*.js' -c 'npm run server:build && npm run server:run'"
}
Am I on the right track here? What is the best solution to this sort of problem?
(For clarification, I am talking about transpiling server-side code, so webpack or any other module bundler would be inappropriate here)
nodemon is watching your dist files, and is never terminating. With your watch script, you're launching a second instance of nodemon every time there's a file change, hence the "port in use" error.
You need to launch nodemon once, and keep in running in the background while your watch script is running. This is most easily done with https://github.com/mysticatea/npm-run-all.
Install it by running:
npm install npm-run-all --save-dev
on the command line.
Then you'd edit your scripts to:
// package.json
"scripts": {
"server:clean": "rm -f dist/*",
"server:build": "npm run server:clean && ./node_modules/.bin/babel ./src --experimental --source-maps-inline -d ./dist",
"server:run": "nodemon dist/app.js --watch",
"server:watch": "chokidar 'src/*.js' -c 'npm run server:build'",
"server:dev": "run-p server:run server:watch"
}
Then, you simply run npm run server:dev on the command line to start up your server via nodemon, and start the watch/rebuild process, all in one command.
Side note: If you're developing on Windows, you won't be able to use single quotes in your scripts, you have to use double-quotes, escaped with \" in the JSON.

nodeJS app, running node and webpack --watch?

brothers and sisters, I am trying to do this:
"scripts": {
"start": "node ./node_modules/.bin/http-server ./public",
"poststart": "webpack --watch"
}
It is not working. The idea being node runs the app (duh) and there's a webpack --watch instance rebuilding my code as it changes. What's the answer here?
One way to do it is like this:
you start webpack --watch in one terminal to watch the sources and rebuild when changes occur
you use nodemon (which is a npm package) in another terminal to watch for build (NOT source) changes and restart the app
Reference: https://nodemon.io/

How do I make "node ." work?

I've run node . in various NodeJS apps in the past, and it seems to know to run index.js, server.js, etc. Is there a package.json setting (or something similar) that I can configure so that NodeJS knows which file should run?
npm does have a one configuration for that:
"main": "app.js"

Resources