- Do not rely on OS system calls or core utilities without using an abstraction layer.
- Test each OS with virtual machines and continuous integration.
- Instead of
nvm, usenvm-windowsandnpm-windows-upgradeon Windows. nveandnvexecacan be used to run a single command with one or several different Node.js versions.- Run
npm install -g windows-build-toolson Windows when installing C/C++ addons.
π File encoding
- Keep the default encoding as
UTF-8. File/terminal input should either be validated or converted to it (node-chardet). - Use editorconfig.
- Use any characters from cross-platform-terminal-characters
- Avoid printing Unicode characters (including emoji) except through projects like figures and log-symbols.
- Use
os.EOLwhen reading from or writing to a file,\notherwise. - End files with a newline.
- Avoid the
substitute character
(
CTRL-Z) in non-binary files.
π Filesystem
- Use
path.normalize()when writing a file path to a terminal or file. Otherwise use Unix paths (slashes). - Use
url.fileURLToPath()withimport.meta.url. Alternatively, useimport.meta.filenameandimport.meta.dirname. - Only use lowercase
a-z,0-9and-._,=()in filenames. - Avoid paths longer than 260 characters.
- Copy files instead of symlinking them.
- Use
chokidarto watch files. - Avoid
--watch-path - Avoid the
O_NOATIMEandUV_FS_O_FILEMAPflags offs.open() - Avoid
blksize,blocks,mode,uid,gid,atime,atimeMs,ctime,ctimeMs,birthtimeandbirthtimeMsreturned byfs.stat(). - Use
global-cache-dirto retrieve the global cache directory. - Use
env-pathsfor other common directories.
π» Terminal
- Fire shell commands with
execa. - Keep shell commands to simple
command arguments...calls. - Use
npxorexecato fire local binaries. - Outside Node.js (e.g. in
npmscripts), environment variables should be referenced and passed usingcross-env. - Avoid redirecting to a file descriptor with the
stdiooption ofchild_processmethods.
π Security
- Avoid
fs.chmod(),fs.access()(exceptF_OK),fs.open()'smode,fs.mkdir()'soptions.modeandprocess.umask(). - Avoid
os.userInfo().uid|gid,child_process'suidandgid,fs.chown()and theprocessmethodsgetuid(),geteuid(),getgid(),getegid(),setuid(),seteuid(),setgid(),setegid(),getgroups(),setgroups()andinitgroups(). - Avoid
--secure-heap
π‘ Networking / IPC
- Use
error.codeinstead oferror.errno. - Use
fkillto terminate processes. - Only use
process.kill()with the following signals:SIGINT,SIGTERM,SIGKILL,SIGQUITand0. - Only use
process.on(signal)with the following signals:SIGINT,SIGHUPandSIGWINCH. - Use
ps-list,pid-from-portandprocess-existsto find and check for processes. - Sockets / named pipes must be prefixed with
\\.\pipe\on Windows. - TCP servers should not
listen()on a file descriptor. - Do not use
--diagnostic-report-on-signal
ποΈ System
- Use
osNode.js core module andnavigatorwhen needed. If it lacks some information, usesysteminformationinstead. - When using OS-specific logic, identify the current OS with
process.platform. - Do not assume
process.hrtime()is nanoseconds-precise. - Avoid
os.cpus()'stimes.nice,os.loadavg()andprocess.resourceUsage()'svoluntaryContextSwitchesandinvoluntaryContextSwitches.