LearningNodeJs

From HerzbubeWiki
Jump to navigation Jump to search

Node.js (pronounced, I think, "node jazz") is a JavaScript runtime that lets you run JavaScript programs outside of their typical runtime environment, the web browser. You can use Node.js to run a server-side web service or simply to run scripts written in JavaScript.

Two key features of Node.js (important for running web services) are that it's event-driven and that it has a non-blocking I/O model.


References


Command line

The following command runs a JavaScript script on the command line:

node foo.js

This runs an inline script:

node -e 'inline javascript code'


npm

npm is a package manager for Node.js. A project usually has dependencies on libraries. These dependencies are noted down in a file named

package.json

If the project itself is also published as an NPM package, the package.json file must contain name, version and other things describing the project. To see all possible options that can appear in a package.json file refer to the NPM documentation. When you start a new project and don't have a package.json file yet, the following command generates a new file after interactively asking you some questions:

npm init

To install the dependencies in an already existing package.json, e.g. after git-cloning a project, invoke this command:

npm install

To install a new package that is not yet in package.json you invoke this command

npm install <package-name>

This will then automatically add the package as a dependency to package.json. The dependencies list contains the package names and the acceptable version numbers for the packages. Version number rules are detailed here and follow the Semantic Versioning principle. For instance in the following snippet the project depends on the package "express" with version 4.17.1, or any newer 4.x version (the caret "^" expresses that the left-most non-zero digit must not change).

  "dependencies": {
    "express": "^4.17.1"
  },

To update an already existing dependency to the newest version allowed by the version number in package.json:

npm update <package-name>

Usually the version number in package.json contains a caret ("^") prefix, so updates to a new major version are not possible with npm update. The options here are

  • Manually edit package.json followed by npm install
  • If you have a major version 4 in package.json, then you can upgrade to the latest major version 5 with this command: npm install <package-name>@5

To see which are the latest versions of packages that are in package.json:

npm outdated

package.json can also contain scripts that can be executed using the npm command. For instance:

"scripts": {
  "build": "tsc",
  "start": "npm run build && node dist/index.js",
},

defines two scripts named "build" and "start". These can be run using the commands

npm run build
npm run start


Modules

TODO: Write something about the modules concept of Node.js

Notes:

  • __dirname looks like a global variable, but it's not. It's a parameter of the invisible function that Node.js wraps around each module, and it contains the absolute path of the folder containing the current module, or you could also say the "currently executing file".
  • "fs" module to read and write from/to the filesystem.
  • "path" module to work with filesystem paths


Platform support

Node.js on macOS

Apparently Node.js has standalone install packages specifically for macOS, however I prefer to install Node.js via Homebrew:

brew install node

This also automatically installs the Node.js package manager npm.


Node.js on Windows

Node.js has a 64-bit MSI installer for Windows.