Learning Node.js — Part 1

What is Node? / Setting Up First Node Application: Building a Quick Node Web Server / Implementing package.json File

Osha Groetz
4 min readApr 14, 2021
codewithmosh.com

As a disclaimer, this series of blogs I’ll be taking notes as I work through Mosh Hamedani’s ‘The Complete Node.js Course’. I am by no means taking credit for any of the following, simply using these blogs as a quick reference to myself as I learn Node.

What is Node?

Node is a runtime environment for executing Javascript code outside of the browser. It is NOT a programming language or a framework. It is a C++ program that includes Chrome’s V8 Javascript engine.

Node has a non-blocking/asynchronous nature. Node.js applications run on a single thread along with its event loop. Node is good for data intensive applications (not for CPU, Central Processing Unit, intensive apps, for ex: apps that are sorting, searching, relying heavily on graph-traversal).

Installing Node

Check Node version in terminal: node — — version (there should be a space between node and the dashes there)

Install or update latest Node version: nodejs.org . Download/install LTS, the latest stable version.

Building A New Node Application

In your terminal change directories into the desired folder where you’ll build your Node application. Next, make a new directory of your app: mkdir <app-name>, then cd <app-name> and open in the application in your text editor (code . for VS Code).

Creating the Node Web Server²

In the Node.js documentation, you can see that there are a number of modules built directly into Node.js. For this web server example, we are going to use the HTTP Module/Library to make our quick Node server, but when building our networking application (backend server), we’ll implement the framework Express, for more extensive routing.

In our new apps folder, setup a new file: app.js . We will load in our http module as follows and get the server up and running:

From here, in the terminal type command: node app.js . The server should now be up and running and if you visit http://localhost:3000/ , you should see the message ‘Hello, Node Server is Running’ in plain text on the screen.

SIDE NOTE: To render html, instead of plain text, you could implement the following code in the app.js file:

As well as set up a new file: index.html , with this code:

Until we go over the shortcut to get our server to listen for changes automatically, don’t forget to restart your server anytime you make a change in any files associated with the server (app.js or index.html). Terminal command control C to stop the server and then node app.js to restart it.

Setting Up package.json File

Create a new file directly in our app with terminal command: npm init . The application will then ask you a number of questions: 1. package name: (<default value here>) — Press Enter to accept default value. 2. version: (1.0.0) — Press Enter. 3. description: Enter, again. 4. entry point: (index.js) — Enter. 5. test command: — Enter. 6. git respository: — Enter. 7. keywords: — Enter. 8. author: — Enter. 9. license: (ISC) — Enter. 10. Is this ok? (yes) — Enter. ((Feel free to change any of these above as necessary))

This package.json file will need to be set up in an application before any NPM packages are installed.

Faster way to install: terminal command: npm init — — yes ((space before the double dash))

Install Node Package Manager (NPM) Dependencies

*commands below that start with sudo are for Mac

Global Dependencies:

Node Package Manager: terminal command: sudo npm i -g npm or npm install npm@latest -g

Application Dependencies;

Underscore: JS library that provides functional programming helpers³. terminal command: npm install underscore (or: npm i underscore)

Mongoose: Used to store data in MongoDB, ‘MongoDB object modeling⁴’. terminal command: npm i mongoose

Development Dependencies:

JSHint: A tool that helps to detect errors and potential problems in your JavaScript code⁵. terminal command: npm i jshint — — save-dev ((space before double dash only))

Show dependencies of application, terminal command: npm list

SIDE NOTES:

To initialize empty git repo, terminal command: git init

Handling node_modules folder: It’s a good idea to have a .gitignore file that contains code for ignoring node_modules when committing our app to github. It is important that this modules folder is excluded from the commit, terminal command: touch .gitignore . Open up the file and add this line of code: node_modules/ (also seen this as /node_modules) to the file. The node_modules folder and all of its’ files will never be included in git commits. Should you need to fork a node application, and reinstall dependencies, terminal command: npm install .

To update npm packages, terminal command: npm update . To update to very latest version, terminal command: sudo npm i -g npm-check-updates . Enter password, then terminal command: npm-check-updates . Run: ncu -u , then npm install to update package.json and to upgrade and install all npm packages to latest versions.

To uninstall: npm un <package name>

  1. Node.js Tutorial for Beginners | Programming with Mosh | “https://www.youtube.com/watch?v=TlB_eWDSMt4&t=2349s
  2. Your First Node.js Web Server | Web Dev Simplified| “https://www.youtube.com/watch?v=VShtPwEkDD0&ab_channel=WebDevSimplified”
  3. Underscore.js | “http://underscorejs.org/
  4. Mongoose | “https://mongoosejs.com/
  5. JSHint | “https://jshint.com/

--

--

Osha Groetz
Osha Groetz

Written by Osha Groetz

Software Engineer. React, Javascript, Typescript, HTML, CSS, Ruby, Ruby On Rails

No responses yet