Node.js and npm, First Steps
References for this Part
It has learning material as well as the canonical documentation.
Jonathan Wexler. Get Programming with Node.js, Manning, 2019
Flavio Copes. The Node.js Handbook, Flavio Copes, 2018
node
Assuming that you have already installed Node.js,
let us dive in at the deep end first. Somewhere
on your hard drive, you create a project folder,
and navigate to that folder with cd
.
Write a file eg servera.js
:
Example 1. <mydir>/servera.js
|
|
When the file is saved in your project folder. You issue the following command on the CLI:
|
|
or, if you are lazy
|
|
Please notice its response on the command line. Then go to your browser, and navigate to: (http://localhost:3000) and check its content.
This is amazing. A webserver in 14, well really only 11, lines of code. This is what we shall work with if web programming is our endeavour. In Node.js we do not use a generic web server. We build our own project specific servers.
From the Top
After this shockingly easy step, let us
take some baby steps aimed at getting to
know node
from the basics.
In some project directory write a file:
Example 2. <somedir>/js0.js
|
|
then run it by
|
|
resulting in
|
|
You may also do programming experiments live by executing node interactively
|
|
Generally you should never start any program on your
computer befor you know how to exit from it.
You exit the node
interpreter by issuing one of
.exit
- Ctrl+d
- Ctrl+c Ctrl+c
It is obviously easier in the long run to create js files in an editor and the running them as already shown above.
Working interactively you may load js files into
the interpreter with a .load <filename>
command,
and you may save your interactive work with a
.save <filename>
command.
Modules in node
In programming we use modules as a means of storing
generalized code as modules thereby making this code
available for import wherever needed. In node
the
import
construct, as known in browser based JavaScript
is called require
, and the usage is slightly different.
Try this:
Example 3. First Module: messages.js
|
|
Usage:
Example 4. Using the Module, printMessages.js
|
|
Execution:
|
|
Modules are self contained JavaScript files containing code that pertains to a single concept, functionality, or library. (Wexler, 2019)
We create them in order to use them in more than one context.
As such they are a manifestation of the DRY principle. If you
think of frontend JavaScript in the browser, yes, they are very
similar to modules handled by import
, but beware of the syntax
differences. See also (Copes, 2019 pp 43-44).
Syntactical Variations of Export
The following contains some variations in writing modules and the use of them. The choice is yours. You’re the programmer.
Example 5. First Variant
|
|
Usage:
|
|
Example 6. Second Variant
|
|
Usage:
|
|
Example 7: Third Variant
|
|
Usage:
|
|
Example 8. Fourth Variant
|
|
Usage:
|
|
The difference is really only aesthetic. The result in all four cases:
|
|
npm
The npm
documentation is found at (https://docs.npmjs.com)
atly, that the “Node.js package ecosystem is the world’s
largest ecosystem of open source online libraries”.
(https://nodejsera.com/nodejs-tutorial-day7-all-about-npm.html)
is a good video about it.
npm
is often installed automatically when you
install node
. If it does not respond to the following,
install it.
It is essential that we’re always using the latest version
of this software.
Check your version with
|
|
To get the latest you might update via your package manager, npm itself.
|
|
The g flag makes it a global install meaning it is not for the current project only.
npm
Commands
The npm
commands needed at this preliminary stage are
- npm init
- This command create a project description file containing various configuration parameters,
- npm set
- Sets up certain defaults used by npm init.
- npm install
- Installs a package from the npm repository.
- npm update
- Updates packages of the project.
- npm outdated
- Tells you whether some packages need to be updated.
- npm uninstall
- Removes packages if no longer needed.
npm init
When we work with node we need to keep track on the
files needed for the current project and package.json
helps us with that. It is interactively created
with npm init
Example 9. Interactive npm init
|
|
It is non-interactively done as follows. This is based on some configurable best practices parameters.
Example 10. Automatic npm init
|
|
Just like that, no questions asked. The generated file package.json
may be edited and adapted in either case. In a bit we shall see an
example of just that.
npm set
You may remember from ancient history when you first
started using git, you supplied some useful configuration
parameters such as your name and email address. npm
offers
a similar feature. This is a one time effort.
Example 11. npm set
|
|
having done that let us try re-issuing the npm init -y
Example 12. Result of Automated npm init
|
|
npm install
If we install an npm
module in our project eg
Example 13. npm install <package>
|
|
We see in the edited package.json
that a dependencies
section has been added, this is done by npm install
.
This section is used by npm install
(no parameters.)
Example 14. npm install
|
|
installs or updates the modules needed for the project.
Those modules are kept in the directory node_modules
which is created by the first execution of npm install
.
If, for some reason, this directory is deleted, it is
recreated just by running npm install
again.
It is normal procedure to run npm install
as the first
step of each test of the project.
A package name such as bcryptjs
may be suffixed
meaning that the npm install bcryptjs@2.4.3
will install exactly that version whereas
npm install bcryptjs^2.0.0
means that the latest version 2
variant will be installed.
npm update
Example 15. npm update
|
|
in the project folder will update all packages.
npm outdated
Example 16. npm outdated
|
|
will reveal the necessity of an update. Just after an update, its output should, of course, be empty.
npm uninstall
Example 17. npm uninstall
|
|
removes a package.
On all these commands a global flag,
-g
or --global
is applicable here.
Global means that the action will concern all projects,
not just the project in the current directory.