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.
This project depends on the following external software for the Microsoft Windows platform:
Optionally one may also install the following software:
- ConEmu 2023 (release notes)
- MongoDB Shell 2.3 (change log)
- Node.js 16.x LTS (change log)
- Node.js 20.x LTS (change log)
- Node.js 22.x LTS (change log)
- Visual Studio Code 1.94 (release notes)
🔎 Git for Windows provides a BASH emulation used to run
git
from the command line (as well as over 250 Unix commands likeawk
,diff
,file
,grep
,more
,mv
,rmdir
,sed
andwc
).
For instance our development environment looks as follows (November 2024) 2:
C:\opt\ConEmu\ ( 26 MB) C:\opt\Git\ (315 MB) C:\opt\mongodb\ (1.6 GB) C:\opt\mongosh\ (133 MB) C:\opt\node-v16.20.2-win-x64\ ( 71 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\ (341 MB)
☛ Installation policy
When possible we install software from a Zip archive rather than via a Windows installer. In our case we definedC:\opt\
as the installation directory for optional software tools (in reference to the/opt/
directory on Unix).
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
- directory
bin\
contains utility batch scripts. - directory
docs\
contains Node.js related papers/articles. - directory
samples\
contains Node.js code examples grabbed from various websites. - directory
samples_Bojinov\
contains Node.js code examples from Bojinov's book. - directory
samples_Cook\
contains Node.js code examples from Cook's book. - directory
samples_Duuna\
contains Node.js code examples from Düüna's book. - directory
samples_Lambert\
contains Node.js code examples from Lambert's book. - directory
samples_Pillora\
contains Node.js code examples from Pillora's book. - file
README.md
is the Markdown document for this page. - file
RESOURCES.md
gathers Node.js related informations. - file
setenv.bat
is the batch script for setting up our environment.
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:
-
setenv.bat
- This batch command makes the external tools such asnode.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
-
bin\check-outdated.bat
prints out outdated package dependencies for all project directories (i.e. directories containing filepackage.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
-
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
orsiege.exe
).
Usage examples ▴
setenv.bat
3
We execute command setenv.bat
once to setup your development environment:
> setenv Tool versions: node v16.20.2, npm 9.8.1, node v18.20.4, npm 10.8.2 mongod v7.0.12, mongosh 2.3.0, 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 v16.20.2, npm 9.8.1, node v18.20.4, npm 10.8.2 mongod v7.0.12, mongosh 2.3.0, git 2.47.0, diff 3.10, bash 5.2.37(1) Tool paths: C:\opt\node-v16.20.2-win-x64\node.exe C:\opt\node-v16.20.2-win-x64\npm.cmd C:\opt\node-v18.20.4-win-x64\node.exe C:\opt\node-v18.20.4-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-v18.20.4-win-x64" "NODE16_HOME=C:\opt\node-v16.20.2-win-x64" "NODE18_HOME=C:\opt\node-v18.20.4-win-x64" "NODE20_HOME=C:\opt\node-v20.17.0-win-x64" Path associations: Q:\: => %USERPROFILE%\workspace-perso\nodejs-examples
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
).
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 v16.20.2, npm 9.8.1, node v18.20.4, npm 10.8.2 mongod v7.0.12, mongosh 2.3.0, git 2.47.0, diff 3.10, bash 5.2.37(1) Tool paths: C:\opt\node-v16.20.2-win-x64\node.exe C:\opt\node-v16.20.2-win-x64\npm.cmd C:\opt\node-v18.20.4-win-x64\node.exe C:\opt\node-v18.20.4-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
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 > webaudio-example@1.0.0 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 > async-downloads@0.0.1 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 version Initial Release End-of-life 24.x April 2025 April 2028 22.x April 2024 April 2027 20.x April 2023 April 2026 18.x April 2022 April 2025 16.x October 2021 April 2024 14.x October 2020 April 2023 12.x October 2019 April 2022 10.x October 2018 April 2021 8.x October 2017 December 2019 - Node.js version 14 LTS has been announced in April 2020.
-
- IBM Developer: Node.js 14 release,
by Michael Dawson, April 21, 2020. - Medium: Node.js version 14 available now, Node.js team, April 21, 2020.
- IBM Developer: Node.js 14 release,
[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.0-win32-x64.zip ( 33 MB) node-v16.20.2-win-x64.zip ( 25 MB) node-v18.20.4-win-x64.zip ( 27 MB) node-v20.17.0-win-x64.zip ( 28 MB) PortableGit-2.47.0-64-bit.7z.exe ( 42 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
andVsDevCmd.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 invokeVsDevCmd.bat
(resp.vcvarall.bat
for older Visual Studio versions) to setup the Visual Studio tools on the command prompt.