Node.js: Installation & Basics
- 🎛 NVM
- 📦 NPM
You will likely need to switch between multiple versions of Node.js based on the needs of different projects you're working on. Node Version Manager allows you to quickly install and use different versions of node via the command line.
Open your terminal and install NVM with
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.
List which versions of Node are currently installed (should be none at this point):
Install both the current and stable LTS versions of Node.js.
Install the current stable LTS release of Node.js (recommended for production applications:
Install the current release of Node.js (for testing latest Node.js features and improvements, but more likely to have issues):
List what versions of Node are installed:
Now you should see the two versions that you just installed listed.
Verify that Node.js is installed and the current version:
Then verify that you have npm installed as well:
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:
Switch to the LTS version:
You can also use the specific number for any additional versions you've installed:
To list all of the versions of Node.js available, use the command:
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/.
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 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
Modules are installed via the
npm install or
npm i command.
The above command will install the React module as a dependency in
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
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.
--save flag used to be needed to install modules as a dependency, but it is now done automatically with the
Is the same as:
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
-D flag. This adds the module to the
devDependencies section of
Apart from installing a single module, you can install all modules that are listed as
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:
--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
Removing modules works in the same way as installing them. Flags will need to be used for any global or developer dependencies.
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
Two more symbols we can use are
^ is the latest minor release.
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.
The exact package versions will be documented in a generated
The values found inside the
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.
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:
The keys in the scripts object are the command names and the values are the actual commands.