The “npm start” pattern wastes a lot of RAM

Convention

npm start is a convenient way to fire up our Node applications. Just create a server.js file, and npm start will invoke node server.js. Many hosting providers, such as Heroku, Modulus, and others make use of this convention to boot up Node when new servers are spawned.

Problem

Unfortunately, there’s one problem: npm start immediately consumes about 46MB of memory. Consider the following simple tests with node v5.10, and npm 3.8.3 on OSX:

node server.js

~/workspace/langa $ ps -f -o rss | grep n[op][md]
501 14704 86626 0 5:20AM ttys002 0:00.60 node server.js 62808

Total usage: 62MB

npm start

~/workspace/langa $ ps -f -o rss | grep n[op][md]
501 11586 11580 0 5:12AM ttys002 0:00.58 node server.js 62552
501 11580 86626 0 5:12AM ttys002 0:00.34 npm 46336

Total usage: 108MB.

(I’ve bolded the RAM usage for each of these node processes). In both cases, I’m firing up the server for the Langa website, which is a very simple Trails.js application.

Solution

If you’re focusing on performance of your web application, 46MB of RAM is a lot. It’s somewhat surprising that a scalable and performant web hosting platform like Heroku uses npm start by default. An easy solution is simply to start your servers by invoking node directly rather than via npm. I’m comfortable with treating the two commands as functionally equivalent, and recommend that production applications simply invoke node server.js directly. You’ll save a couple seconds of boot time as well.

Update

Upon further testing, it looks like the “npm” process does decrease in memory over time. About 48 hours later, testing shows the npm process using negligible memory.

Like what you read? Give Travis Webb a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.