[Tutorial] how to use ANY node.js version without using significant extra disk space

create a file called start.sh

export NVM_DIR=/rbd/pnpm-volume/$PROJECT_ID/nvm-$PROJECT_ID
if ! ls $NVM_DIR > /dev/null 2>&1 
then mkdir $NVM_DIR
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
nvm install 14 # Replace with your desired version
rm -rf /app/node_modules
ln -s /rbd/pnpm-volume/$PROJECT_ID/node_modules /app/node_modules 
npm install # prevent *.node not found error
fi
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" 
node server.js # original start command

and replace the original start script

"scripts":{
  "start":"bash start.sh"
}

then make sure you replace the last command in start.sh with your original start command
Also replace 14 iwith your desired version.

Example: https://glitch.com/edit/#!/nvm-nodisk

7 Likes

You might think this will reinstall node a lot, but actually it only reinstalls when /tmp is cleared which doesn’t happen as much as you think, only when it gets routinely cleared if it’s setup to do that or your project changes containers

But be aware that you shouldn’t abuse /tmp otherwise it’ll get banned for all of us.

1 Like

UPDATE: I edited it.

I can reduce reinstalls even more by using the node_modules folder. It is in /rbd/pnpm-volume/<uuid>. will post here after I figure it out.

Or you can put it in the tmp folder to not add project storage usage at all, however that will cost more time on startups of course. Caching them in the pnpm-volume directory is probably better :thinking:

UPDATE: Now it is installed into the node_modules path.