Node Handbook
Node Handbook
Preface
The [Link] Handbook
Conclusion
Preface
The [Link] Handbook follows the 80/20 rule: learn in 20% of the time the
80% of a topic.
Enjoy!
The [Link] Handbook
1. Introduction to [Link]
1.1. [Link] has a vast number of libraries
1.2. An example [Link] application
1.3. [Link] frameworks and tools
2. A brief history of [Link]
2.0.1. 2009
2.0.2. 2010
2.0.3. 2011
2.0.4. 2012
2.0.5. 2013
2.0.6. 2014
2.0.7. 2015
2.0.8. 2016
2.0.9. 2017
2.0.10. 2018
2.0.11. 2019
2.0.12. 2020
2.0.13. 2021
2.0.14. 2022
3. How to install [Link]
4. How much JavaScript do you need to know to use Node?
5. Differences between Node and the Browser
6. The V8 JavaScript Engine
6.1. Other JS engines
6.2. The quest for performance
6.3. Compilation
7. Run [Link] scripts from the command line
7.1. Restart the application automatically
8. How to exit from a [Link] program
9. How to read environment variables from [Link]
10. Where to host a [Link] app
10.1. Simplest option ever: local tunnel
10.2. Zero configuration deployments
10.2.1. Glitch
10.2.2. Codepen
10.3. Serverless
10.4. PAAS
10.4.1. Zeit Now
10.4.2. Nanobox
10.4.3. Heroku
10.4.4. Microsoft Azure
10.4.5. Google Cloud Platform
10.5. Virtual Private Server
10.6. Bare metal
11. How to use the [Link] REPL
11.1. Use the tab to autocomplete
11.2. Exploring JavaScript objects
11.3. Explore global objects
11.4. The _ special variable
11.5. The Up arrow key
11.6. Dot commands
11.7. Run REPL from JavaScript file
12. Node, accept arguments from the command line
13. Output to the command line using Node
13.1. Basic output using the console module
13.2. Clear the console
13.3. Counting elements
13.4. Reset counting
13.5. Print the stack trace
13.6. Calculate the time spent
13.7. stdout and stderr
13.8. Color the output
13.9. Create a progress bar
14. Accept input from the command line in Node
15. An introduction to the npm package manager
15.1. Introduction to npm
15.2. Downloads
15.2.1. Installing all dependencies
15.2.2. Installing a single package
15.2.3. Updating packages
15.3. Versioning
15.4. Running Tasks
16. Where does npm install the packages?
17. How to use or execute a package installed using npm
18. The [Link] guide
18.1. The file structure
18.2. Properties breakdown
18.2.1. name
18.2.2. author
18.2.3. contributors
18.2.4. bugs
18.2.5. homepage
18.2.6. version
18.2.7. license
18.2.8. keywords
18.2.9. description
18.2.10. repository
18.2.11. main
18.2.12. private
18.2.13. scripts
18.2.14. dependencies
18.2.15. devDependencies
18.2.16. engines
18.2.17. browserslist
18.2.18. Command-specific properties
18.3. Package versions
19. The [Link] file
19.1. An example
20. Find the installed version of an npm package
21. Install an older version of an npm package
22. Update all the Node dependencies to their latest version
22.1. How Packages Become Dependencies
22.2. Update All Packages to the Latest Version
23. Semantic Versioning using npm
24. Uninstalling npm packages with npm uninstall
40.0.2. [Link]()
40.0.3. [Link]()
40.0.4. [Link]()
40.0.5. [Link]()
40.0.6. [Link]()
40.0.7. [Link]()
40.0.8. [Link]()
40.0.9. [Link]()
40.0.10. [Link]()
41.2. [Link]()
41.3. [Link]()
41.4. [Link]()
41.5. [Link]()
41.6. [Link]()
41.7. [Link]()
41.8. [Link]()
41.9. [Link]()
41.10. [Link]()
41.11. [Link]()
41.12. [Link]()
41.13. [Link]()
41.14. [Link]()
42.2. [Link]()
42.3. [Link]()
42.4. [Link]()
42.5. [Link]()
42.6. [Link]()
42.7. [Link]()
42.8. [Link]()
42.9. [Link]()
42.10. [Link]()
42.11. [Link]()
42.12. [Link]()
42.13. [Link]()
42.14. [Link]()
43. The Node http module
43.1. Properties
43.1.1. [Link]
43.1.2. http.STATUS_CODES
43.1.3. [Link]
43.2. Methods
43.2.1. [Link]()
43.2.2. [Link]()
43.2.3. [Link]()
43.3. Classes
43.3.1. [Link]
43.3.2. [Link]
43.3.3. [Link]
43.3.4. [Link]
43.3.5. [Link]
[Link] runs the V8 JavaScript engine, the core of Google Chrome, outside
of the browser. This allows [Link] to be very performant.
A [Link] app runs in a single process, without creating a new thread for
every request. [Link] provides a set of asynchronous I/O primitives in its
standard library that prevent JavaScript code from blocking and generally,
libraries in [Link] are written using non-blocking paradigms, making
blocking behavior the exception rather than the norm.
When [Link] performs an I/O operation, like reading from the network,
accessing a database or the filesystem, instead of blocking the thread and
wasting CPU cycles waiting, [Link] will resume the operations when the
response comes back.
[Link] = 200
[Link]('Content-Type', 'text/plain')
[Link]('Hello World\n')
})
})
To run this snippet, save it as a [Link] file and run node [Link]
in your terminal.
The first provides the request details. In this simple example, this is not
used, but you could access the request headers and request data.
[Link] = 200
[Link]('Content-Type', 'text/plain')
Express, one of the most simple yet powerful ways to create a web
server. Its minimalist approach, unopinionated, focused on the core
features of a server, is key to its success.
Meteor, an incredibly powerful full-stack framework, powering you
with an isomorphic approach to building apps with JavaScript, sharing
code on the client and the server. Once an off-the-shelf tool that
provided everything, now integrates with frontend libs React, Vue and
Angular. Can be used to create mobile apps as well.
koa, built by the same team behind Express, aims to be even simpler
and smaller, building on top of years of knowledge. The new project
born out of the need to create incompatible changes without disrupting
the existing community.
[Link], a framework to render server-side rendered React
applications.
Micro, a very lightweight server to create asynchronous HTTP
microservices.
[Link], a real-time communication engine to build network
applications.
SvelteKit: Sapper is a framework for building web applications of all
sizes, with a beautiful development experience and flexible filesystem-
based routing. Offers SSR and more!
Remix: Remix is a fullstack web framework for building excellent
user experiences for the web. It comes out of the box with everything
you need to build modern web applications (both frontend and
backend) and deploy them to any JavaScript-based runtime
environment (including [Link]).
Fastify a fast and efficient web framework highly focused on
providing the best developer experience with the least overhead and a
powerful plugin architecture, inspired by Hapi and Express.
2. A brief history of [Link]
Believe it or not, [Link] is only 13 years old.
13 years isn't a very long time in tech, but [Link] seems to have been
around forever.
In this post, we draw the big picture of [Link] in its history, to put things in
perspective.
Part of the business model of Netscape was to sell Web Servers, which
included an environment called Netscape LiveWire that could create
dynamic pages using server-side JavaScript. Unfortunately, Netscape
LiveWire wasn't very successful and server-side JavaScript wasn't
popularized until recently, by the introduction of [Link].
One key factor that led to the rise of [Link] was the timing. Just a few
years earlier, JavaScript had started to be considered as a more serious
language, thanks to "Web 2.0" applications (such as Flickr, Gmail, etc.) that
showed the world what a modern experience on the web could be like.
JavaScript engines also became considerably better as many browsers
competed to offer users the best performance. Development teams behind
major browsers worked hard to offer better support for JavaScript and find
ways to make JavaScript run faster. The engine that [Link] uses under the
hood, V8 (also known as Chrome V8 for being the open-source JavaScript
engine of The Chromium Project), improved significantly due to this
competition.
[Link] happened to be built in the right place and right time, but luck isn't
the only reason why it is popular today. It introduces a lot of innovative
thinking and approaches for JavaScript server-side development that have
already helped many developers.
2.0.1. 2009
[Link] is born
The first version of npm
2.0.2. 2010
Express is born
[Link] is born
2.0.3. 2011
npm hits version 1.0
Big companies start adopting Node: LinkedIn, Uber, etc
2.0.4. 2012
Adoption continues very rapidly
2.0.5. 2013
First big blogging platform using Node: Ghost
Koa is born
2.0.6. 2014
The Big Fork: [Link] is a major fork of [Link], with the goal of
introducing ES6 support and moving faster
2.0.7. 2015
The [Link] Foundation is born
[Link] is merged back into [Link]
Node 4 (no 1, 2, 3 versions were previously released)
2.0.8. 2016
The leftpad incident
Yarn is born
Node 6
2.0.9. 2017
npm focuses more on security
Node 8 - 9
HTTP/2
V8 introduces Node in its testing suite, officially making Node a target
for the JS engine, in addition to Chrome
3 billion npm downloads every week
2.0.10. 2018
Node 10 - 11
ES modules .mjs experimental support
2.0.11. 2019
Node 12 - 13
2.0.12. 2020
Node 14 - 15
GitHub (owned by Microsoft) acquired NPM
2.0.13. 2021
[Link] 16
[Link] 17
2.0.14. 2022
[Link] 18
3. How to install [Link]
[Link] can be installed in different ways.
There you can choose to download an LTS version (LTS stands for Long
Term Support) or the latest available release. As usual, the latest version
contains the latest goodies.
It is also very useful to test your code with old Node versions.
My suggestion is to use the official installer if you are just starting out and
you don't use Homebrew already, otherwise, Homebrew is my favorite
solution because I can easily update node by running brew upgrade node .
In any case, when Node is installed you'll have access to the node
Lexical Structure
Expressions
Types
Variables
Functions
this
Arrow Functions
Loops
Loops and Scope
Arrays
Template Literals
Semicolons
Strict Mode
ECMAScript 6, 2016, 2017
With those concepts in mind, you are well on your road to become a
proficient JavaScript developer, in both the browser and in [Link].
Building apps that run in the browser is a completely different thing than
building a [Link] application.
Despite the fact that it's always JavaScript, there are some key differences
that make the experience radically different.
In the browser, most of the time what you are doing is interacting with the
DOM, or other Web Platform APIs like Cookies. Those do not exist in
Node, of course. You don't have the document , window and all the other
objects that are provided by the browser.
And in the browser, we don't have all the nice APIs that [Link] provides
through its modules, like the filesystem access functionality.
This means that you can write all the modern ES6-7-8-9 JavaScript that
your Node version supports.
Since JavaScript moves so fast, but browsers can be a bit slow and users a
bit slow to upgrade, sometimes on the web, you are stuck to use older
JavaScript / ECMAScript releases.
Going forward ES Modules ( import ) are the way to load modules across
all JavaScript, frontend or backend, but [Link] still supports the require
syntax.
6. The V8 JavaScript Engine
V8 is the name of the JavaScript engine that powers Google Chrome. It's
the thing that takes our JavaScript and executes it while browsing with
Chrome.
The cool thing is that the JavaScript engine is independent by the browser
in which it's hosted. This key feature enabled the rise of [Link]. V8 was
chosen to be the engine that powered [Link] back in 2009, and as the
popularity of [Link] exploded, V8 became the engine that now powers an
incredible amount of server-side code written in JavaScript.
On the web, there is a race for performance that's been going on for years,
and we (as users and developers) benefit a lot from this competition
because we get faster and more optimized machines year after year.
6.3. Compilation
JavaScript is generally considered an interpreted language, but modern
JavaScript engines no longer just interpret JavaScript, they compile it.
This has been happening since 2009, when the SpiderMonkey JavaScript
compiler was added to Firefox 3.5, and everyone followed this idea.
Our applications can now run for hours inside a browser, rather than being
just a few form validation rules or simple scripts.
In this new world, compiling JavaScript makes perfect sense because while
it might take a little bit more to have the JavaScript ready, once done it's
going to be much more performant than purely interpreted code.
7. Run [Link] scripts from the
command line
The usual way to run a [Link] program is to run the globally available
node command (once you install [Link]) and pass the name of the file
you want to execute.
If your main [Link] application file is [Link] , you can call it by typing:
node [Link]
Above, you are explicitly telling the shell to run your script with node .
You can also embed this information into your JavaScript file with a
"shebang" line. The "shebang" is the first line in the file, and tells the OS
which interpreter to use for running the script. Below is the first line of
JavaScript:
#!/usr/bin/node
Above, we are explicitly giving the absolute path of interpreter. Not all
operating systems have node in the bin folder, but all should have env .
You can tell the OS to run env with node as parameter:
#!/usr/bin/env node
// your code
To use a shebang, your file should have executable permission. You can
give [Link] the executable permission by running:
While running the command, make sure you are in the same directory
which contains the [Link] file.
module is used.
npm i -g nodemon
npm i -D nodemon
This local installation of nodemon can be run by calling it from within npm
script such as npm start or using npx nodemon.
nodemon [Link]
8. How to exit from a [Link] program
There are various ways to terminate a [Link] application.
When running a program in the console you can close it with ctrl-C , but
what we want to discuss here is programmatically exiting.
Let's start with the most drastic one, and see why you're better off not using
it.
The process core module provides a handy method that allows you to
programmatically exit from a [Link] program: [Link]() .
When [Link] runs this line, the process is immediately forced to terminate.
This means that any callback that's pending, any network request still being
sent, any filesystem access, or processes writing to stdout or stderr -
all is going to be ungracefully terminated right away.
If this is fine for you, you can pass an integer that signals the operating
system the exit code:
[Link](1)
By default the exit code is 0 , which means success. Different exit codes
have different meaning, which you might want to use in your own system to
have the program communicate to other programs.
You can read more on exit codes at
[Link]
[Link] = 1
and when the program ends, [Link] will return that exit code.
Many times with [Link] we start servers, like this HTTP server:
[Link]('Hi!')
})
Express is a framework that uses the http module under the hood,
[Link]() returns an instance of http. You would use
[Link] if you needed to serve your app using HTTPS, as
[Link] only uses the http module.
This program is never going to end. If you call [Link]() , any
currently pending or running request is going to be aborted. This is not nice.
[Link]('Hi!')
})
ready'))
[Link]('SIGTERM', () => {
[Link](() => {
[Link]('Process terminated')
})
})
What are signals? Signals are a POSIX intercommunication system: a
notification sent to a process in order to notify it of an event that
occurred.
You can send this signal from inside the program, in another function:
[Link]([Link], 'SIGTERM')
Or from another [Link] running program, or any other app running in your
system that knows the PID of the process you want to terminate.
9. How to read environment variables
from [Link]
The process core module of [Link] provides the env property which
hosts all the environment variables that were set at the moment the process
was started.
The below code runs [Link] and set USER_ID and USER_KEY .
That will pass the user USER_ID as 239482 and the USER_KEY as foobar.
This is suitable for testing, however for production, you will probably be
configuring some bash scripts to export variables.
[Link].USER_ID // "239482"
[Link].USER_KEY // "foobar"
In the same way you can access any custom environment variable you set.
If you have multiple environment variables in your node project, you can
also create an .env file in the root directory of your project, and then use
the dotenv package to load them during runtime.
# .env file
USER_ID="239482"
USER_KEY="foobar"
NODE_ENV="development"
In your js file
require('dotenv').config()
[Link].USER_ID // "239482"
[Link].USER_KEY // "foobar"
[Link].NODE_ENV // "development"
You can also run your js file with node -r dotenv/config [Link]
I will list the options from simplest and constrained to more complex and
powerful.
This option is suited for some quick testing, demo a product or sharing of
an app with a very small group of people.
Using it, you can just type ngrok PORT and the PORT you want is exposed
to the internet. You will get a [Link] domain, but with a paid subscription
you can get a custom URL as well as more security options (remember that
you are opening your machine to the public Internet).
10.2.1. Glitch
Glitch is a playground and a way to build your apps faster than ever, and
see them live on their own [Link] subdomain. You cannot currently
have a a custom domain, and there are a few restrictions in place, but it's
really great to prototype. It looks fun (and this is a plus), and it's not a
dumbed down environment - you get all the power of [Link], a CDN,
secure storage for credentials, GitHub import/export and much more.
10.2.2. Codepen
Codepen is an amazing platform and community. You can create a project
with multiple files, and deploy it with a custom domain.
10.3. Serverless
A way to publish your apps, and have no server at all to manage, is
Serverless. Serverless is a paradigm where you publish your apps as
functions, and they respond on a network endpoint (also called FAAS -
Functions As A Service).
To very popular solutions are
Serverless Framework
Standard Library
10.4. PAAS
PAAS stands for Platform As A Service. These platforms take away a lot of
things you should otherwise worry about when deploying your application.
Zeit is an interesting option. You just type now in your terminal, and it
takes care of deploying your application. There is a free version with
limitations, and the paid version is more powerful. You forget that there's a
server, you just deploy the app.
10.4.2. Nanobox
Nanobox
10.4.3. Heroku
Heroku is an amazing platform.
This is a great article on getting started with [Link] on Heroku.
Digital Ocean
Linode
Amazon Web Services, in particular I mention Amazon Elastic
Beanstalk as it abstracts away a little bit the complexity of AWS.
Since they provide an empty Linux machine on which you can work, there
is no specific tutorial for these.
There are lots more options in the VPS category, those are just the ones I
used and I would recommend.
10.6. Bare metal
Another solution is to get a bare metal server, install a Linux distribution,
connect it to the internet (or rent one monthly, like you can do using the
Vultr Bare Metal service)
11. How to use the [Link] REPL
The node command is the one we use to run our [Link] scripts:
node [Link]
If we run the node command without any script to execute or without any
arguments, we start a REPL session:
node
❯ node
>
The command stays in idle mode and waits for us to enter something.
Tip: if you are unsure how to open your terminal, google "How to open
terminal on your-operating-system".
> [Link]('test')
test
undefined
>
The first value, test , is the output we told the console to print, then we
get undefined which is the return value of running [Link]() .
Node read this line of code, evaluated it, printed the result, and then went
back to waiting for more lines of code. Node will loop through these three
steps for every piece of code we execute in the REPL until we exit the
session. That is where the REPL got its name.
Node automatically prints the result of any line of JavaScript code without
the need to instruct it to do so. For example, type in the following line and
press enter:
false
>
Note the difference in the outputs of the above two lines. The Node REPL
printed undefined after executed [Link]() , while on the other
hand, it just printed the result of 5 === '5' . You need to keep in mind that
the former is just a statement in JavaScript, and the latter is an expression.
In some cases, the code you want to test might need multiple lines. For
example, say you want to define a function that generates a random number,
in the REPL session type in the following line and press enter:
function generateRandom() {
...
The Node REPL is smart enough to determine that you are not done writing
your code yet, and it will go into a multi-line mode for you to type in more
code. Now finish your function definition and press enter:
function generateRandom() {
...return [Link]()
undefined
Node will get out of the multi-line mode, and print undefined since there
is no value returned. This multi-line mode is limited. Node offers a more
featured editor right inside the REPL. We discuss it below under Dot
commands.
11.1. Use the tab to autocomplete
The cool thing about the REPL is that it's interactive.
As you write your code, if you press the tab key the REPL will try to
autocomplete what you wrote to match a variable you already defined or a
predefined one.
The REPL will print all the properties and methods you can access on that
class:
11.3. Explore global objects
You can inspect the globals you have access to by typing global. and
pressing tab :
The REPL knows when you are typing a multi-line statement without the
need to invoke .editor .
and you press enter , the REPL will go to a new line that starts with 3
dots, indicating you can now continue to work on that block.
... [Link](num)
... })
If you type .break at the end of a line, the multiline mode will stop and
the statement will not be executed.
Using the repl variable we can perform various operations. To start the
REPL command prompt, type in the following line
[Link]()
> const n = 10
You can pass a string which shows when the REPL starts. The default is '> '
(with a trailing space), but we can define custom prompt.
[Link]('exit', () => {
[Link]('exiting repl')
[Link]()
})
12. Node, accept arguments from the
command line
You can pass any number of arguments when invoking a [Link]
application using
node [Link]
For example:
or
This changes how you will retrieve this value in the [Link] code.
The way you retrieve it is using the process object built into [Link].
All the additional arguments are present from the third position going
forward.
You can iterate over all the arguments (including the node path and the file
path) using a loop:
[Link](`${index}: ${val}`)
})
You can get only the additional arguments by creating a new array that
excludes the first 2 params:
args[0]
In this case:
args[0] is name=joe , and you need to parse it. The best way to do so is
by using the minimist library, which helps dealing with arguments:
[Link] // joe
Install the required minimist package using npm (lesson about the
package manager comes later on).
This time you need to use double dashes before each argument name:
It is basically the same as the console object you find in the browser.
The most basic and most used method is [Link]() , which prints the
string you pass to it to the console.
const x = 'x'
const y = 'y'
[Link](x, y)
For example:
[Link]('My %s has %d ears', 'cat', 2)
Example:
[Link]('%o', Number)
const y = 2
const z = 3
[Link](
'The value of x is ' + x + ' and has been checked .. how many
times?'
[Link](
'The value of x is ' + x + ' and has been checked .. how many
times?'
[Link](
'The value of y is ' + y + ' and has been checked .. how many
times?'
[Link]((fruit) => {
[Link](fruit)
})
[Link]((fruit) => {
[Link](fruit)
})
[Link]((fruit) => {
[Link](fruit)
})
[Link]((fruit) => {
[Link](fruit)
})
[Link]('orange')
[Link]((fruit) => {
[Link](fruit)
})
function1()
This will print the stack trace. This is what's printed if we try this in the
[Link] REPL:
Trace
at function2 (repl:1:33)
at function1 (repl:1:25)
at repl:1:1
at [Link] ([Link]:33)
at [Link] ([Link]:29)
at bound ([Link]:14)
at [Link] ([Link]:10)
at emitOne ([Link]:20)
at [Link] ([Link]:7)
[Link]('doSomething()')
doSomething()
[Link]('doSomething()')
measureDoingSomething()
It will not appear in the console, but it will appear in the error log.
Example:
[Link]('\x1b[33m%s\x1b[0m', 'hi!')
You can try that in the [Link] REPL, and it will print hi! in yellow.
However, this is the low-level way to do this. The simplest way to go about
coloring the console output is by using a library. Chalk is such a library, and
in addition to coloring it also helps with other styling facilities, like making
text bold, italic or underlined.
You install it with npm install chalk@4 , then you can use it:
[Link]([Link]('hi!'))
Check the project link posted above for more usage examples.
This snippet creates a 10-step progress bar, and every 100ms one step is
completed. When the bar completes we clear the interval:
const ProgressBar = require('progress')
[Link]()
if ([Link]) {
clearInterval(timer)
}, 100)
14. Accept input from the command
line in Node
[Link] has a built-in module system.
Any other object or variable defined in the file by default is private and not
exposed to the outer world.
// [Link]
const car = {
brand: 'Ford',
model: 'Fiesta',
[Link] = car
// [Link]
const car = {
brand: 'Ford',
model: 'Fiesta',
[Link] = car
or directly
[Link] = {
brand: 'Ford',
model: 'Fiesta',
And in the other file, you'll use it by referencing a property of your import:
The first exposes the object it points to. The latter exposes the properties of
the object it points to.
require will always return the object that [Link] points to.
// [Link]
[Link] = {
brand: 'Ford',
model: 'Fiesta',
[Link] = {
brand: 'Tesla',
// [Link]
[Link](tesla, ford)
This will print { brand: 'Tesla', model: 'Model S' } undefined since
the require function's return value has been updated to the object that
[Link] points to, so the property that exports added can't be
accessed.
15. An introduction to the npm
package manager
15.1. Introduction to npm
npm is the standard package manager for [Link].
In January 2017 over 350000 packages were reported being listed in the
npm registry, making it the biggest single language code repository on
Earth, and you can be sure there is a package for (almost!) everything.
Yarn and pnpm are alternatives to npm cli. You can check them out as
well.
15.2. Downloads
npm manages downloads of dependencies of your project.
file devDependencies
--no-save installs but does not add the entry to the [Link]
file dependencies
--save-optional installs and adds the entry to the [Link]
file optionalDependencies
--no-optional will prevent optional dependencies from being
installed
npm update
npm will check all packages for a newer version that satisfies your
versioning constraints.
Many times you'll find that a library is only compatible with a major release
of another library.
In all those cases, versioning helps a lot, and npm follows the semantic
versioning (semver) standard.
For example:
{
"scripts": {
"scripts": {
[Link]",
[Link]",
[Link]"
a local install
a global install
the package is installed in the current file tree, under the node_modules
subfolder.
As this happens, npm also adds the lodash entry in the dependencies
When this happens, npm won't install the package under the local folder,
but instead, it will use a global location.
Where, exactly?
The npm root -g command will tell you where that exact location is on
your machine.
If you use nvm to manage [Link] versions, however, that location would
differ.
For example, if your username is 'joe' and you use nvm , then packages
location will show as
/Users/joe/.nvm/versions/node/v8.9.0/lib/node_modules .
17. How to use or execute a package
installed using npm
When you install a package into your node_modules folder using npm ,
or also globally, how do you use it in your [Link] code?
Say you install lodash , the popular JavaScript utility library, using
To use it in your code, you just need to import it into your program using
require :
const _ = require('lodash')
In this case, it will put the executable file under the node_modules/.bin/
folder.
file.
What's that for? What should you know about it, and what are some of the
cool things you can do with it?
{}
things change radically, and you must have a set of properties that will help
other people use it. We'll see more about this later on.
"name": "test-project"
It defines a name property, which tells the name of the app, or package,
that's contained in the same folder where this file lives.
Here's a much more complex example, which was extracted from a sample
[Link] application:
{
"name": "test-project",
"version": "1.0.0",
"main": "src/[Link]",
"private": true,
"scripts": {
build/[Link]",
},
"dependencies": {
"vue": "^2.5.2"
},
"devDependencies": {
"autoprefixer": "^7.1.2",
"babel-core": "^6.22.1",
"babel-eslint": "^8.2.1",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-jest": "^21.0.2",
"babel-loader": "^7.1.1",
"babel-plugin-dynamic-import-node": "^1.2.0",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-es2015-modules-commonjs":
"^6.26.0",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-plugin-transform-vue-jsx": "^3.5.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.22.0",
"chalk": "^2.0.1",
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^0.28.0",
"eslint": "^4.15.0",
"eslint-config-airbnb-base": "^11.3.0",
"eslint-friendly-formatter": "^3.0.0",
"eslint-import-resolver-webpack": "^0.8.3",
"eslint-loader": "^1.7.1",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-vue": "^4.0.0",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^1.1.4",
"friendly-errors-webpack-plugin": "^1.6.1",
"html-webpack-plugin": "^2.30.1",
"jest": "^22.0.4",
"jest-serializer-vue": "^0.3.0",
"node-notifier": "^5.1.2",
"optimize-css-assets-webpack-plugin": "^3.2.0",
"ora": "^1.2.0",
"portfinder": "^1.0.13",
"postcss-import": "^11.0.0",
"postcss-loader": "^2.0.8",
"postcss-url": "^7.2.1",
"rimraf": "^2.6.0",
"semver": "^5.3.0",
"shelljs": "^0.7.6",
"uglifyjs-webpack-plugin": "^1.1.1",
"url-loader": "^0.5.8",
"vue-jest": "^1.0.2",
"vue-loader": "^13.3.0",
"vue-style-loader": "^3.0.1",
"vue-template-compiler": "^2.5.2",
"webpack": "^3.6.0",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-server": "^2.9.1",
"webpack-merge": "^4.1.0"
},
"engines": {
},
All those properties are used by either npm or other tools that we can use.
18.2.1. name
Sets the package name.
Example:
"name": "test-project"
The name must be less than 214 characters, must not have spaces, it can
only contain lowercase letters, hyphens ( - ) or underscores ( _ ).
This is because when a package is published on npm , it gets its own URL
based on this property.
If you published this package publicly on GitHub, a good value for this
property is the GitHub repository name.
18.2.2. author
Lists the package author name
Example:
"author": {
"name": "Joe",
"email": "joe@[Link]",
"url": "[Link]
18.2.3. contributors
As well as the author, the project can have one or more contributors. This
property is an array that lists them.
Example:
([Link]
"contributors": [
"name": "Joe",
"email": "joe@[Link]",
"url": "[Link]
18.2.4. bugs
Links to the package issue tracker, most likely a GitHub issues page
Example:
"bugs": "[Link]
18.2.5. homepage
Sets the package homepage
Example:
{
"homepage": "[Link]
18.2.6. version
Indicates the current version of the package.
Example:
"version": "1.0.0"
The first number is the major version, the second the minor version and the
third is the patch version.
18.2.7. license
Indicates the license of the package.
Example:
"license": "MIT"
18.2.8. keywords
This property contains an array of keywords that associate with what your
package does.
Example:
"keywords": [
"email",
"machine learning",
"ai"
This helps people find your package when navigating similar packages, or
when browsing the [Link] website.
18.2.9. description
This property contains a brief description of the package
Example:
"description": "A package to work with strings"
18.2.10. repository
This property specifies where this package repository is located.
Example:
"repository": "github:whatever/testing",
Notice the github prefix. There are other popular services baked in:
"repository": "gitlab:whatever/testing",
"repository": "bitbucket:whatever/testing",
"type": "git",
"url": "[Link]
"repository": {
"type": "svn",
"url": "..."
18.2.11. main
Sets the entry point for the package.
Example:
"main": "src/[Link]"
18.2.12. private
if set to true prevents the app/package to be accidentally published on
npm
Example:
"private": true
18.2.13. scripts
Defines a set of node scripts you can run
Example:
"scripts": {
build/[Link]",
}
These scripts are command line applications. You can run them by calling
npm run XXXX or yarn XXXX , where XXXX is the command name.
Example: npm run dev .
You can use any name you want for a command, and scripts can do literally
anything you want.
18.2.14. dependencies
Sets a list of npm packages installed as dependencies.
Example:
"dependencies": {
"vue": "^2.5.2"
18.2.15. devDependencies
Sets a list of npm packages installed as development dependencies.
They differ from dependencies because they are meant to be installed
only on a development machine, not needed to run the code in production.
Example:
"devDependencies": {
"autoprefixer": "^7.1.2",
"babel-core": "^6.22.1"
18.2.16. engines
Sets which versions of [Link] and other commands this package/app work
on
Example:
"engines": {
"yarn": "^0.13.0"
18.2.17. browserslist
Is used to tell which browsers (and their versions) you want to support. It's
referenced by Babel, Autoprefixer, and other tools, to only add the polyfills
and fallbacks needed to the browsers you target.
Example:
"browserslist": [
"> 1%",
"last 2 versions",
This configuration means you want to support the last 2 major versions of
all browsers with at least 1% of usage (from the [Link] stats), except
IE8 and lower.
(see more)
18.2.18. Command-specific properties
The [Link] file can also host command-specific configuration, for
example for Babel, ESLint, and more.
That symbol specifies which updates your package accepts, from that
dependency.
Given that using semver (semantic versioning) all versions have 3 digits,
the first being the major release, the second the minor release and the third
is the patch release, you have these "Rules".
You can combine most of the versions in ranges, like this: 1.0.0 ||
>=1.1.0 <1.2.0 , to either use 1.0.0 or one release from 1.1.0 up, but lower
than 1.2.0.
19. The [Link] file
In version 5, npm introduced the [Link] file.
What's that? You probably know about the [Link] file, which is
much more common and has been around for much longer.
If you specify exact versions, like 0.13.0 in the example, you are not
affected by this problem.
It could be you, or another person trying to initialize the project on the other
side of the world by running npm install .
So your original project and the newly initialized project are actually
different. Even if a patch or minor release should not introduce breaking
changes, we all know bugs can (and so, they will) slide in.
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"ansi-regex": {
"version": "3.0.0",
"resolved": "[Link]
regex/-/ansi-regex-3.
[Link]",
"integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg="
},
"cowsay": {
"version": "1.3.1",
"resolved": "[Link]
[Link]"
"integrity": "sha512-
3PVFe6FePVtPj1HTeLin9v8WyLl+VmM1l1H/5P+BTTDkM
Ajufp+0F9eLjzRnOHzVAYeIYFF5po5NjRrgefnRMQ==",
"requires": {
"get-stdin": "^5.0.1",
"optimist": "~0.6.1",
"string-width": "~2.1.1",
"strip-eof": "^1.0.0"
},
"get-stdin": {
"version": "5.0.1",
"resolved": "[Link]
stdin-5.0.
[Link]",
"integrity": "sha1-Ei4WFZHiH/TFJTAwVpPyDmOTo5g="
},
"is-fullwidth-code-point": {
"version": "2.0.0",
"resolved": "[Link]
code-point/-/
[Link]",
"integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8="
},
"minimist": {
"version": "0.0.10",
"resolved":
"[Link]
.tgz",
"integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8="
},
"optimist": {
"version": "0.6.1",
"resolved":
"[Link]
"integrity": "sha1-2j6nRob6IaGaERwybpDrFaAZZoY=",
"requires": {
"minimist": "~0.0.1",
"wordwrap": "~0.0.2"
},
"string-width": {
"version": "2.1.1",
"resolved": "[Link]
width/-/[Link]",
"integrity": "sha512-
nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaT
jAqvVwdfeZ7w7aCvJD7ugkw==",
"requires": {
"is-fullwidth-code-point": "^2.0.0",
"strip-ansi": "^4.0.0"
},
"strip-ansi": {
"version": "4.0.0",
"resolved": "[Link]
ansi/-/[Link]",
"integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=",
"requires": {
"ansi-regex": "^3.0.0"
},
"strip-eof": {
"version": "1.0.0",
"resolved": "[Link]
eof/-/[Link]",
"integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8="
},
"wordwrap": {
"version": "0.0.3",
"resolved":
"[Link]
"integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="
get-stdin
optimist
string-width
strip-eof
In turn, those packages require other packages, as we can see from the
requires property that some have:
ansi-regex
is-fullwidth-code-point
minimist
wordwrap
strip-eof
They are added in alphabetical order into the file, and each one has a
version field, a resolved field that points to the package location, and
an integrity string that we can use to verify the package.
20. Find the installed version of an
npm package
To see the version of all installed npm packages, including their
dependencies:
npm list
For example:
❯ npm list
/Users/joe/dev/node/cowsay
└─┬ cowsay@1.3.1
├── get-stdin@5.0.1
├─┬ optimist@0.6.1
│ ├── minimist@0.0.10
│ └── wordwrap@0.0.3
├─┬ string-width@2.1.1
│ ├── is-fullwidth-code-point@2.0.0
│ └─┬ strip-ansi@4.0.0
│ └── ansi-regex@3.0.0
└── strip-eof@1.0.0
You can also just open the [Link] file, but this involves some
visual scanning.
To get only your top-level packages (basically, the ones you told npm to
install and you listed in the [Link] ), run npm list --depth=0 :
/Users/joe/dev/node/cowsay
└── cowsay@1.3.1
You can get the version of a specific package by specifying its name:
/Users/joe/dev/node/cowsay
└── cowsay@1.3.1
/Users/joe/dev/node/cowsay
└─┬ cowsay@1.3.1
└─┬ optimist@0.6.1
└── minimist@0.0.10
If you want to see what's the latest available version of the package on the
npm repository, run npm view [package_name] version :
1.3.1
21. Install an older version of an npm
package
You can install an old version of an npm package using the @ syntax:
Example:
[ '1.0.0',
'1.0.1',
'1.0.2',
'1.0.3',
'1.1.0',
'1.1.1',
'1.1.2',
'1.1.3',
'1.1.4',
'1.1.5',
'1.1.6',
'1.1.7',
'1.1.8',
'1.1.9',
'1.2.0',
'1.2.1',
'1.3.0',
'1.3.1' ]
22. Update all the Node dependencies
to their latest version
22.1. How Packages Become Dependencies
When you install a package using npm install <packagename> , the latest
version is downloaded to the node_modules folder. A corresponding entry
is added to [Link] and [Link] in the current folder.
npm determines the dependencies and installs their latest versions as well.
Let's say you install cowsay , a nifty command-line tool that lets you make
a cow say things.
When you run npm install cowsay , this entry is added to the
[Link] file:
"dependencies": {
"cowsay": "^1.3.1"
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"cowsay": {
"version": "1.3.1",
"resolved": "[Link]
[Link]",
"integrity": "sha512-
3PVFe6FePVtPj1HTeLin9v8WyLl+VmM1l1H/5P+BTTDkMAjufp+0F9eLjzRnOHz
VAYeIYFF5po5NjRrgefnRMQ==",
"requires": {
"get-stdin": "^5.0.1",
"optimist": "~0.6.1",
"string-width": "~2.1.1",
"strip-eof": "^1.0.0"
Now those 2 files tell us that we installed version 1.3.1 of cowsay, and
our npm versioning rule for updates is ^1.3.1 . This means npm can
update to patch and minor releases: 1.3.2 , 1.4.0 and so on.
If there is a new minor or patch release and we type npm update , the
installed version is updated, and the [Link] file diligently
filled with the new version.
Since npm version 5.0.0, npm update updates [Link] with newer
minor or patch versions. Use npm update --no-save to prevent modifying
[Link] .
Some of those updates are major releases. Running npm update won't
help here. Major releases are never updated in this way because they (by
definition) introduce breaking changes, and npm wants to save you
trouble.
ncu -u
npm install
23. Semantic Versioning using npm
If there's one great thing in [Link] packages, it's that they all agreed on
using Semantic Versioning for their version numbering.
When you make a new release, you don't just up a number as you please,
but you have rules:
you up the major version when you make incompatible API changes
you up the minor version when you add functionality in a backward-
compatible manner
you up the patch version when you make backward-compatible bug
fixes
update .
>
>=
<
<=
||
You can combine some of those notations, for example use 1.0.0 ||
>=1.1.0 <1.2.0 to either use 1.0.0 or one release from 1.1.0 up, but lower
than 1.2.0.
no symbol: you accept only that specific version you specify ( 1.2.1 )
latest : you want to use the latest version available
24. Uninstalling npm packages with
npm uninstall
from the project root folder (the folder that contains the node_modules
Use --no-save option if you don't want to update the [Link] and
[Link] files.
flag:
for example:
local packages are installed in the directory where you run npm
install -g <package-name>
require('package-name')
This makes sure you can have dozens of applications in your computer, all
running a different version of each package if needed.
Updating a global package would make all your projects use the new
release, and as you can imagine this might cause nightmares in terms of
maintenance, as some packages might break compatibility with further
dependencies, and so on.
All projects have their own local version of a package, even if this might
appear like a waste of resources, it's minimal compared to the possible
negative consequences.
You can also install executable commands locally and run them using npx,
but some packages are just better installed globally.
Great examples of popular global packages which you might know are
npm
vue-cli
grunt-cli
mocha
react-native-cli
gatsby-cli
forever
nodemon
When you go in production, if you type npm install and the folder
contains a [Link] file, they are installed, as npm assumes this is a
development deploy.
If you don't want to install npm, you can install npx as a standalone
package
npx lets you run code built with [Link] and published through the npm
registry.
This was a pain because you could not really install different versions of the
same command.
_______
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
This only works if you have the cowsay command globally installed from
npm previously. Otherwise you'll get an error when you try to run the
command.
npx allows you to run that npm command without installing it first. If the
command isn't found, npx will install it into a central cache:
running the vue CLI tool to create new applications and run them:
npx @vue/cli create my-vue-app
react-app my-react-app
This helps to avoid tools like nvm or the other [Link] version
management tools.
You can run code that sits in a GitHub gist, for example:
npx
[Link]
Of course, you need to be careful when running code that you do not
control, as with great power comes great responsibility.
28. The [Link] Event Loop
28.1. Introduction
The Event Loop is one of the most important aspects to understand about
[Link].
The [Link] JavaScript code runs on a single thread. There is just one thing
happening at a time.
This is a limitation that's actually very helpful, as it simplifies a lot how you
program without worrying about concurrency issues.
You just need to pay attention to how you write your code and avoid
anything that could block the thread, like synchronous network calls or
infinite loops.
In general, in most browsers there is an event loop for every browser tab, to
make every process isolated and avoid a web page with infinite loops or
heavy processing to block your entire browser.
The event loop continuously checks the call stack to see if there's any
function that needs to run.
While doing so, it adds any function call it finds in the call stack and
executes each one in order.
You know the error stack trace you might be familiar with, in the debugger
or in the browser console? The browser looks up the function names in the
call stack to inform you which function originates the current call:
[Link]('foo')
bar()
baz()
foo()
foo
bar
baz
as expected.
When this code runs, first foo() is called. Inside foo() we first call
bar() , then we call baz() .
At this point the call stack looks like this:
The event loop on every iteration looks if there's something in the call
stack, and executes it:
until the call stack is empty.
[Link]('foo')
setTimeout(bar, 0)
baz()
foo()
This code prints, maybe surprisingly: foo
baz
bar
When this code runs, first foo() is called. Inside foo() we first call
setTimeout, passing bar as an argument, and we instruct it to run
immediately as fast as it can, passing 0 as the timer. Then we call baz().
The loop gives priority to the call stack, and it first processes
everything it finds in the call stack, and once there's nothing in there, it
goes to pick up things in the message queue.
We don't have to wait for functions like setTimeout , fetch or other things
to do their own work, because they are provided by the browser, and they
live on their own threads. For example, if you set the setTimeout timeout
to 2 seconds, you don't have to wait 2 seconds - the wait happens elsewhere.
Example:
[Link]('foo')
setTimeout(bar, 0)
baz()
foo()
This prints
foo
baz
bar
Every time the event loop takes a full trip, we call it a tick.
// do something
})
When this operation ends, the JS engine runs all the functions passed to
nextTick calls during that operation.
It's the way we can tell the JS engine to process a function asynchronously
(after the current function), but as soon as possible, not queue it.
Calling setTimeout(() => {}, 0) will execute the function at the end of
next tick, much later than when using nextTick() which prioritizes the
call and executes it just before the beginning of the next tick.
Use nextTick() when you want to make sure that in the next event loop
iteration that code is already executed.
30. Understanding setImmediate()
When you want to execute some piece of code asynchronously, but as soon
as possible, one option is to use the setImmediate() function provided by
[Link]: setImmediate(() => {
// run something
})
queue .
[Link]('baz')
[Link]('start')
setImmediate(baz)
resolve('bar')
}).then((resolve) => {
[Link](resolve)
[Link](zoo)
})
[Link](foo)
start()
This code will first call start() , then call foo() in [Link]
queue . After that, it will handle promises microtask queue , which prints
bar and adds zoo() in [Link] queue at the same time.
Then it will call zoo() which has just been added. In the end, the baz()
}, 2000)
setTimeout(() => {
}, 50)
This syntax defines a new function. You can call whatever other function
you want in there, or you can pass an existing function name, and a set of
parameters: const myFunction = (firstParam, secondParam) => {
// do something
}, 2000)
// I changed my mind
clearTimeout(id)
[Link]('after ')
}, 0)
before
after
This is especially useful to avoid blocking the CPU on intensive tasks and
let other functions be executed while performing a heavy calculation, by
queuing functions in the scheduler.
Some browsers (IE and Edge) implement a setImmediate() method
that does this same exact functionality, but it's not standard and
unavailable on other browsers. But it's a standard function in [Link].
31.2. setInterval()
setInterval is a function similar to setTimeout , with a difference:
instead of running the callback function once, it will run it forever, at the
specific time interval you specify (in milliseconds): setInterval(() => {
}, 2000)
The function above runs every 2 seconds unless you tell it to stop, using
clearInterval , passing it the interval id that setInterval returned:
const id = setInterval(() => {
}, 2000)
clearInterval(id)
clearInterval(interval)
}
// otherwise do things
}, 100)
If a function always takes the same amount of time, it's all fine:
// do something
setTimeout(myFunction, 1000)
setTimeout(myFunction, 1000)
On the backend side, [Link] offers us the option to build a similar system
using the events module.
This module, in particular, offers the EventEmitter class, which we'll use
to handle our events.
This object exposes, among many others, the on and emit methods.
[Link]('started')
})
When we run
[Link]('start')
the event handler function is triggered, and we get the console log.
You can pass arguments to the event handler by passing them as additional
arguments to emit() : [Link]('start', (number) => {
[Link](`started ${number}`)
})
[Link]('start', 23)
Multiple arguments:
})
[Link]('start', 1, 100)
The EventEmitter object also exposes several other methods to interact with
events, like
})
r+ open the file for reading and writing, if file doesn't exist it won't
be created.
w+ open the file for reading and writing, positioning the stream at the
beginning of the file. The file is created if not existing.
a open the file for writing, positioning the stream at the end of the
file. The file is created if not existing.
a+ open the file for reading and writing, positioning the stream at the
end of the file. The file is created if not existing.
You can also open the file by using the [Link] method, which
returns the file descriptor, instead of providing it in a callback: const fs =
require('fs')
try {
} catch (err) {
[Link](err)
Once you get the file descriptor, in whatever way you choose, you can
perform all the operations that require it, like calling [Link]() and
many other operations that interact with the filesystem.
You can also open the file by using the promise-based [Link]
let filehandle
try {
[Link]([Link])
}))
} finally {
await [Link]()
example()
example()
You call it passing a file path, and once [Link] gets the file details it will
call the callback function you pass, with 2 parameters: an error message,
and the file stats: const fs = require('fs')
if (err) {
[Link](err)
})
[Link] also provides a sync method, which blocks the thread until the file
stats are ready: const fs = require('fs')
try {
} catch (err) {
[Link](err)
}
The file information is included in the stats variable. What kind of
information can we extract using the stats?
A lot, including:
There are other advanced methods, but the bulk of what you'll use in your
day-to-day programming is this.
const fs = require('fs')
if (err) {
[Link](err)
return
[Link]() // true
[Link]() // false
[Link]() // false
})
You can also use promise-based [Link]() method offered by
the fs/promises module if you like: const fs = require('fs/promises')
try {
[Link]() // true
[Link]() // false
[Link]() // false
} catch (err) {
[Link](err)
example()
35. Node File Paths
Every file in the system has a path.
while Windows computers are different, and have a structure such as:
C:\users\joe\[Link]
You need to pay attention when using paths in your applications, as this
difference must be taken into account.
You include this module in your files using const path = require('path')
Example:
const notes = '/users/joe/[Link]'
[Link](notes) // /users/joe
[Link](notes) // [Link]
[Link](notes) // .txt
You can get the file name without the extension by specifying a second
argument to basename : [Link](notes, [Link](notes)) //
notes
name = 'joe'
'/users/joe/[Link]'
You can get the absolute path calculation of a relative path using
[Link]() : [Link]('[Link]') // '/Users/joe/[Link]' if
In this case [Link] will simply append /[Link] to the current working
directory. If you specify a second parameter folder, resolve will use the
first as a base for the second: [Link]('tmp', '[Link]') //
'/users/[Link]'
Neither resolve nor normalize will check if the path exists. They just
calculate a path based on the information they got.
36. Reading files with Node
The simplest way to read a file in [Link] is to use the [Link]()
method, passing it the file path, encoding and a callback function that will
be called with the file data (and the error): const fs = require('fs')
if (err) {
[Link](err)
return
[Link](data)
})
try {
[Link](data)
} catch (err) {
[Link](err)
}
You can also use the promise-based [Link]() method
offered by the fs/promises module: const fs = require('fs/promises')
try {
encoding: 'utf8' })
[Link](data)
} catch (err) {
[Link](err)
example()
This means that big files are going to have a major impact on your memory
consumption and speed of execution of the program.
In this case, a better option is to read the file content using streams.
37. Writing files with Node
The easiest way to write to files in [Link] is to use the [Link]()
API.
Example:
const fs = require('fs')
if (err) {
[Link](err)
})
try {
[Link]('/Users/joe/[Link]', content)
// file written successfully
} catch (err) {
[Link](err)
try {
} catch (err) {
[Link](err)
example()
By default, this API will replace the contents of the file if it does already
exist.
=> {})
content!'
if (err) {
[Link](err)
// done!
})
require('fs/promises')
try {
const content = 'Some content!'
} catch (err) {
[Link](err)
example()
counterpart) to check if the folder exists and [Link] can access it with its
permissions.
try {
if () {
[Link](folderName)
} catch (err) {
[Link](err)
This piece of code reads the content of a folder, both files and subfolders,
and returns their relative path: const fs = require('fs')
[Link](folderPath)
You can also filter the results to only return the files, and exclude the
folders: const isFile = (fileName) => {
return [Link](fileName).isFile()
[Link](folderPath)
.map((fileName) => {
})
.filter(isFile)
if (err) {
[Link](err)
// done
})
[Link]() is the synchronous version: const fs = require('fs')
try {
[Link]('/Users/joe', '/Users/roger')
} catch (err) {
[Link](err)
require('fs/promises')
try {
} catch (err) {
[Link](err)
example()
Removing a folder that has content can be more complicated than you need.
You can pass the option { recursive: true } to recursively remove the
contents.
const fs = require('fs')
if (err) {
throw err
[Link](`${dir} is deleted!`)
})
const fs = require('fs')
if (err) {
throw err
[Link](`${dir} is deleted!`)
})
Or you can install and make use of the fs-extra module, which is very
popular and well maintained. It's a drop-in replacement of the fs module,
which provides more features on top of it.
Install it using
const fs = require('fs-extra')
[Link](err)
})
// done
})
.catch((err) => {
[Link](err)
})
or with async/await:
try {
await [Link](folder)
// done
} catch (err) {
[Link](err)
removeFolder(folder)
39. The Node fs module
The fs module provides a lot of very useful functionality to access and
interact with the file system.
There is no need to install it. Being part of the [Link] core, it can be used
by simply requiring it: const fs = require('fs')
Once you do so, you have access to all its methods, which include:
[Link]() : check if the file exists and [Link] can access it with
its permissions
[Link]() : append data to a file. If the file does not exist, it's
created
[Link]() : change the permissions of a file specified by the
filename passed. Related: [Link]() , [Link]()
One peculiar thing about the fs module is that all the methods are
asynchronous by default, but they can also work synchronously by
appending Sync .
For example:
[Link]()
[Link]()
[Link]()
[Link]()
if (err) {
return [Link](err)
// done
})
A synchronous API can be used like this, with a try/catch block to handle
errors: const fs = require('fs')
try {
[Link]('[Link]', '[Link]')
// done
} catch (err) {
[Link](err)
}
The key difference here is that the execution of your script will block in the
second example, until the file operation succeeded.
const fs = require('fs')
if (err) {
[Link](err)
return
[Link](data)
if (err2) {
[Link](err2)
return
if (err3) {
[Link](err3)
return
}
[Link](data3)
})
})
})
The callback-based API may rises callback hell when there are too many
nested callbacks. We can simply use promise-based API to avoid it: //
const fs = require('fs/promises')
try {
[Link](data)
[Link](newData)
} catch (err) {
[Link](err)
example()
40. The Node path module
The path module provides a lot of very useful functionality to access and
interact with the file system.
There is no need to install it. Being part of the [Link] core, it can be used
by simply requiring it: const path = require('path')
40.0.1. [Link]()
Return the last portion of a path. A second parameter can filter out the file
extension: require('path').basename('/test/something') // something
require('path').basename('/test/[Link]') // [Link]
require('path').basename('/test/[Link]', '.txt') //
something
40.0.2. [Link]()
require('path').dirname('/test/something/[Link]') //
/test/something
40.0.3. [Link]()
require('path').extname('/test/something/[Link]') // '.txt'
40.0.4. [Link]()
'/Users/joe/[Link]'
require('path').format({ root: '/Users/joe', name: 'test', ext:
'.txt' }) // '/Users/joe/[Link]'
// WINDOWS
// 'C:\\Users\\joe\\[Link]'
40.0.5. [Link]()
require('path').isAbsolute('./test/something') // false
40.0.6. [Link]()
'/users/joe/[Link]'
40.0.7. [Link]()
Tries to calculate the actual path when it contains relative specifiers like .
or .. , or double slashes:
require('path').normalize('/users/joe/..//[Link]') //
'/users/[Link]'
40.0.8. [Link]()
Example:
require('path').parse('/users/[Link]')
results in
root: '/',
dir: '/users',
base: '[Link]',
ext: '.txt',
name: 'test'
}
40.0.9. [Link]()
Accepts 2 paths as arguments. Returns the relative path from the first path
to the second, based on the current working directory.
Example:
require('path').relative('/Users/joe', '/Users/joe/[Link]')
// '[Link]'
require('path').relative('/Users/joe',
'/Users/joe/something/[Link]') // 'something/[Link]'
40.0.10. [Link]()
You can get the absolute path calculation of a relative path using
[Link]() : require('path').resolve('[Link]') //
By specifying a second parameter, resolve will use the first as a base for
the second: require('path').resolve('tmp', '[Link]') //
If the first parameter starts with a slash, that means it's an absolute path:
require('path').resolve('/etc', '[Link]') // '/etc/[Link]'
41. The Node os module
This module provides many functions that you can use to retrieve
information from the underlying operating system and the computer the
program runs on, and interact with it.
const os = require('os')
There are a few useful properties that tell us some key things related to
handling files: [Link] gives the line delimiter sequence. It's \n on
Linux and macOS, and \r\n on Windows.
41.1. [Link]()
Return the string that identifies the underlying architecture, like arm ,
x64 , arm64 .
41.2. [Link]()
Return information on the CPUs available on your system.
Example:
/*
speed: 2400,
times: {
user: 281685380,
nice: 0,
sys: 187986530,
idle: 685833750,
irq: 0,
},
},
speed: 2400,
times: {
user: 282348700,
nice: 0,
sys: 161800480,
idle: 703509470,
irq: 0,
},
},
]
*/
41.3. [Link]()
Return the number of bytes that represent the free memory in the system.
41.4. [Link]()
Return the path to the home directory of the current user.
Example:
'/Users/joe'
41.5. [Link]()
Return the host name.
41.6. [Link]()
Return the calculation made by the operating system on the load average.
Example:
//[3.68798828125, 4.00244140625, 11.1181640625]
41.7. [Link]()
Returns the details of the network interfaces available on your system.
Example:
{ lo0:
[ { address: '[Link]',
netmask: '[Link]',
family: 'IPv4',
mac: 'fe:82:00:00:00:00',
internal: true },
{ address: '::1',
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
family: 'IPv6',
mac: 'fe:82:00:00:00:00',
scopeid: 0,
internal: true },
{ address: 'fe80::1',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: 'fe:82:00:00:00:00',
scopeid: 1,
internal: true } ],
en1:
[ { address: 'fe82::9b:8282:d7e6:496e',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '06:00:00:02:0e:00',
scopeid: 5,
internal: false },
{ address: '[Link]',
netmask: '[Link]',
family: 'IPv4',
mac: '06:00:00:02:0e:00',
internal: false } ],
utun0:
[ { address: 'fe80::2513:72bc:f405:61d0',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: 'fe:80:00:20:00:00',
scopeid: 8,
internal: false } ] }
41.8. [Link]()
Return the platform that [Link] was compiled for:
darwin
freebsd
linux
openbsd
win32
...more
41.9. [Link]()
Returns a string that identifies the operating system release number
41.10. [Link]()
Returns the path to the assigned temp folder.
41.11. [Link]()
Returns the number of bytes that represent the total memory available in the
system.
41.12. [Link]()
Identifies the operating system:
Linux
Darwin on macOS
Windows_NT on Windows
41.13. [Link]()
Returns the number of seconds the computer has been running since it was
last rebooted.
41.14. [Link]()
Returns an object that contains the current username , uid , gid ,
shell , and homedir
42. The Node events module
The events module provides us the EventEmitter class, which is key to
working with events in [Link].
42.1. [Link]()
Alias for [Link]() .
42.2. [Link]()
Emits an event. It synchronously calls every event listener in the order they
were registered.
[Link]('slam') // emitting the event "slam"
42.3. [Link]()
Return an array of strings that represent the events registered on the current
EventEmitter object: [Link]()
42.4. [Link]()
Get the maximum amount of listeners one can add to an EventEmitter
[Link]()
42.5. [Link]()
Get the count of listeners of the event passed as parameter:
[Link]('open')
42.6. [Link]()
Gets an array of listeners of the event passed as parameter:
[Link]('open')
42.7. [Link]()
Alias for [Link]() added in [Link] 10
42.8. [Link]()
Adds a callback function that's called when an event is emitted.
Usage:
[Link]('open', () => {
})
42.9. [Link]()
Adds a callback function that's called when an event is emitted for the first
time after registering this. This callback is only going to be called once,
never again.
const EventEmitter = require('events')
[Link]('my-event', () => {
})
42.10. [Link]()
When you add a listener using on or addListener , it's added last in the
queue of listeners, and called last. Using prependListener it's added, and
called, before other listeners.
42.11. [Link]()
When you add a listener using once , it's added last in the queue of
listeners, and called last. Using prependOnceListener it's added, and
called, before other listeners.
42.12. [Link]()
Removes all listeners of an EventEmitter object listening to a specific
event: [Link]('open')
42.13. [Link]()
Remove a specific listener. You can do this by saving the callback function
to a variable, when added, so you can reference it later: const doSomething
= () => {}
[Link]('open', doSomething)
[Link]('open', doSomething)
42.14. [Link]()
Sets the maximum amount of listeners one can add to an EventEmitter
[Link](50)
43. The Node http module
The HTTP core module is a key module to [Link] networking.
The module provides some properties and methods, and some classes.
43.1. Properties
43.1.1. [Link]
require('http').METHODS
[ 'ACL',
'BIND',
'CHECKOUT',
'CONNECT',
'COPY',
'DELETE',
'GET',
'HEAD',
'LINK',
'LOCK',
'M-SEARCH',
'MERGE',
'MKACTIVITY',
'MKCALENDAR',
'MKCOL',
'MOVE',
'NOTIFY',
'OPTIONS',
'PATCH',
'POST',
'PROPFIND',
'PROPPATCH',
'PURGE',
'PUT',
'REBIND',
'REPORT',
'SEARCH',
'SUBSCRIBE',
'TRACE',
'UNBIND',
'UNLINK',
'UNLOCK',
'UNSUBSCRIBE' ]
43.1.2. http.STATUS_CODES
This property lists all the HTTP status codes and their description: >
require('http').STATUS_CODES
{ '100': 'Continue',
'101': 'Switching Protocols',
'102': 'Processing',
'200': 'OK',
'201': 'Created',
'202': 'Accepted',
'207': 'Multi-Status',
'302': 'Found',
'401': 'Unauthorized',
'403': 'Forbidden',
'409': 'Conflict',
'410': 'Gone',
'423': 'Locked',
43.1.3. [Link]
Points to the global instance of the Agent object, which is an instance of the
[Link] class.
It's used to manage connections persistence and reuse for HTTP clients, and
it's a key component of [Link] HTTP networking.
43.2. Methods
43.2.1. [Link]()
Usage:
const server = [Link]((req, res) => {
})
43.2.2. [Link]()
43.2.3. [Link]()
43.3. Classes
The HTTP module provides 5 classes:
[Link]
[Link]
[Link]
[Link]
[Link]
43.3.1. [Link]
This object makes sure that every request made to a server is queued and a
single socket is reused.
43.3.2. [Link]
43.3.3. [Link]
43.3.4. [Link]
})
The method you'll always call in the handler is end() , which closes the
response, the message is complete and the server can send it to the client. It
must be called on each response.
After processing the headers you can send them to the client by calling
[Link]() , which accepts the statusCode as the first
parameter, the optional status message, and the headers object.
To send data to the client in the response body, you use write() . It will
send buffered data to the HTTP response stream.
If the headers were not sent yet using [Link]() , it will send
the headers first, with the status code and message that's set in the request,
which you can edit by setting the statusCode and statusMessage
43.3.5. [Link]
Streams are not a concept unique to [Link]. They were introduced in the
Unix operating system decades ago, and programs can interact with each
other passing streams through the pipe operator ( | ).
For example, in the traditional way, when you tell the program to read a
file, the file is read into memory, from start to finish, and then you process
it.
Using streams you read it piece by piece, processing its content without
keeping it all in memory.
The [Link] stream module provides the foundation upon which all
streaming APIs are built. All streams are instances of EventEmitter
Using the [Link] fs module, you can read a file, and serve it over HTTP
when a new connection is established to your HTTP server: const http =
require('http')
const fs = require('fs')
[Link](data)
})
})
[Link](3000)
readFile() reads the full contents of the file, and invokes the callback
function when it's done.
[Link](data) in the callback will return the file contents to the HTTP
client.
If the file is big, the operation will take quite a bit of time. Here is the same
thing written using streams: const http = require('http')
const fs = require('fs')
[Link](res)
})
[Link](3000)
Instead of waiting until the file is fully read, we start streaming it to the
HTTP client as soon as we have a chunk of data ready to be sent.
44.4. pipe()
The above example uses the line [Link](res) : the pipe() method
is called on the file stream.
What does this code do? It takes the source, and pipes it into a destination.
You call it on the source stream, so in this case, the file stream is piped to
the HTTP response.
The return value of the pipe() method is the destination stream, which is
a very convenient thing that lets us chain multiple pipe() calls, like this:
[Link](dest1).pipe(dest2)
Readable : a stream you can pipe from, but not pipe into (you can
receive data, but not send data to it). When you push data into a
readable stream, it is buffered, until a consumer starts to read the data.
Writable : a stream you can pipe into, but not pipe from (you can
send data, but not receive from it)
Duplex : a stream you can both pipe into and pipe from, basically a
combination of a Readable and Writable stream
Transform : a Transform stream is similar to a Duplex, but the output
is a transform of its input
You can also implement _read using the read option: const
read( ) {},
})
[Link]('ho!')
44.8. How to create a writable stream
To create a writable stream we extend the base Writable object, and we
implement its _write() method.
next) => {
[Link]([Link]())
next()
[Link](writableStream)
read( ) {},
})
[Link]([Link]())
next()
[Link](writableStream)
[Link]('hi!')
[Link]('ho!')
You can also consume a readable stream directly, using the readable
[Link]([Link]())
})
read( ) {},
})
[Link]([Link]())
next()
[Link](writableStream)
[Link]('hi!')
[Link]('ho!')
[Link]()
event on the readable stream to ensure it is not called before all write events
have passed through the pipe, as doing so would cause an error event to
be emitted. Calling destroy() on the readable stream causes the close
event to be emitted. The listener to the close event on the writable stream
demonstrates the completion of the process as it is emitted after the call to
end() .
44.12. How to create a transform stream
We get the Transform stream from the stream module, and we initialize it
and implement the transform._transform() method.
require('stream')
[Link]([Link]().toUpperCase())
callback()
[Link](transformStream).pipe([Link])
45. Node, the difference between
development and production
You can have different configurations for production and development
environments.
NODE_ENV=production
in the shell, but it's better to put it in your shell configuration file (e.g.
.bash_profile with the Bash shell) because otherwise the setting does not
persist in case of a system restart.
// ...
// ...
// ...
For example, in an Express app, you can use this to set different error
handlers per environment: if ([Link].NODE_ENV === 'development')
true }))
[Link]([Link]())
}
46. Error handling in [Link]
Errors in [Link] are handled through exceptions.
As soon as JavaScript executes this line, the normal program flow is halted
and the control is held back to the nearest exception handler.
or
class NotEnoughCoffeeError extends Error {
// ...
Any exception raised in the lines of code included in the try block is
handled in the corresponding catch block: try {
// lines of code
} catch (e) {}
You can add multiple handlers, that can catch different kinds of errors.
})
You don't need to import the process core module for this, as it's
automatically injected.
.then(doSomething3)
How do you know where the error occurred? You don't really know, but you
can handle errors in each of the functions you call ( doSomethingX ), and
inside the error handler throw a new error, that's going to call the outside
catch handler: const doSomething1 = () => {
// ...
try {
// ...
} catch (err) {
// ...
// handle error
})
})
.then(() => {
// handle error
})
})
try {
await someOtherFunction()
} catch (err) {
[Link]([Link])
}
47. Build an HTTP Server
Here is a sample Hello World HTTP web server: const http =
require('http')
[Link] = 200
[Link]('Content-Type', 'text/html')
[Link]('<h1>Hello, World!</h1>')
})
[Link](port, () => {
})
The server is set to listen on the specified port, 3000 . When the server is
ready, the listen callback function is called.
The callback function we pass is the one that's going to be executed upon
every request that comes in. Whenever a new request is received, the
request event is called, providing two objects: a request (an
[Link] object) and a response (an [Link]
object).
request provides the request details. Through it, we access the request
headers and request data.
response is used to populate the data we're going to return to the client.
[Link] = 200
'text/html')
The simplest way to perform an HTTP request using [Link] is to use the
Axios library: const axios = require('axios')
axios
.get('[Link]
.then((res) => {
[Link](`statusCode: ${[Link]}`)
[Link](res)
})
.catch((error) => {
[Link](error)
})
require('https')
const options = {
hostname: '[Link]',
port: 443,
path: '/todos',
method: 'GET',
[Link](`statusCode: ${[Link]}`)
[Link](d)
})
})
[Link](error)
})
[Link]()
.post('[Link] {
})
.then((res) => {
[Link](`statusCode: ${[Link]}`)
[Link](res)
})
.catch((error) => {
[Link](error)
})
require('https')
})
const options = {
hostname: '[Link]',
port: 443,
path: '/todos',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': [Link],
},
[Link](`statusCode: ${[Link]}`)
[Link](d)
})
})
[Link](error)
})
[Link](data)
[Link]()
If you are using Express, that's quite simple: use the [Link]()
require('axios')
[Link]('[Link] {
})
require('express')
[Link](
[Link]({
extended: true,
})
)
[Link]([Link]())
[Link]([Link])
})
If you're not using Express and you want to do this in vanilla [Link], you
need to do a bit more work, of course, as Express abstracts a lot of this for
you.
The key thing to understand is that when you initialize the HTTP server
using [Link]() , the callback is called when the server got all
the HTTP headers, but not the request body.
So, we must listen for the body content to be processed, and it's processed
in chunks.
We first get the data by listening to the stream data events, and when the
data ends, the stream end event is called, once: const server =
})
[Link]('end', () => {
// end of data
})
})
data += chunk
})
[Link]('end', () => {
[Link]()
})
})
Starting from [Link] v10 a for await .. of syntax is available for use.
It simplifies the example above and makes it look more linear: const
const buffers = []
[Link](chunk)
[Link]()
})
Conclusion
Thanks a lot for reading this book.