Setting up Versioned Node Environment
Wednesday, November 25th, 2020Today I spent a little time with a good friend helping me get going on a good, versioned Node environment - a lot like RVM for Ruby - but for Node. I wanted to do this because it looks like I might be doing some work for a Node-based company where the development is all based in Node, and I wanted to make sure I got it all set up right.
I just finished reading a nice book on ES5, ES6, Promises, async and await, and all the new features of JavaScript called Simplifying JavaScript from the Pragmatic Programmers. They are having a Thanksgiving Sale, and it seemed like a great time to pick up a book that I'd probably like on the subject. I did.
It's been a long time since I spent any real time on JavaScript, and if I'm going to be taking a bit out of this project, I wanted to make sure I had come up to speed on JavaScript, and Node as well. The book was laid out well, with all the ideas based on a decent understanding of JavaScript, but not the latest additions. It really read well to me, and I was able to finish it in two days.
So, here's what I needed to do on my 16" MacBook Pro to get things up and running... 🙂
Start off by installing nodenv from Homebrew. This is the equivalent of rvm, and will manage the Node versions nicely for me.
$ brew install nodenv
I then needed to add in the environmental set-up in my ~/.zlogin file by adding:
# now do the nodenv stuff eval "$(nodenv init -)"
right after I set up my PATH and RVM environment things. It's very similar to the RVM approach, with directory-level controls, as well as system-wide defaults.
At that point, I can source my ~/.zlogin and then I'm ready to go. Next, is to install a good, long-term stable (LTS) version of Node:
$ nodenv install 14.15.1
$ nodenv global 14.15.1
where the second command sets that version as the global default for new projects, etc. You can always check the versions with:
$ nodenv versions * 14.15.1 (set by /Users/drbob/.nodenv/version)
Next was to install a few global tools with npm that I'd need:
$ npm install -g express-generator $ npm install -g nodemon $ nodenv rehash
where the first is the basic RESTful pattern for a service, and the latter is a way to run a Node app while monitoring the filesystem for changes to the files, and reloading them automatically. This will no-doubt prove to be exceptionally handy. The rehash command is something he's found to be nexessary when installing new global tools, as they don't seem to properly get picked up in the PATH without it. Fair enough.
At this point, we can make a new project, just to play with the new features in the book. Start by making a directory to put all this, and then use the express-generator to make the skeleton we need:
$ mkdir playground $ cd playground $ express api --no-view
and now, in the api/ directory we have what we need to get started. Simply have npm pull everything down:
$ cd api $ npm install
and we are ready to go.
There is an index.html file in the public/ directory, and we can use that... and running the Node server is as simple as:
$ node bin/www
... this is the log output...
or if we want to use the file-watching version, we can say:
$ nodemon bin/www
... this is the log output...
The port is set in the bin/www script, but I'm guessing the default is port 3000, so if you go to localhost:3000 you'll see the GET calls, and the page. Very slick... very easy. 🙂
Once I get this into a git repo, or start working on a real project/git repo, I'll see if I can get it loaded up on my iPad Pro using play.js - as it appears to be able to run all this, and have a web page to hit it... so that would be very interesting to work with - given the power of the iPad Pro, and the perfect size.
UPDATE: Indeed... once I pused the code to GitHub, and then went into play.js on my iPad Pro, I could Create a new project, from a Git Clone, and putting in the repo location, and the SSH Keys, etc. it all came down. Then it was just resolving the dependencies with the UI, and then setting the "start" command to be the npm command in the package.json, and then it ran.
Open up the play.js web browser, and it's there. On port 3000, just like it's supposed to be. And editing the file, refreshing the page - it's there. No saving, it's just there. Amazing. This is something I could get used to.