Stephan Lamoureux
Posted on October 10, 2022
Node.js is a JavaScript runtime environment that executes JavaScript code outside a web browser. It allows us to install packages, run local web servers, create APIs, and more.
Requirements
- WSL/macOS/Linux
- cURL or Wget
π NVM
You will likely need to switch between multiple versions of Node.js based on the needs of the different projects you're working on. Node Version Manager allows you to quickly install and use different versions of Node via the command-line.
Installing NVM
- 1. Open your terminal and install NVM with
curl
orwget
:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
To verify installation, enter: command -v nvm
. This should return nvm
, if you receive command not found
or no response at all, close your current terminal, reopen it, and try again.
- 2. List which versions of Node are currently installed (should be none at this point):
nvm ls
- 3. Install both the current and stable LTS versions of Node.js.
Install the current stable LTS release of Node.js (recommended for production applications):
nvm install --lts
Install the current release of Node.js (for testing the latest Node.js features and improvements, but more likely to have issues):
nvm install node
- 4. List what versions of Node are installed:
nvm ls
Now you should see the two versions that you just installed listed.
- 5. Verify that Node.js is installed and the current version:
node --version
Then verify that you have NPM installed as well:
npm --version
Changing Node Versions
Use the following commands to change the version of Node you would like to use for any given project:
Switch to the Current version:
nvm use node
Switch to the LTS version:
nvm use --lts
You can also use the specific number for any additional versions you've installed:
nvm use v8.2.1
To list all of the versions of Node.js available, use the command: nvm ls-remote
.
π¦ NPM
Node Package Manager is the default package manager for Node.js. It is a command-line tool used to download or publish packages and manage the dependencies of a project. There is a searchable repository of all available NPM packages at https://www.npmjs.com/.
New Projects
When creating a new project that will utilize NPM, it must be initialized with the init
command. First, make sure you are in the root directory of your project, and then use the following command:
npm init
package.json
npm init
generates a package.json
file and will prompt you for the metadata of your project. This includes things like the name, version, description, and license. You can think of it as the blueprint of your project as it will also contain the packages it depends on. The metadata can be changed at any time by editing the package.json
file after the initialization.
If you would like to automatically populate the initialization with all the default values, you may add the --yes
flag.
npm init --yes
Installing Modules
Modules are installed via the npm install
or npm i
command.
npm install react
The above command will install the React module as a dependency in package.json
.
We can also install NPM packages globally on our system. This is useful for utilities like command line interfaces.
Yarn is another widely used node package manager, if we wanted to use it on all our node projects we would need the --global
or -g
flag.
npm install --global yarn
Dependencies
You can save a module as either a dependency or a developer dependency.
A dependency would be something that your project cannot function properly without.
The --save
flag used to be needed to install modules as a dependency, but it is now done automatically with the install
command.
npm install --save gray-matter
Is the same as:
npm install gray-matter
Developer Dependencies
A developer dependency would be the modules used to build the project, not run it. This would include things like linters and testing tools.
Developer dependencies are added with the --save-dev
or -D
flag. This adds the module to the devDependencies
section of package.json
npm install --save-dev husky
Batch Installing
Apart from installing a single module, you can install all modules that are listed as dependencies
and devDependencies
:
npm install
This will install all modules listed in the package.json
of your current directory.
If we only wanted to install the dependencies needed to run our project, the --production
flag is used:
npm install --production
the --production
flag will only install the modules from the dependencies
section of package.json
and ignore the devDependencies
. The perk of this is notably reducing the size of the node_modules
folder.
Uninstalling
Removing modules works in the same way as installing them. Flags will need to be used for any global or developer dependencies.
Dependencies:
npm uninstall react
Developer Dependencies:
npm uninstall --save-dev husky
Global Installs:
npm uninstall --global yarn
Versioning
Package versions are identified with major, minor, and patch releases. 8.1.1
would be major version 8, minor version 1, and patch version 1.
You can specify an exact version number by using the @
symbol.
npm install react@17.0.1
Two more symbols we can use are ^
and ~
.
^
is the latest minor release.
For example, npm install ^8.1.1
specification might install version 8.3.1
if that's the latest minor version.
~
is the latest patch release.
In the same way as minor releases, npm install ~8.1.1
could install version 8.1.6
if that's the latest patch version available.
When using the npm install
or npm i
command, the latest minor version will be used.
package-lock.json
The exact package versions will be documented in a generated package-lock.json
file.
The values found inside the dependencies
and devDependencies
objects of the package.json
file include a range of acceptable versions of each package to install.
package-lock.json
is generated by the npm install
command and contains the exact versions of the dependencies used.
Scripts
package.json
also contains a scripts
property that can be defined to run command-line tools installed on the current project. This can include things like running tests, formatting your code, and launching a local server.
NPM scripts are run by using the run
command. With the above configuration, you would use the following command to have prettier format your code:
npm run format
The keys in the scripts object are the command names and the values are the actual commands.
Check out the official NPM, NVM, and Node.js docs for more in-depth guides.
References
Posted on October 10, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.