TLDR; This short article deals with installing a later version of nodejs on a linux (ubuntu) Amazon EC2 instance.
Why update?
We ran node 4.5.0 for years without ever needing to advance to a more recent version. Yes, I know we were running a VERY old version, but our app was functioning without issue, and upgrading was a "nice to have", but not essential.
Eventually we'd come to an upgrade (eg of our heroku stack) or of a dependency or new feature that would force us to upgrade. That was the moment that made sense. Until then I was relativly happy to remain on an older version.
Then came the time to migrate our front end from Backbone to React. Create-react-app wont run on node less than v8, so there as no choice.
npx create-react-app react-base
You are running Node 4.5.0.
Please update your version of Node.
Below I'll explain why we updated to v12 (and not v8, v13 or v14).
Updating Nodejs via "n" Node Version Manager
node -v
V4.5.0
Clean the cache:
npm cache clean -f
Install “n” to manage node versions:
sudo npm install -g n
Check installed version of n
n -V
3.0.0
Check installed version of node
node -v
V4.5.0
Use “n” to install node v12
Next we'll Use “n” to install node (at least version 8 for react)
Check for latest stable releases of Nodejs here:
https://nodejs.org/en/about/releases/
I am installing node v12 which is Active LTS and has been established for a year at time of writing, and 2 years from End of Life. I could install v13 or v14 (both are current) but v13 is already in maintenance and will reach EOL next month. V14 has an extra year of lifespan over v12, but was only released last month, so i prefer12 for stability. Later we can migrate to v14 (skipping v13).
Install node v12:
sudo n v12
Check install:
node -v
v12.17.0
Check For Errors
No, before advancing run your tests and run you app to verify everything is still functioning OK.
ERROR:
We ran into problems with several packages. One had a newer version, so we simply updated the latest version in package.json and ran npm install to update.
“The Module {} was compiled against a different Node.js version”
Another package was 8 years old (yikes!!!) and (worse) did not have a later version. It had been build when Node was back at version 4 (or less).
It looked like we’d need to find an alternate package, but that would have meant a longer diversion from the task at hand.
Looking back at the error message gave a nice clue:
“Please try re-compiling or re-installing the module (for instance, using npm rebuild
or npm install
).”
FIXES: "npm install" and "npm rebuild"
npm install {package}
That did not fix it, BUT npm rebuild worked..
npm rebuild {package}
Instead, we rebuild the package undernode 12 and that fixed it.
Why does npm rebuild work?
I was curious why running npm rebuild would work where npm install did not. After some hunting around I learned that npm rebuild will recompile the underlying C++ addon code to comply with your installed node version.
(See NPM docs for more info.)
Conclusion:
Thinking back I would say the best way forward would be to either delete node_modules and run “npm install”, or run “npm rebuild” for your whole project.
OK, thats it! We have successfully update Node to v12 and rebuilt our packages.