Now, let’s get started building our first Node.js project! The first thing we need to do is create a new project folder and name it something like
Before we start adding files to our project, let’s talk about one of the most important tools in Node.js development, NPM. When you installed Node.js, it also installed a command line tool called
npm which stands for ‘Node Package Manager’. NPM is used to install Node packages (AKA dependencies) to your project. We will be using NPM to install Express as a dependency in this project.
The very first thing we want to do in any new Node.js project is to initialize the project with NPM. To do that, open a command prompt at the directory you just created for your project and run
This will start up a wizard-like command line utility that will walk you through creating a package.json file (more on package.json in a bit). You can fill in an answer for each prompt, or you can just press enter to use the default shown in parentheses. The first prompt asks for “package name:” with the default in our case being “(starter)”. In most cases, accepting the defaults is ok, so you can just keep pressing “Enter” until the utility finishes. You can always go back and modify these by editing your
package.json file later.
The very last prompt will show you a preview of the contents of your package.json file. Press “Enter” once more to accept and generate your
(Alternatively, you can accept all defaults without running through the wizard utility by running
npm init -y. The
-y flag signifies that you wish to accept all default values automatically.)
Now, if you look inside your project folder, you should see a
package.json file has been added. If you open it up in your favorite IDE or text editor, you’ll see that it contains a JSON object with all of the fields from the
npm init utility. We will come back into this file a little later. For now, let’s move on to building our project file structure. For more info on all of the fields your
package.json could possibly contain, have a look at the npm package.json docs.)
Node.js Project File Structure
There is no required file structure for Node.js projects. We are free to organize our project however we like. However, there are some common best practices. In this tutorial, we will use a fairly common file structure for Node.js projects.
index.js. Some developers like to call their main file
app.js. We are calling ours
index.js so it matches the default value of the “main” field in our
package.json file, which identifies the entry point of our application.
Now let’s add some folders to our project. We won’t be using these right away, but we will as the project grows. Go ahead and add the following folders to your project:
views- This is where we will keep all of the files that are responsible for displaying dynamic content to the user.
models- This is where we will keep all files that are responsible for interfacing with our database.
Once you’ve created all the files and folders mentioned above your project structure should look like this:
Now you are all ready to start writing Node.js code. Continue on to the next post to learn how to write a simple server with Express.