This tutorial article was written using Linux – that's why the commands won't work on a Windows computer. While it's not a requirement, if you are planning to become a developer, I strongly recommend using a Unix based operating system.
The only official requirement to run a Node project is having Node installed on your computer, but this is not what happens in the real world. To make it easier to deploy an application, some tools are used – npm in this case (Node Package Manager).
The first step is to install NPM (and the way to do it depends on your Linux distribution or Operating System).
Installing NPM (Node Package Manager)
On Arch linux, npm is supplied by the npm community package:
[brudhu@brudhu-manjaro tutorials]$ sudo pacman -Sy npm
On Ubuntu (and other distributions), the instructions can be found here: https://github.com/nodesource/distributions/blob/master/README.md
[brudhu@brudhu-manjaro tutorials]$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
[brudhu@brudhu-manjaro tutorials]$ sudo apt-get install -y nodejs
Creating the app using NPM
Create a directory for you project and enter the directory:
[brudhu@brudhu-manjaro tutorials]$ mkdir tutorial-project-1
[brudhu@brudhu-manjaro tutorial]$ cd tutorial-project-1
Once you are in the directory, create the app using NPM:
[brudhu@brudhu-manjaro tutorial-project-1]$ npm init
After running the init command, it will ask some questions about your project (you can just press enter to all of then for this project):
- package name: the name of you project
- version: the version of your project
- description: the description of your project
- entry point: the file that will be called to run your project
- test command: a command to run tests on your project
- git repository: the git repository of your project, in case it already has one
- keywords: keywords of you project
- author: the author's name
- license: the license type of the project
This is what I answered for this tutorial - once you answer all the questions, it will create a package.json file, as shown bellow:
[brudhu@brudhu-manjaro tutorial-project-1]$ 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 json` 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: (tutorial-project-1)
version: (1.0.0)
description: My first Node.js app project
entry point: (index.js)
test command:
git repository:
keywords: node tutorial
author: Bruno Luvizotto
license: (ISC)
About to write to /home/brudhu/tutorials/tutorial-project-1/package.json:
{
"name":"tutorial-project-1",
"version":"1.0.0",
"description":"My first Node.js app project",
"main":"index.js",
"scripts":{
"test":"echo \"Error: no test specified\" && exit 1"
},
"keywords":[
"node",
"tutorial"
],
"author":"Bruno Luvizotto",
"license":"ISC"
}
Is this OK? (yes)
The package.json file is the descriptor of you project - it stores all the information you answered in the npm init command and will store information on the packages used by the project (dependencies).
If you list the files in the project's directory, there will be the new package.json file:
[brudhu@brudhu-manjaro tutorial-project-1]$ ls
package.json
Now that we have the project descriptor (aka package.json), let's create the first file (the entry point of the project):
[brudhu@brudhu-manjaro tutorial-project-1]$ echo 'console.log("I did it! My first project!")' > index.js
At this point, we have the package.json and the index.js files. The next thing to do is to create a start script in your package.json file. Add the line "start": "node index.js"
under “scripts”. Don't forget to add the comma after the previous line:
{
"name": "tutorial-project-1",
"version": "1.0.0",
"description": "My first Node.js app project",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [
"node",
"tutorial"
],
"author": "Bruno Luvizotto",
"license": "ISC"
}
The scripts described under “scripts” in the package.json file can be run using the npm run command (e.g. npm run test or npm run start in this case).
Now that we have the start script described and also the index.js file, we can finally run the project:
[brudhu@brudhu-manjaro tutorial-project-1]$ npm run start
> tutorial-project-1@1.0.0 start /home/brudhu/tutorials/tutorial-project-1
> node index.js
I did it! My first project!
Congratulations! This is the very beginning of a Node.js project!
Top comments (0)