Setting up a Node.js project, and installing nodemon and Express
We assume that Node and NPM are correctly installed on your machine. However, it is better to check their versions in the command line to make sure they were successfully installed.
> node --version > npm --version
If everything is ok, you’ll see their versions.
Node.js programs can be created using JavaScript files, contain JavaScript programming statements and expressions, and have a filename extension of ‘.js’.
To execute a Node.js program, you need to run the Node.js executable for the given environment and pass the name of the file (with or without the JS extension) as the first argument. For example, if our program is within a file named server.js, we can run it by calling the name of the file after the node
term in the terminal, as follows:
> node server
Note: To exit the node environment in the terminal, you need to type
> ctrl + c
1. Creating and setting up a Node.js project
Create a folder that will contain your project, and give it the name you want.
Navigate into the created folder, and create a js file (e.g., server.js), which you’ll use to write your code.
1.1 Create .gitignore
A .gitignore
file specifies intentionally untracked files that Git should ignore. Therefore, we need to create this file to at least untrack the /node_modules
directory/file, which can be done as follows:
Create a file called .gitignore
in the root directory of your project, open it, and add the following:
# dependencies /node_modules
1.2 Creating a package.json file
The package.json
file is the heart of Node.js system. It is the manifest file of any Node.js project and contains the metadata of the project.
We used package.json
before and you should remember that it includes essential information concerning your app/project as well as the modules it depends on. However, we did not create one, which can be done as follows:
Create a package.json
file to store all your project dependencies by typing in the terminal:
> npm init
Then, follow the instructions (default is ok). When the package.json
file is created, try to inspect/check its content.
Note: in earlier versions of Node, the use of --save
was required to add the dependency to the package.json
file. Anyway, always check the package.json
file after installing a new package to be sure that it was installed correctly.
2. Install nodemon
nodemon is a tool that helps developing Node.js applications by automatically restarting the node application when the content of a .js file changes.
To install nodemon, write in the terminal
> npm install -g nodemon
After installing nodemon, use it to run your application
> nodemon server
3. Install Express
Express is a framework that enables us to professionally manage our routing requests, and it also makes the code much easier to read and modify.
You can install Express by writing to the terminal
> npm install express
If it was installed successfully, you should see it listed within the dependencies in the package.json
file.