NodeJS: Difference between revisions
Created page with "Node.js is a JavaScript environment based on Chrome's V8 JavaScript engine. Node Package Manager (npm) is used to manager node packages installed on your computer or used in y..." |
|||
(36 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Node.js is a JavaScript environment based on Chrome's V8 JavaScript engine. | Node.js is a JavaScript environment based on Chrome's V8 JavaScript engine.<br> | ||
Node Package Manager (npm) is used to | Node Package Manager (npm) is used to manage node packages installed on your computer or used in your node projects.<br> | ||
This page is primarilly on node-specific APIs and usage. See also [[JavaScript]] and [[TypeScript]]. | |||
==Installation== | ==Installation== | ||
===Linux=== | ===Linux=== | ||
Using the [https://github.com/nvm-sh/nvm Node Version Manager (nvm)] is recommended. This | Using the [https://github.com/nvm-sh/nvm Node Version Manager (nvm)] is recommended.<br> | ||
This installs nvm in your home directory. | |||
You should never use sudo when using nvm or npm. | |||
Doing so could lead to permission issues. | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash | curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash | ||
source ~/.bashrc | source ~/.bashrc | ||
nvm install --lts node | nvm install --lts node | ||
nvm install-latest-npm | nvm install-latest-npm | ||
</syntaxhighlight> | |||
===Windows=== | |||
You can [https://nodejs.org/en/ install node directly] or use [https://github.com/coreybutler/nvm-windows nvm-windows].<br> | |||
Note that nvm-windows is not a port of nvm. | |||
<syntaxhighlight lang="bash"> | |||
choco install nvm | |||
nvm install lts | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==Usage== | ==Usage== | ||
JavaScript files can be run directly using <code>node index.js</code> | JavaScript files can be run directly using <code>node index.js</code> | ||
===Filesystem Access=== | |||
Install [https://www.npmjs.com/package/fs-extra fs-extra] to your project. | |||
<syntaxhighlight lang="js"> | |||
const fs = require('fs-extra'); | |||
const path = require('path'); | |||
// Synchronous functions | |||
//// Only in fs-extra | |||
// Remove file or folder (rm -rf) | |||
// Does nothing if path doesn't exist | |||
fs.removeSync('/tmp/fileOrFolder'); | |||
// Creates a directory if it doesn't exist | |||
// This example will create folder1 and folder2 if they don't exist | |||
fs.ensureDirSync('/tmp/folder1/folder2'); | |||
//// Available in node's native 'fs' | |||
// List all files and folders in a directory | |||
fs.readdirSync('/tmp'); | |||
// Ensure file exists | |||
try { | |||
fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK); | |||
console.log('can read/write'); | |||
} catch (err) { | |||
console.error('no access!'); | |||
} | |||
// Read an entire text file as a string | |||
fs.readFileSync('temp.txt', 'utf8'); | |||
</syntaxhighlight> | |||
====Paths==== | |||
* <code>path.extname</code> | |||
===Writing to a file=== | |||
[https://nodejs.org/api/fs.html Reference]<br> | |||
<syntaxhighlight lang="javascript"> | |||
const fs = require('fs-extra'); | |||
let pw = 'My_password_is_kkk'; | |||
fs.writeFileSync('pw.txt', pw); | |||
</syntaxhighlight> | |||
===Logging=== | |||
Mostly the same as web JavaScript.<br> | |||
[https://nodejs.org/api/console.html Reference] | |||
<syntaxhighlight lang="js"> | |||
console.log("a", "b"); | |||
console.error("to stderr"); | |||
</syntaxhighlight> | |||
===CSV=== | |||
Install csv from npm.<br> | |||
[https://csv.js.org/parse/api/ Reference] | |||
<syntaxhighlight lang="js"> | |||
</syntaxhighlight> | |||
===Running other processes=== | |||
You can invoke other processes or CLI programs using <code>spawn</code> or <code>spawnSync</code> | |||
<syntaxhighlight lang="javascript"> | |||
const {spawn, spawnSync} = require('child_process'); | |||
</syntaxhighlight> | |||
Example: | |||
<syntaxhighlight lang="javascript"> | |||
spawnSync("ffmpeg", flatten([ | |||
"-i", file, | |||
"-c:v libx265 -crf 28 -preset medium -c:a libopus -b:a 128K".split(" "), | |||
"-y", output_path | |||
]), { | |||
cwd: process.cwd(), | |||
env: process.env, | |||
stdio: [process.stdin, process.stdout, process.stderr], | |||
encoding: 'utf-8' | |||
}); | |||
</syntaxhighlight> | |||
==Debugging== | |||
===Inspector=== | |||
* Add the <code>--inspect</code> flag to your node call (before the name of the script). | |||
* Then go to <code>chrome://inspect</code> | |||
==Package Management== | |||
===NPM=== | ===NPM=== | ||
To add node packages to an new project. Use <code>npm init</code><br> | To add node packages to an new project. Use <code>npm init</code><br> | ||
To download the dependencies for a node project, run <code>npm install</code> or <code>npm ci</code>. | To download the dependencies for a node project, run <code>npm install</code> or <code>npm ci</code>.<br> | ||
<code>npm install</code> installs the latest dependencies listed in <code>package.json</code> subject to the requirements listed in that file. It also updates <code>package-lock.json</code>.<br> | <code>npm install</code> installs the latest dependencies listed in <code>package.json</code> subject to the requirements listed in that file. It also updates <code>package-lock.json</code>.<br> | ||
<code>npm ci</code> installs exact versions from the <code>package-lock.json</code>. | <code>npm ci</code> installs exact versions from the <code>package-lock.json</code>. | ||
===Yarn=== | |||
[https://yarnpkg.com/en/ Yarn] is an alternative to npm. It caches packages on disk to speed up installs. | |||
While you can migrate from npm to yarn, it is recommended to stick to a single package manager since they each use their own lockfile. | |||
===pnpm=== | |||
[https://pnpm.js.org pnpm] uses symlinks to a single copy of each dependency to save disk space. | |||
pnpm uses its own lockfile instead of packages-lock.json. | |||
===Publishing=== | |||
See [[Npm (software)#Publishing]]. | |||
==Useful Packages== | |||
===fs-extra=== | |||
[https://www.npmjs.com/package/fs-extra fs-extra] adds additional commands for accessing the filesystem such as copying and moving files. | |||
===node-fetch=== | |||
[https://www.npmjs.com/package/node-fetch node-fetch]<br> | |||
===file-type=== | |||
[https://www.npmjs.com/package/file-type file-type]<br> | |||
[https://www.npmjs.com/package/file-type-cli file-type-cli]<br> | |||
Detects the file type of a file or buffer | |||
===argparse=== | |||
[https://www.npmjs.com/package/argparse argparse] | |||
This is a port of python's argparse. However, it does not support configuration files. | |||
{{hidden | Example | | |||
<syntaxhighlight lang="python"> | |||
const parser = new ArgumentParser({ | |||
version: "0.0.1", | |||
addHelp: true, | |||
description: "Description here" | |||
}); | |||
// Add your arguments here | |||
parser.addArgument(["--config"], { | |||
type: String, | |||
help: "Path to config file (optional)" | |||
}); | |||
const args = (function() { | |||
let myArgs = parser.parseArgs(); | |||
if (myArgs.config && myArgs.config.length > 0) { | |||
const filedata = fs.readFileSync(myArgs.config, "utf8"); | |||
const myArgsArr = filedata | |||
.split("\n") | |||
.filter(x => x.length > 0) | |||
.map(x => "--" + x) | |||
.map(x => x.split("=")) | |||
.flatten(); | |||
myArgs = parser.parseArgs(myArgsArr); | |||
} | |||
return myArgs; | |||
})(); | |||
</syntaxhighlight> | |||
}} | |||
===node-worker-threads-pool=== | |||
[https://www.npmjs.com/package/node-worker-threads-pool Link] | |||
A cool thread pool implementation. |
Latest revision as of 18:47, 10 February 2023
Node.js is a JavaScript environment based on Chrome's V8 JavaScript engine.
Node Package Manager (npm) is used to manage node packages installed on your computer or used in your node projects.
This page is primarilly on node-specific APIs and usage. See also JavaScript and TypeScript.
Installation
Linux
Using the Node Version Manager (nvm) is recommended.
This installs nvm in your home directory.
You should never use sudo when using nvm or npm.
Doing so could lead to permission issues.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
source ~/.bashrc
nvm install --lts node
nvm install-latest-npm
Windows
You can install node directly or use nvm-windows.
Note that nvm-windows is not a port of nvm.
choco install nvm
nvm install lts
Usage
JavaScript files can be run directly using node index.js
Filesystem Access
Install fs-extra to your project.
const fs = require('fs-extra');
const path = require('path');
// Synchronous functions
//// Only in fs-extra
// Remove file or folder (rm -rf)
// Does nothing if path doesn't exist
fs.removeSync('/tmp/fileOrFolder');
// Creates a directory if it doesn't exist
// This example will create folder1 and folder2 if they don't exist
fs.ensureDirSync('/tmp/folder1/folder2');
//// Available in node's native 'fs'
// List all files and folders in a directory
fs.readdirSync('/tmp');
// Ensure file exists
try {
fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK);
console.log('can read/write');
} catch (err) {
console.error('no access!');
}
// Read an entire text file as a string
fs.readFileSync('temp.txt', 'utf8');
Paths
path.extname
Writing to a file
const fs = require('fs-extra');
let pw = 'My_password_is_kkk';
fs.writeFileSync('pw.txt', pw);
Logging
Mostly the same as web JavaScript.
Reference
console.log("a", "b");
console.error("to stderr");
CSV
Install csv from npm.
Reference
Running other processes
You can invoke other processes or CLI programs using spawn
or spawnSync
const {spawn, spawnSync} = require('child_process');
Example:
spawnSync("ffmpeg", flatten([
"-i", file,
"-c:v libx265 -crf 28 -preset medium -c:a libopus -b:a 128K".split(" "),
"-y", output_path
]), {
cwd: process.cwd(),
env: process.env,
stdio: [process.stdin, process.stdout, process.stderr],
encoding: 'utf-8'
});
Debugging
Inspector
- Add the
--inspect
flag to your node call (before the name of the script). - Then go to
chrome://inspect
Package Management
NPM
To add node packages to an new project. Use npm init
To download the dependencies for a node project, run npm install
or npm ci
.
npm install
installs the latest dependencies listed in package.json
subject to the requirements listed in that file. It also updates package-lock.json
.
npm ci
installs exact versions from the package-lock.json
.
Yarn
Yarn is an alternative to npm. It caches packages on disk to speed up installs. While you can migrate from npm to yarn, it is recommended to stick to a single package manager since they each use their own lockfile.
pnpm
pnpm uses symlinks to a single copy of each dependency to save disk space. pnpm uses its own lockfile instead of packages-lock.json.
Publishing
See Npm (software)#Publishing.
Useful Packages
fs-extra
fs-extra adds additional commands for accessing the filesystem such as copying and moving files.
node-fetch
file-type
file-type
file-type-cli
Detects the file type of a file or buffer
argparse
This is a port of python's argparse. However, it does not support configuration files.
const parser = new ArgumentParser({
version: "0.0.1",
addHelp: true,
description: "Description here"
});
// Add your arguments here
parser.addArgument(["--config"], {
type: String,
help: "Path to config file (optional)"
});
const args = (function() {
let myArgs = parser.parseArgs();
if (myArgs.config && myArgs.config.length > 0) {
const filedata = fs.readFileSync(myArgs.config, "utf8");
const myArgsArr = filedata
.split("\n")
.filter(x => x.length > 0)
.map(x => "--" + x)
.map(x => x.split("="))
.flatten();
myArgs = parser.parseArgs(myArgsArr);
}
return myArgs;
})();
node-worker-threads-pool
Link
A cool thread pool implementation.