How to start nodejs project

To start nodejs project first of all you need to install nodejs on your machine.

For Windows and OS X users you can use the official installer.
For linux users installation method varies by distribution.

In this article, I show you how to start nodejs project on ubuntu/debian-based distributive.

1) Install nodejs:

sudo apt udpate && sudo apt install nodejs

Make sure that nodejs is installed correctly:

node -v

2) Install npm:

sudo apt install npm

Make sure that npm is installed correctly:

npm -v

3) Create a specific folder for your project:

cd ~
mkdir Projects && cd Projects
mkdir nodejs_app && cd nodejs_app

4) Start the project with npm:

npm init
  • Press enter or choose a new name for your application
  • Press enter or choose the new version of our application, I recommend you to choose 0.0.1 since we just start the new app.
  • You can add a description, I leave it empty.
  • The entry point should be index.js so please press enter on this step.
  • The test command is empty for now.
  • The git repository is empty too.
  • Leave keywords empty as well.
  • The author can be empty also.
  • License the same – empty.
  • Confirm your settings and press enter one more time.
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (nodejs_app) 
version: (1.0.0) 0.0.1
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /home/lamps/Projects/nodejs_app/package.json:

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


Is this OK? (yes) 

5) Create a file with a simple log message:

 touch index.js && echo "console.log('my_nodejs_app')" > index.js

6) Check your nodejs app:

node index.js

You should see a log message that we added in 5 step:

$ node index.js 
my_nodejs_app

Congratulation! You have started your first nodejs application. The result you can check here.

Leave a Reply

Your email address will not be published. Required fields are marked *