DEV Community

Cover image for How to create a Node.js project from zero with good practices?
Luiz Calaça
Luiz Calaça

Posted on

How to create a Node.js project from zero with good practices?

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

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 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

fix
feature
docs
chore
tests
style
refactor
Enter fullscreen mode Exit fullscreen mode

Git
After you run git init already add the .gitignore and into there folders like:

node_modules
dist
coverage
Enter fullscreen mode Exit fullscreen mode

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"
    ]
}
Enter fullscreen mode Exit fullscreen mode

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

{
    "hooks":{
        "pre-commit": "eslint"
    }
}
Enter fullscreen mode Exit fullscreen mode

Javascript Standard Style and eslint
We need to know about standard JS, just use eslint if has just Javascript files, but if you are using Typescript you could use 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"
  },
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

Software Architecture
If you are gonna use such as DDD + Clean Architecture, you could create the folders with its hierarchy.
DDD + Clean Arch

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

configuration Node.js project

Contacts
https://www.buymeacoffee.com/calaca

Top comments (0)