Dirk Harriman Banner Image


Notes Node JS




Installing Node JS

The most common install downloads are at https://nodejs.dev/en/download/. They include an installer for both Windows and Mac.
One very convenient way to install Node.js is through a package manager. In this case, every operating system has its own. Other package managers for MacOS, Linux, and Windows are listed in https://nodejs.dev/download/package-manager/.
nvm is a popular way to run Node.js. It allows you to easily switch the Node.js version, and install new versions to try and easily rollback if something breaks. It is also very useful to test your code with old Node.js versions.


Javascript in Node JS vs Browser

Both the browser and Node.js use JavaScript as their programming language. Building apps that run in the browser is a completely different thing than building a server-side Node.js application. Despite the fact that it's always JavaScript, there are some key differences that make the experience radically different.

In the browser, most of the time what you are doing is interacting with the DOM, or other Web Platform APIs like Cookies. Those do not exist in Node.js, of course. You don't have the document, window and all the other objects that are provided by the browser.

And in the browser, we don't have all the nice APIs that Node.js provides through its modules, like the filesystem access functionality.

Another big difference is that in Node.js you control the environment. Unless you are building an open source application that anyone can deploy anywhere, you know which version of Node.js you will run the application on. Compared to the browser environment, where you don't get the luxury to choose what browser your visitors will use, this is very convenient.

This means that you can write all the modern ES2015+ JavaScript that your Node.js version supports. Since JavaScript moves so fast, but browsers can be a bit slow to upgrade, sometimes on the web you are stuck with using older JavaScript / ECMAScript releases. You can use Babel to transform your code to be ES5-compatible before shipping it to the browser, but in Node.js, you won't need that.

Another difference is that Node.js supports both the CommonJS and ES module systems (since Node.js v12), while in the browser we are starting to see the ES Modules standard being implemented.

In practice, this means that you can use both require() and import in Node.js, while you are limited to import in the browser.


Running A Node JS Application

Node JS applications do not run on IIS like PHP and ASPX applications do.

Starting A Node JS Application

To start a Node JS server apllication, do the following:

  1. Open a Command Line interface
  2. Navigate to the application's root folder
  3. Figure out the main application file, which will have a "js" extension
  4. Type in "node AppFileName.js"
  5. The response will be something like "Server Started on Port: 3000"
  6. Type the adddress "http://localhost:3000/" in the browser and you should see the application running.
Stopping A Node JS Application

To stop a Node JS server application, type "Ctrl C"


NPM Package Manager

npm is the standard package manager for Node.js.
In September 2022 over 2.1 million packages were reported being listed in the npm registry, making it the biggest single language code repository on Earth, and you can be sure there is a package for (almost!) everything.
It started as a way to download and manage dependencies of Node.js packages, but it has since become a tool used also in frontend JavaScript.


npm manages downloads of dependencies of your project.

Installing All Dependencies

If a project has a package.json file, by running

npm install

it will install everything the project needs, in the node_modules folder, creating it if it does not exist already.

Installing A Single Package

You can also install a specific package by running

npm install <package-name>

Furthermore, since npm 5, this command adds <package-name> to the package.json file dependencies. Before version 5, you needed to add the flag --save.

Some Install Flags
--save installs and adds the entry to the package.json file dependencies. Shorthand: -S
--save-dev installs and adds the entry to the package.json file devDependencies. Shorthand: -D
--no-save installs but does not add the entry to the package.json file dependencies.
--save-optional installs and adds the entry to the package.json file optionalDependencies. Shorthand: -O
--no-optional will prevent optional dependencies from being installed.

The difference between devDependencies and dependencies is that the former contains development tools, like a testing library, while the latter is bundled with the app in production.

As for the optionalDependencies the difference is that build failure of the dependency will not cause installation to fail. But it is your program's responsibility to handle the lack of the dependency.

Updating Packages

Updating is also made easy, by running

npm update

npm will check all packages for a newer version that satisfies your versioning constraints.
You can specify a single package to update as well:

npm update <package-name>


In addition to plain downloads, npm also manages versioning, so you can specify any specific version of a package, or require a version higher or lower than what you need.
Many times you'll find that a library is only compatible with a major release of another library.
Or a bug in the latest release of a lib, still unfixed, is causing an issue.
Specifying an explicit version of a library also helps to keep everyone on the same exact version of a package, so that the whole team runs the same version until the package.json file is updated.
In all those cases, versioning helps a lot, and npm follows the semantic versioning (semver) standard.
You can install a specific version of a package, by running

npm install <package-name>@<version>

Running Tasks

The package.json file supports a format for specifying command line tasks that can be run by using

npm run <task-name>

For example:

{ "scripts": { "start-dev": "node lib/server-development", "start": "node lib/server-production" } }

It's very common to use this feature to run Webpack:

{ "scripts": { "watch": "webpack --watch --progress --colors --config webpack.conf.js", "dev": "webpack --progress --colors --config webpack.conf.js", "prod": "NODE_ENV=production webpack -p --config webpack.conf.js" } }

So instead of typing those long commands, which are easy to forget or mistype, you can run

$ npm run watch $ npm run dev $ npm run prod