DEV Community

Douglas Alves
Douglas Alves

Posted on Originally published at Medium on

Building your First Node.js Project

Node.js Logo

When learning a new programming language, it’s common to find several questions in our minds and a sea of ​​content to navigate. You can face trouble searching for a path to follow, but don’t worry! I will explain some concepts to help you get started on the right foot with Node.js!

First of all, what is Node.js?

Node.js is a JavaScript execution environment that runs outside a browser. In other words, you can run JavaScript directly in your operating system. It runs under the V8 engine, the Google Chrome JavaScript engine.

Beyond running your scripts, Node.js offers a series of tools that transform JavaScript into a complete programming language.

What is npm?

When you install Node.js, you will notice it also installs npm , the Node Package Manager. We use npm to install and manage our project dependencies.

You can find anything you can think of in the constantly growing npm library.

Here are some npm commands for you to get familiar with:

  • To start a project: npm init
  • Install a new package: npm install <>
  • Remove a package: npm remove <>
  • Run the project: npm start

Understanding the package file

The package.json is the main file of a Node.js project. In this file, you define your project metadata, the dependencies (packages your project need), and other features as scripts to run and debug your project.

These are some sections of package.json that you should know at the beginning:

  • Metadata : general information about the project. This section is essential when publishing the project on package registries.
  • Scripts : these are shortcuts you can use with npm to automate and make your work easier. For example, you can type npm start instead of node index.js to run the project. Scripts are essential when using tools that require complex commands.

Whenever you create a script (except in the cases of the start script), you should run it as follows: npm run << your script >>.

  • Dependencies : npm will record the packages used in your project in this section when you install them using the command npm install <> --save. Notice that you must add the flag --save to record a package reference.

After running the command npm install, npm will create a folder named node_modules in your project to store the dependencies.

Hands-on!

Now that you know the basics, let’s build the famous hello world example with Node.js:

  • First, install Node.js on your computer.
  • Next, create a new folder named my-project by running the following command: mkdir my-project && cd my-project
  • Inside the folder my-project , run the command npm init and type the information required to create a new project:

Creating a new project with npm
Creating a new project with npm

  • Next, create a new file named index.js and paste the following code:
  • Create the script to start the project in the package.json file as follows: "start": "node index.js"
  • Finally, start the project by running npm start, and access http://localhost:3000 in your browser.

Running a simple HTTP server
Running a simple HTTP server

I recommend you change the code, rerun the project, and explore the results in your browser.

Next steps

To do well in Node.js, you must master JavaScript, especially in more recent versions (ES6+).

Another important tip is to keep an eye on TypeScript, a superset of JavaScript that adds features like types and static code analysis, which will save you a lot of time by describing errors and more.

To help you in this journey, I recommend these free resources:

Conclusion

Thanks for reading! Keep studying, and soon you will master JavaScript and Node.js. Let me know your thoughts in the comments section, and consider following me for more articles like this.

Top comments (0)