How to create a Node.js project from zero with good practices?
Published: 2022-07-19 22:24:58

## Hi, Devs!

How to create your Node.js project? What is important to organize to masterize? So, let's see necessary things to build your initial project, code quality, manutenability and scalability

**Spoiler:** _At the final there's a github boilerplate repo with all configurations_.

---

![Configurations from zero Node.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iu4zx7m5k7uaa0erljeu.png)
 

## **Let's go!**

**Basic**
`npm init -y`
Creating our package.json to manage our packages.

**Git alias**
That's cool thing that you can use to optmize your commits, It's always necessary add and commit? So, why don't you automatize with just one command instead of two? Git alias pretty formatting can help us too showing in a better way the git console output.
`[alias] c = !git add --all && git commit -m`

_Look at here my [gist](https://gist.github.com/luizcalaca/ea1642991eefd7e3c312b9e11a3ddb44) to handle completely alias like that._

**Conventional Commits Linter**
`npm install git-commit-msg-linter`

Here's the git linter to force us to use the [Conventional Commits pattern](https://www.conventionalcommits.org/en/v1.0.0/)

```
fix
feature
docs
chore
tests
style
refactor
```

**Git**
After you run `git init` already add the `.gitignore` and into there folders like: 
```
node_modules
dist
coverage
```

**Husky and lintStaged for code quality**
Below is the `.lintstagedrc.json` configuration to add into project root folder. It garantees that commit will be done just before fix all lint problems and all tests ok.
```
{
    "*.ts": [
        "eslint 'src/**' --fix",
        "npm run test:staged"
        "git add"
    ]
}
```

And here `.huskyrc.json` to garantee esling running before to commit:

```
{
    "hooks":{
        "pre-commit": "eslint"
    }
}
```

**Javascript Standard Style and eslint**
We need to know about [standard JS](https://standardjs.com/), just use eslint if has just Javascript files, but if you are using Typescript you could [use](https://github.com/standard/eslint-config-standard-with-typescript) and plugin ESlint on VSCode.

**Configuring scripts on you package.json**
You could configure your scripts and already all prepared to use.
```
  "scripts": {
    "lint": "npx eslint .",
    "test": "jest"
  },
```

**Library Testing**
If necessary install Jest or Mocha, chain, sinon for unit tests. Frisby for test integrations and Cypress for End2End tests.

**Docker**
Certainly you neeed to add `docker-compose.yml` with yours infrastructure (database and other services)

```
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
```

**Software Architecture**
If you are gonna use such as DDD + Clean Architecture, you could create the folders with its hierarchy.
![DDD + Clean Arch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bk2d3ub248fjcge58uil.png)
 

_Get all the article here_ 
**Complete Boilerplate with that all configurations**
Link: https://github.com/luizcalaca/node-typescript-boilerplate-configuration

![configuration Node.js project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dc2ieai6mpgd7kgbxr3h.png)
 
**Contacts**
https://www.buymeacoffee.com/calaca