How to Install Node.js with npm on Debian 10

How to Install Node.js with npm on Debian 10

Install Node.js with npm on Debian 10 In this tutorial, we are going to learn how to install Node.js with npm on Debian 10

Table of Contents

Install Node.js with npm on Debian 10

In this tutorial, we are going to learn how to install Node.js with npm on Debian 10. Node.js is the opensource JavaScript Run-time environment for server-side execution of JavaScript code. Node.js built on Chrome’s V8 JavaScript engine so it can be used to build different types of server-side applications.

Where npm stands for Node Package Manager which is the default package manager for Node.js. npm is the world’s largest software registry for Node.js packages with thousands of packages available.

In this tutorial we will install Node.js in following two ways:

  1. Install Node.js and npm using Debian repository
  2. Install Node.js and npm using nvm
  3. Install Node.js from the NodeSource repository.

1. Install Node.js and npm using Debian repository

First, Update Debian apt package manager index by running the following command.

sudo apt update

Install Node.js from Debian global repository by typing

sudo apt install node

Confirm the installation of Node.js by typing

node --version

Install npm by running following command

sudo apt install npm

Confirm the installation of npm by typing

npm --version

2. Install Node.js and npm using nvm

NVM stands for Node Version Manager which is used to manage multiple Node.js versions. If you want to install or uninstall different versions of Node.js then NVM is there for you.

First, we will install NVM (Node Package Manager) on your system. So download the NVM installation script running the following command.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

Check nvm version and confirm installation typing

node --version

Now install Node.js by using the following command.

nvm install node

Verify Node.js installation by typing

node --version

The output should be:

Output

v10.14.0

You can install multiple versions of Node.js. To do so type the following:

nvm install 8.14
nvm install --lts
nvm install 11.3

To list all the versions installed run following command.

nvm ls

You can change the current default version of Node.js by using the following command.

nvm use 8.14

To uninstall a Node.js version type following command

nvm uninstall 11.14

Install Node.js from NodeSource Repository

NodeSource company provides enterprise-grade node support also maintains the repository containing the latest version of Node.js.

To enable the NodeSource repository on your system run following command.

curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -

NOTE: The latest LTS version of Node.js is 10.x if you want to install 8.x version then just replace setup_10.x with setup_8.x

Now install Node.js and npm package typing.

sudo apt install nodejs

Verify installation of Node.js and npm running following command

node --version
npm --version

Install Development Tools

Now install some packages needed for development by running following command

sudo apt install gcc g++ make

Uninstall Node.js and npm

Uninstall Node.js use following command

sudo apt remove nodejs npm
sudo apt autoremove

To uninstall node.js version using nvm type following command

nvm uninstall 10.14

Conclusion

You have successfully learned how to install Node.js with npm on Debian 10. If you have any queries don’t forget to comment below.

Learn how to use NestJS, Node.js framework to build a secure API

Learn how to use NestJS, Node.js framework to build a secure API

Learn how to use NestJS, a Node.js framework powered by TypeScript, to build a secure API

In this tutorial, you'll learn how to build a secure API using NestJS, a module-based architecture framework for Node.js powered by TypeScript.

NestJS helps developers create highly scalable, modular, and maintainable server-side web applications. It leverages the Express framework to easily implement the MVC (Model-View-Controller) pattern and to provide you with extensibility, as you can use any of the third-party modules available for Express. However, the most outstanding feature of NestJS is its native support for TypeScript, which lets you access optional static type-checking along with strong tooling for large apps and the latest ECMAScript features.

What You Will Build

In this tutorial, you'll build a feature-complete API that lets clients perform data operations on resources that describe a restaurant menu.

You'll be using a production client called "WHATABYTE Dashboard" to consume, test, and even try to hack the API!

This dashboard is inspired by the sleek web player from Spotify.

API access will be constrained by the following business rules:

  • Anyone can read data: read menu items.

  • Only users with a menu-admin role are authorized to write data: create, update, or delete menu items.

For simplicity, you'll store data in-memory and not in an external database for this phase of the tutorial.

Getting Started with NestJS

NestJS requires Node.js and NPM to run. Check if these are installed by running the following commands in your terminal:

node -v && npm -v

If you need to install any of them, follow the instructions provide by the Node.js Foundation for your operating system. This tutorial was tested using Node.js v10.16.3 and NPM v6.9.0.

NestJS offers a powerful CLI tool to create and build your application. To generate a new project, use npx to run the NestJS CLI without installing it globally in your system:

npx @nestjs/cli new nest-restaurant-api

The npx command is available with npm v5.2.0 and higher.

The CLI will ask you to choose a package manager, npm or yarn, and proceed to install project dependencies using your selection. To follow this tutorial choose npm.

Once the installation is complete you'll get a directory called nest-restaurant-api. Navigate to this directory:

# move into the project directory
cd nest-restaurant-api

Cleaning Up the NestJS Starter Project

For simplicity, you won't be writing any tests in this tutorial. However, you should write solid tests for any production-ready application. As such, delete the test directory and the src/app.controller.spec.ts file from your project:

rm -rf test/
rm src/app.controller.spec.ts

Refer to the NestJS Testing documentation for details on how to perform automated tests.

After that, delete the files defining AppController and AppService:

rm src/app.controller.ts src/app.service.ts

Deleting these files breaks AppModule as it depends on AppController and AppService. To fix that, open your project in your preferred IDE and update src/app.module.ts as follows:

// src/app.module.ts

import { Module } from '@nestjs/common';

@Module({
  imports: [],
  controllers: [],
  providers: [],
})
export class AppModule {}

Using Environmental Variables

src/main.ts is the entry point of your application; however, this file has hard-coded configuration dependencies that make your application less flexible and adaptable to different deployment environments.

Open src/main.ts and notice that the app is configured to listen for incoming requests on a hard-coded port number, 3000:

await app.listen(3000);

To fix this configuration rigidity, you'll use environmental variables to provide your application with configuration values, such process.env.PORT, instead of hard-coded ones.

To start, install dotenv in your project:

npm i dotenv

dotenv is a zero-dependency module that loads environment variables from a .env file into the global variable process.env.

Create this hidden file under the root project directory as follows:

touch .env

Open .env and populate it with the following variable:

PORT=7000

The configuration variables held by .env can be attached to process.env by calling the dotenv.config() method. As such, update src/main.ts to call this method right below the module imports and replace the hard-coded port number with process.env.PORT:

// src/main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as dotenv from 'dotenv';

dotenv.config();

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(process.env.PORT);
}
bootstrap();

Now anytime you run your application, it will be listening for requests on port 7000 — or whatever the value of process.env.PORT may be. This allows your application to be externally configured by deployment services such as AWS or Heroku.

Caution! Your .env file eventually may contain sensitive information, such as API keys or secrets. As such, it's critical that you add it to your .gitignore file to prevent it from being committed to version control, such as git.

Installing the NestJS CLI Locally

Now that the project is clean and configured, you'll use the NestJS CLI locally as a development dependency to easily generate the architectural elements of your app. Run the following command to install it:

npm install --save-dev @nestjs/cli

It's critical to pass the --save-dev flag to npm to ensure that the package is installed locally for development and testing purposes and it's not included in the production bundle of your app.

Checkpoint

Similar to video games, it's important to save your progress as you work through a software project. While IDEs and text editors may offer the ability to roll back different versions of a project file, it's a good idea to use version control as your "checkpoint".

Create an empty Git repository for your project as follows:

git init

Add your current project files to the repository, ignoring any file listed in .gitignore:

git add .

Finally, commit the file bundle as follows:

git commit -m "Set up foundation of NestJS app"

Top 7 Most Popular Node.js Frameworks You Should Know

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser.

One of the main advantages of Node is that it enables developers to use JavaScript on both the front-end and the back-end of an application. This not only makes the source code of any app cleaner and more consistent, but it significantly speeds up app development too, as developers only need to use one language.

Node is fast, scalable, and easy to get started with. Its default package manager is npm, which means it also sports the largest ecosystem of open-source libraries. Node is used by companies such as NASA, Uber, Netflix, and Walmart.

But Node doesn't come alone. It comes with a plethora of frameworks. A Node framework can be pictured as the external scaffolding that you can build your app in. These frameworks are built on top of Node and extend the technology's functionality, mostly by making apps easier to prototype and develop, while also making them faster and more scalable.

Below are 7of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Express

With over 43,000 GitHub stars, Express is the most popular Node framework. It brands itself as a fast, unopinionated, and minimalist framework. Express acts as middleware: it helps set up and configure routes to send and receive requests between the front-end and the database of an app.

Express provides lightweight, powerful tools for HTTP servers. It's a great framework for single-page apps, websites, hybrids, or public HTTP APIs. It supports over fourteen different template engines, so developers aren't forced into any specific ORM.

Meteor

Meteor is a full-stack JavaScript platform. It allows developers to build real-time web apps, i.e. apps where code changes are pushed to all browsers and devices in real-time. Additionally, servers send data over the wire, instead of HTML. The client renders the data.

The project has over 41,000 GitHub stars and is built to power large projects. Meteor is used by companies such as Mazda, Honeywell, Qualcomm, and IKEA. It has excellent documentation and a strong community behind it.

Koa

Koa is built by the same team that built Express. It uses ES6 methods that allow developers to work without callbacks. Developers also have more control over error-handling. Koa has no middleware within its core, which means that developers have more control over configuration, but which means that traditional Node middleware (e.g. req, res, next) won't work with Koa.

Koa already has over 26,000 GitHub stars. The Express developers built Koa because they wanted a lighter framework that was more expressive and more robust than Express. You can find out more about the differences between Koa and Express here.

Sails

Sails is a real-time, MVC framework for Node that's built on Express. It supports auto-generated REST APIs and comes with an easy WebSocket integration.

The project has over 20,000 stars on GitHub and is compatible with almost all databases (MySQL, MongoDB, PostgreSQL, Redis). It's also compatible with most front-end technologies (Angular, iOS, Android, React, and even Windows Phone).

Nest

Nest has over 15,000 GitHub stars. It uses progressive JavaScript and is built with TypeScript, which means it comes with strong typing. It combines elements of object-oriented programming, functional programming, and functional reactive programming.

Nest is packaged in such a way it serves as a complete development kit for writing enterprise-level apps. The framework uses Express, but is compatible with a wide range of other libraries.

LoopBack

LoopBack is a framework that allows developers to quickly create REST APIs. It has an easy-to-use CLI wizard and allows developers to create models either on their schema or dynamically. It also has a built-in API explorer.

LoopBack has over 12,000 GitHub stars and is used by companies such as GoDaddy, Symantec, and the Bank of America. It's compatible with many REST services and a wide variety of databases (MongoDB, Oracle, MySQL, PostgreSQL).

Hapi

Similar to Express, hapi serves data by intermediating between server-side and client-side. As such, it's can serve as a substitute for Express. Hapi allows developers to focus on writing reusable app logic in a modular and prescriptive fashion.

The project has over 11,000 GitHub stars. It has built-in support for input validation, caching, authentication, and more. Hapi was originally developed to handle all of Walmart's mobile traffic during Black Friday.

The Ultimate Node.js Beginner Tutorial

The Ultimate Node.js Beginner Tutorial

This tutorial is for all developers who want to program with Node.js but don't know much about Node.js yet. Node.js is becoming more and more popular and every web developer should know at least the basics. After this tutorial you can clearly count yourself in this group.

This tutorial is for all developers who want to program with Node.js but don't know much about Node.js yet. Node.js is becoming more and more popular and every web developer should know at least the basics. After this tutorial you can clearly count yourself in this group.

So that you have a red thread through this Node.js Tutorial, I have you the points times summarized:

Table Of Contents 1. What is Node.js?

With Node.js the scripting language JavaScript has also found its way into server-side programming. Before Node.js JavaScript was mainly responsible for the frontend and the interaction with the visitor. With Node.js you can now develop from small CLI (Command Line) tools to complex programs and HTTP servers. Exactly this universal usability makes Node.js for me personally - and also for many other developers - so ingenious!

If you are wondering if you really need Node.js and want to be able to use it, you should read the most frequently asked questions below and decide if Node.js is the right technology for you.

Another important reason for the success are the on-board resources that come with Node.js. From the ground up, Node.js already has many features and modules that are easy to use. And to make the system even more perfect, there is the package manager - also called npm (Node Package Manager). More information can be found later in the Node.js Module section. Anyway, it's very easy to add new features, like extensions from other developers.

That sounds promising at first, but can you imagine anything concrete about it? Probably not. At least it wouldn't be very easy for me if I read something like this on a topic I didn't know. Therefore read this section best at the end of this article once again and I assure you, you will know what I am talking about! ;)

2. Install Node.js

Before we can start programming, we first have to install Node.js on our computer. Now select the operating system you want to develop on. Windows or Mac…?

2.1 Windows

Under Windows you can simply download the installer and follow the installation instructions. It is best to download the LTS (Long-term support) version as it is already established and has fewer bugs than the latest version.
Windows Installer

2.2 Mac

On a Mac, just like Windows, you can download the official installer and follow the installation instructions. Also download the LTS (Long-term support) version, because it is already established and has less bugs than the latest version.
Mac Installer

2.3 Plesk (Hosting)

If you manage your domains with Plesk and want to host your app there, all you need is the Node.js extension, which you can download with a simple click.


To develop, however, you should work locally on your Mac or Windows computer. When your app is ready and you want to deploy it, you can use this guide to help you.

3. CLI

CLI stands for Command-Line Interface and means command line. On the Mac you can use the pre-installed program "Terminal" and on Windows the command prompt (cmd.exe).


With this we can "control" Node.js. With the command node we can now execute any JavaScript command. In the further course of this article you will now know what you can understand by the CLI.

4. Node.js Modules

The crown jewels of Node.js are its modules. At the beginning I already mentioned that Node.js has a package manager. The abbreviation and the command for the CLI is npm (Node Package Manager).

NPM is a gigantic network of development tools that can be downloaded free of charge for your application. If you work with Node.js, you'll find that you have to rely on other developers' modules over and over again. You don't have to reinvent the wheel all the time.

In this example we download the express module:

npm install express

You can also install modules globally, which means it applies to your entire computer and is not only available in one project. Just append the parameter -g.

npm install -g express

When you install a module, the node_modules folder is automatically created. This folder contains all installed modules and can normally be ignored by you. We can include a module in the code later in this way:

const express = require('express');

5. First project

5.1 Create project

The time has come, we're finally starting programming. To create a Node.js project, we simply create a normal folder in any directory. There we create an index.js, which is our start file.

5.2 Initialize Node.js App

Now we have to say in this folder that we want to make a Node.js app out of it, we do that with this command:

npm init

We are asked for different parameters like package name, version and description. You can fill in these fields or just leave them at their default values. We can change these settings later at any time.

5.3 package.json

A package.json file has now been created in our folder. This file contains all information about the author, version and most importantly, about all installed dependencies (modules).

// package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

5.4 Write program code

Now we can write the first JavaScript code in our index.js. Of course we can't do without it and have to spend "Hello World".

// index.js

console.log('Hello World');

5.5 Test application

The first code is written and we want to start our program. This is what we do with this command:

node index.js

We get this result, cool! 🥳

6. Automatic App Restarts (Nodemon)

It's really annoying when we have to manually restart our Node.js app after every little change to the code, isn't it? Right, so there is the module nodemon, which we install globally. This module detects file changes and restarts the Node.js app within milliseconds. This speeds up our workflow enormously.

npm install -g nodemon

After the installation we start Nodemon with this command and immediately our app is started and restarted automatically when changes are made.

nodemon index.js

If you now save your files within the project folder, the Node.js app will automatically restart.

7. Browser output

Now we already want to set up our first small web server in order to be able to deliver content in the browser. Therefore we install the module express with the following command:

npm install express

And we adjust our index.js like this:

// index.js

const express = require('express');
const app = express();

app.get('/', (request, response) => {
  response.send('Our first Node.js webserver');
});

app.listen(3000, () => console.log('Server running on port 3000'));

To help you understand everything, here is an explanation line by line:
Line 3: Integration of the express module.
Line 4: Initialization of the express module in the variable app.
Line 6: We intercept the page call from / to our server to perform an action.
Line 7: We send the text "Our first Node.js webserver" back to the requestor as an answer.
Line 9: We start our web server on port 3000 and display a message in the console.

If we ask for browser now our computer on the port 3000, we get this result:

Simple, isn't it? That's why Node.js is so brilliant. You only need seven lines of code for your own web server.

If we now adjust our route a little bit, we can return data that has already been transferred:

// index.js

const express = require('express');
const app = express();

app.get('/:yourName', (req, res) => {
  res.send('Your name: ' + req.params.yourName);
});

app.listen(3000, () => console.log('Server running on port 3000'));

Here :yourName stands for a variable string that we can pass in our URL and with req.params.yourName we can read it and send the name back to the user.

If we now call http://localhost:3000/test, we get the string with the given name as response. So you can already read and send data with really little code.

8. FAQ

When do I use Node.js?

Node.js is primarily used for "normal" dynamic websites and backend APIs (RestAPIs). In addition, Node.js is often used in applications that need to process data in real time (e.g. chats).

Is Node.js a programming language?

Definitely no. Node.js connects the scripting language JavaScript and a network component. Since Node.js was developed on the Google V8 JavaScript engine, Node.js masters the basics of network technology such as the HTTP, DNS and TCP protocols.

Is it easy to learn Node.js?

Since Node.js is not a framework and not a programming language, you have to ask yourself if it is easy to learn JavaScript. Surveys have shown that JavaScript is one of the easiest scripting languages to learn.

What is middleware?

A middleware is a part-program, i.e. a function that is executed between two components. For example, if the user calls /settings, the actual program code should only be executed if the user is logged on. To do this, you write a middleware function and call this function before. This is done so that the same code (checking the login) can be used several times, but only has to be written once.

How and where can I host a Node.js app?

For example, you can host Node.js Apps for free at Heroku.

What does the package.json do?

The package.json at Node.js Apps contains information about the name, the author, and much more. Most important are the dependencies. This is a list of the modules (dependencies) your app uses. You can also use scripts to define commands to run or test your app.

What does the node_modules folder do?

The node_modules folder contains all modules included in your node.js. This folder contains standard modules, but also all modules that you have additionally installed and that are listed in the package.json under scripts. You don't need to push the node_modules folder into your git repository or live hosting, because the npm install command will download all modules into the folder again.

Conclusion

Pooh! Quite a lot of information at once, isn't it? Nevertheless I hope that you got a good impression of what is possible with Node.js and I made you curious for more. If so, you are welcome to continue looking around on webdeasy.de and read even more exciting articles about web development. You are welcome to write any unsettled questions in the comments! :)