Skip to content

michelou/nodejs-examples

Repository files navigation

Playing with Node.js on Windows

Node.js project This repository gathers Node.js code examples coming from various websites and books.
It also includes several build scripts (batch files) for experimenting with Node.js on a Windows machine.

Ada, Akka, C++, COBOL, Dafny, Dart, Deno, Docker, Erlang, Flix, Golang, GraalVM, Haskell, Kafka, Kotlin, LLVM, Modula-2, Rust, Scala 3, Spark, Spring, TruffleSqueak, WiX Toolset and Zig are other topics we are continuously monitoring.

Project dependencies

This project depends on the following external software for the Microsoft Windows platform:

Optionally one may also install the following software:

🔎 Git for Windows provides a BASH emulation used to run git from the command line (as well as over 250 Unix commands like awk, diff, file, grep, more, mv, rmdir, sed and wc).

For instance our development environment looks as follows (November 2024) 2:

C:\opt\ConEmu\                   ( 26 MB)
C:\opt\Git\                      (391 MB)
C:\opt\mongodb\                  (2.0 GB)
C:\opt\mongosh\                  (137 MB)
C:\opt\node-v18.20.4-win-x64\    ( 78 MB)
C:\opt\node-v20.18.0-win-x64\    ( 77 MB)
C:\opt\node-v22.11.0-win-x64\    ( 89 MB
C:\opt\VSCode\                   (381 MB)

Installation policy
When possible we install software from a Zip archive rather than via a Windows installer. In our case we defined C:\opt\ as the installation directory for optional software tools (in reference to the /opt/ directory on Unix).

Directory structure

This project is organized as follows:

bin\
docs\
samples\{README.md, auth-passport, ..}
samples_Bojinov\{README.md, contacts-1-JSON, ..}
samples_Cook\{README.md, 03_basic_auth, ..}
samples_Duuna\{README.md, chp-3-networking, ..}
samples_Lambert\{README.md, 06-files, ..}
samples_Pillora\{README.md, 4-02-project, ..}
README.md
RESOURCES.md
setenv.bat

where

We also define a virtual drive – e.g. drive N: – in our working environment in order to reduce/hide the real path of our project directory (see article "Windows command prompt limitation" from Microsoft Support).

🔎 We use the Windows external command subst to create virtual drives; for instance:

> subst N: %USERPROFILE%\workspace\nodejs-examples

In the next section we give a brief description of the batch files present in this project.

Batch commands

We distinguish different sets of batch commands:

  1. setenv.bat - This batch command makes the external tools such as node.exe, npm.cmd directly available from the command prompt.

     > setenv help
     Usage: setenv { <option> | <subcommand> }
      
       Options:
         -debug      print commands executed by this script
         -verbose    print progress messages
      
       Subcommands:
         help        print this help message
      
     > where node npm
     C:\opt\node-v18.20.4-win-x64\node.exe
     C:\opt\node-v18.20.4-win-x64\npm
     C:\opt\node-v18.20.4-win-x64\npm.cmd
  2. bin\check-outdated.bat prints out outdated package dependencies for all project directories (i.e. directories containing file package.json).

    > bin\check-outdated help
    Usage: check-outdated { <option> | <subcommand> }
     
      Options:
        -debug      print commands executed by this script
        -install    install latest package (if outdated)
        -timer      print total execution time
        -verbose    print progress messages
     
      Subcommands:
        help        print this help message
  3. samples\setenv.bat - This batch command works the same way as in project root directory (point 1) with possibly additional tools (e.g. mongod.exe or siege.exe).

Usage examples

setenv.bat 3

We execute command setenv.bat once to setup your development environment:

> setenv
Tool versions:
   node v18.20.4, npm 10.8.2, node v20.17.0, npm 10.8.2,
   mongod v7.0.12, mongosh 2.3.3, code 1.95.2,
   git 2.47.0, diff 3.10, bash 5.2.37(1)
 
> where mongosh npm
C:\opt\mongosh\bin\mongosh.exe
C:\opt\node-v18.20.4-win-x64\npm
C:\opt\node-v18.20.4-win-x64\npm.cmd

Command setenv.bat -verbose also displays the tool paths:

> setenv -verbose
Tool versions:
   node v18.20.4, npm 10.8.2, node v20.17.0, npm 10.8.2,
   mongod v7.0.12, mongosh 2.3.3, code 1.95.2,
   git 2.47.0, diff 3.10, bash 5.2.37(1)
Tool paths:
   C:\opt\node-v18.20.4-win-x64\node.exe
   C:\opt\node-v18.20.4-win-x64\npm.cmd
   C:\opt\node-v20.17.0-win-x64\node.exe
   C:\opt\node-v20.17.0-win-x64\npm.cmd
   C:\opt\mongodb\bin\mongod.exe
   C:\opt\mongosh\bin\mongosh.exe
   C:\opt\Git\bin\git.exe
   C:\opt\Git\mingw64\bin\git.exe
   C:\opt\Git\usr\bin\diff.exe
Environment variables:
   "GIT_HOME="C:\opt\Git"
   "MONGODB_HOME=C:\opt\mongodb"
   "MONGOSH_HOME=C:\opt\mongosh"
   "NODE_HOME=C:\opt\node-v20.17.0-win-x64"
   "NODE18_HOME=C:\opt\node-v18.20.4-win-x64"
   "NODE20_HOME=C:\opt\node-v20.17.0-win-x64"
   "NODE22_HOME=C:\opt\node-v20.17.0-win-x64"
   "VSCODE_HOME=C:\opt\VSCode"
Path associations:
   Q:\: => %USERPROFILE%\workspace-perso\nodejs-examples

bin\check-outdated.bat

Command bin\check-outdated.bat visits all project directories and prints out outdated package dependencies. For instance we see in the following output that package eslint-plugin-node is outdated in several projects:

> bin\check-outdated
directory samples\auth-passport\
directory samples\locales-1\
directory samples\locales-2\
directory samples\mongoose-default-connection\
directory samples\webaudio-sample\
   outdated package eslint-plugin-node: current=9.2.0, latest=10.0.0
directory samples_Bojinov\contacts-1-JSON\
directory samples_Bojinov\contacts-2-LevelDB\
directory samples_Bojinov\contacts-3-LevelDB2\
directory samples_Bojinov\contacts-4-Mongoose\
directory samples_Bojinov\contacts-5-MongoDB\
   outdated package eslint-plugin-node: current=9.2.0, latest=10.0.0
directory samples_Bojinov\contacts-6-Image\
[...]

Command bin\check-outdated.bat -install also updates the outdated package dependencies (and file package.json).

samples\setenv.bat

Command samples\setenv.bat -verbose inside project directory samples\ also adds the mongod.exe tool to the path:

> cd
N:\samples
 
> setenv -verbose
Tool versions:
   node v18.20.4, npm 10.8.2, node v20.17.0, npm 10.8.2,
   mongod v7.0.12, mongosh 2.3.3, code 1.95.2,
   git 2.47.0, diff 3.10, bash 5.2.37(1)
Tool paths:
   C:\opt\node-v18.20.4-win-x64\node.exe
   C:\opt\node-v18.20.4-win-x64\npm.cmd
   C:\opt\node-v20.17.0-win-x64\node.exe
   C:\opt\node-v20.17.0-win-x64\npm.cmd
   C:\opt\mongodb\bin\mongod.exe
   C:\opt\mongosh\bin\mongosh.exe
   C:\opt\Git\bin\git.exe
   C:\opt\Git\mingw64\bin\git.exe
   C:\opt\Git\usr\bin\diff.exe

npm.cmd

Command npm.cmd works as expected inside every project directory; for instance in project samples\webaudio-sample\.

> cd
N:\samples\webaudio-sample

> npm install -audit
audited 406 packages in 2.527s
found 0 vulnerabilities

> npm start

> [email protected] start N:\samples\webaudio-sample
> node ./npm_scripts/start_browser.js && node .

Module search path: N:\samples_Bojinov\\node_modules
Express server listening on port 8180
Returning Paradise.m4a for request /music
[...]

🔎 From time to time we also run one of the following command to search for possible syntax or logic errors in our JavaScript code:

> npm run lint
 
> [email protected] lint N:\samples\webaudio-sample
> eslint app

Footnotes

[1] Node.js LTS (Long Term Support)

We make the choice to work with the latest LTS version of Node.js. We started this project with version 8 LTS and successively upgraded it to version 10, 12, 14, 16 and 18 LTS.
LTS versionInitial ReleaseEnd-of-life
24.xApril 2025April 2028
22.xApril 2024April 2027
20.xApril 2023April 2026
18.xApril 2022April 2025
16.xOctober 2021April 2024
14.xOctober 2020April 2023
12.xOctober 2019April 2022
10.xOctober 2018April 2021
8.xOctober 2017December 2019
Node.js version 14 LTS has been announced in April 2020.

[2] Downloads

In our case we downloaded the following installation files (see section 1):
mongodb-windows-x86_64-7.0.12.zip  (336 MB)
mongosh-2.3.3-win32-x64.zip        ( 33 MB)
node-v18.20.4-win-x64.zip          ( 27 MB)
node-v20.17.0-win-x64.zip          ( 28 MB)
node-v22.11.0-win-x64.zip          ( 32 MB)
PortableGit-2.47.0-64-bit.7z.exe   ( 42 MB)
VSCode-win32-x64-1.95.2.zip        (131 MB)

[3] setenv.bat usage

Batch file setenv.bat has specific environment variables set that enable us to use command-line developer tools more easily.
It is similar to the setup scripts described on the page "Visual Studio Developer Command Prompt and Developer PowerShell" of the Visual Studio online documentation.
For instance we can quickly check that the two scripts Launch-VsDevShell.ps1 and VsDevCmd.bat are indeed available in our Visual Studio 2019 installation :
> where /r "C:\Program Files (x86)\Microsoft Visual Studio" *vsdev*
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Launch-VsDevShell.ps1
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\vsdevcmd\core\vsdevcmd_end.bat
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\vsdevcmd\core\vsdevcmd_start.bat
Concretely, in our GitHub projects which depend on Visual Studio (e.g. michelou/cpp-examples), setenv.bat does invoke VsDevCmd.bat (resp. vcvarall.bat for older Visual Studio versions) to setup the Visual Studio tools on the command prompt.

mics/November 2024  

About

Code samples from various Node.js books

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages