NodeJS is used to run JavaScript outside of the browser.
Somethings work differently when using JS in NodeJS then in the browser. This cheat sheet will cover those differences.
To make a file execute using node you can add #!/usr/bin/env node
to the beginning of it.
Run node -v
to see what version of node you have. If you don’t have node you can install it from here.
You can run node script.js
or node script
to run a JS file.
NPM is used to install dependencies/packages for NodeJS code.
npm init |
Sets up a package.json file in order to keep track of needed dependencies. |
npm init -y |
Sets up a package.json file with the default settings. |
npm install <package> |
Installs the package for your project. Puts it in your package.json |
npm install <package>@8.2.4 |
Installs the package with the version 8.2.4 for your project. Puts it in your package.json |
npm i --save-dev <package> |
Installs the package as a dev package. |
npm i -D <package> |
Installs the package as a dev package. Short hand for --save-dev |
npm uninstall <package> |
Uninstalls the package for your project. |
npm install |
Installs all the dependencies if there is a package.json file and if the packages aren’t already installed. |
package-lock.json locks the versions of the packages so that new version don’t brake your code.
Inside the package.json
"scripts": {
"start": "node index.js",
"test": "jest"
}
Running a script
npm run scriptName
To import the exported variable you can do const module = require("./filepath.js")
In order to import from a package you do const package = require("packageName")
You can import json directly const json = require("./file.json")
To use the regular importing syntax you can do npm install @types/node --save-dev
.
To export you use the module.exports object
module.exports = {
function1: aFunctionThatDoesSomething,
variable1: "test",
}
node ./
it will look for index.js first.process.exit(1)
global | Global for all files in your program. Like the window global variable in the browser. |
process | Info and controls over the current node environment. |
console | Log info to the console. |
module | |
Buffer | Used to handle binary data. |
__dirname | Specifies the absolute path of the currently running JS file. |
process.platform
get what platform the node is running on like linux.
process.env.NAME
get the environment variable called NAME
server.address().port
gets the current port the server is running on.
process.argv is an array that contains the command-line arguments.
Terminal: node ./ "This is a test" arg1
index.js: console.log(process.argv)
Console:
[
'/path/to/node/executable',
'/path/to/your/javascript/file.js',
'This is a test',
arg1
]
const { EventEmitter } = require('events')
const eventEmitter = new EventEmitter
eventEmitter.emit("eventName")
eventEmitter.on("eventName", () => {
})
const fs = require('fs')
or with premisses const fs = require('fs').promises
const encodingType = "utf8"
// Blocking/Sync
const file = fs.readFileSync("filepath.txt", encodingType)
// Non-Blocking
fs.readFile("filepath.txt", encodingType, (error, file) => {/* function when file is loaded*/})
// Asynchronous/Promises
async function loadFile() {
const file = await fs.readFile("filepath.txt", encodingType)
}
fs.writeFile("./filepath.txt", "Written data", error => {error ? console.log(error) : false})
fs.appendFile("./filepath.txt", "Written data", error => {error ? console.log(error) : false})
Used to make working with file paths easier.
const path = require("path")
Allows you to use the fetch function in node
const fetch = require('node-fetch')
const {exec, execSync} = require("child_process")
exec("ls", (err, stdout, stderr) => {
if(err) console.error(stderr)
console.log(stdout)
})
const data = execSync("ls") // Outputs as buffer instead of a string
console.log(data.toString())
debugger
anywhere in your codenode --inspect ./yourScript.js