s2i-nodejs-container icon indicating copy to clipboard operation
s2i-nodejs-container copied to clipboard

alternatives to npm start

Open evanshortiss opened this issue 3 years ago • 3 comments

I've noticed that the resulting s2i image starts the Node.js application using npm start.

This is a good idea since it's common practice and enables a developer to set flags etc, but it does result in two Node.js processes being launched, as seen in this screenshot:

Screenshot 2021-05-14 at 3 21 33 PM

We have the npm process, and then the child node process.

The npm process is mostly idle, but does consume memory and adds overhead on startup.

Screenshot 2021-05-14 at 3 56 57 PM

Is it worth giving consideration to support a new environment variable that defines a shell script to use on startup instead?

evanshortiss avatar May 14 '21 16:05 evanshortiss

This doesn't entirely address the issue, but you should be able to run any script that is defined in your package.json using the NPM_RUN env var.

Last I checked, I think we actually run something like: npm run $NPM_RUN with a default setting of "NPM_RUN=start". I prefer this to DEV_MODE, because restarting everything via nodemon isn't really ideal for inner-loop work.

see https://github.com/sclorg/s2i-nodejs-container/blob/master/14/s2i/bin/run#L20-L26

ryanj avatar May 14 '21 20:05 ryanj

@ryanj I think maybe I need to explain this better. The issue isn't the target script, the issue is overhead of using npm run, since it creates two Node.js processes inside the container. Running node index.js would create just one process.

It's minor, but if someone were to run an image built this way for a serverless/knative application then the 9x increased startup time (see second screenshot) could start to matter.

Edit: Spelling

evanshortiss avatar May 14 '21 21:05 evanshortiss

Fyi - some info/discussion on this topic is provided in the Node.js Reference architecture: https://github.com/nodeshift/nodejs-reference-architecture/blob/main/docs/development/building-good-containers.md#avoiding-using-npm-to-start-application. It would be good to have a way to run without having to use npm.

mhdawson avatar Aug 10 '21 21:08 mhdawson