Skip to main content

Node.js

Node.js​

  • Node.js is
    • cross-platform: can run on Windows, Linux, Unix, macOS, and more.
    • a back-end JavaScript runtime environment
      • runs on the V8 JavaScript Engine
      • executes JavaScript code outside a web browser.
    • lets developers use JavaScript to
      • write command line tools and
      • for server-side scripting.
        • The ability to run JavaScript code on the server is often used to generate dynamic web page content before the page is sent to the user's web browser.

nvm​

Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

commanddescription
`curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.shbash`
nvm lsFind out which versions of Node.js you may have installed and which one of those you're currently using
nvm ls-remoteList all versions of Node.js available for installation (for Windows: nvm ls available)
nvm install 8.1.0install Node.js v8.1.0
nvm install 8.1.0 --latest-npminstall Node.js v8.1.0 and update npm
nvm install-latest-npmupdate current npm (get the latest supported npm version on the current node version)
nvm use 4.2Choose version. Here: set v4.2.0 as the active version
nvm alias default 16.14.2set default node.js version 16.14.2
nvm alias default lts/iron

npm​

Node Package Manager.

commanddescription
npm i -g npmDo not run this! Use nvm instead. Update npm.
npm install (in a package directory, no arguments)Install the dependencies to the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package. By default, npm install will install all modules listed as dependencies in package.json.
npm installsee doc
npm install --savesee stackoverflow
npm Uninstall pkgUninstall a package
npm initthis tells npm to make a file in your folder called package.json that is going to help you organise these dependencies.
npm startThis runs a predefined command specified in the "start" property of a package's "scripts" object. If the "scripts" object does not define a "start" property, npm will run node server.js. (Warning: sometimes Firefox does not update page changes (eg. when replacing the favicon with a different favicon that has the same file name). In this case you have to clear the site data under "Privacy & Security" → "Cookies and Site Data" → "Manage Data" → search for your website → "Remove Selected" and then reload the page.)
npm listdoes not show much, better use npm list --depth=1
`npm list --depth=1grep css`
npm run build
npm run serve

Semantic Versioning​

  • watch

  • read

  • New package versions may contain different "kinds of updates"

    • "patch"
    • "minor release"
    • "major release"
  • SemVer is a way to communicate

    • as a package publisher:
      • to indicate in the SemVer version number which of the above "kinds of updates" the new published package version contains
    • as a package user:
      • to limit via the SemVer version number which "kind of package updates" will be accepted when we update dependencies

npx​

npm install -g npx