🗃️Microservices and libs

Since we are in a Node.js multiproject, each lib or microservice has a package.json file. As in any other Node project, this file contains all the package's informations and scripts.

Microservice configuration

Every microservice's package.json file is similar and contains the same scripts. The main properties that are different are the name and dependencies.

Example:

package.json
{
    "name": "@microservices/hello-service",
    "private": true,
    "version": "0.0.1",
    "scripts": {
        "build": "node ../../configs/esbuild/esbuild.config.js lambda",
        "build:debug": "node ../../configs/esbuild/esbuild.config.js",
        "lint": "eslint ./src",
        "typecheck": "tsc",
        "clean": "rm -rf dist .turbo node_modules"
    },
    "dependencies": {
        "@fastify/aws-lambda": "^3.2.0",
        "@libs/fastify-utils": "^0.0.1",
        "@libs/utils": "^0.0.1"
    }
}

As you can see there are serveral scripts defined in each microservice:

  • typecheck: this script runs tsc without emitting files (see TypeScript project configuration for more information). This will simply check for type errors.

  • build: this will run a global esbuild configuration file to bundle the entire microservice in a single file (head to Build configuration to learn more)

  • lint: runs eslint on the microservice's source directory. This will use the project root's .eslint.yml file

Lib configuration

package.json
{
    "name": "@libs/utils",
    "private": true,
    "version": "0.0.1",
    "types": "src/index.ts",
    "main": "src/index.ts",
    "scripts": {
        "typecheck": "tsc",
        "lint": "eslint .",
        "test": "mocha",
        "clean": "rm -rf .turbo node_modules"
    },
    "dependencies": {
        "joi": "^17.8.3",
        "pino": "^8.11.0"
    }
}

One important thing that is different from microservice is the main and types properties. These two points to the TypeScript entrypoint of the lib (usually src/index.ts) in order to resolve the lib as a dependency for a microservice (in this way esbuild can build and include the lib in the bundle of a microservice that uses that lib).

Thanks to esbuild, libs do not have a build script. This makes not possibile to build a only the single lib by default.

Last updated