Plugin to run NestJS Apps from Nest Monorepo

Overview

Serverless Nest Monorepo Plugin

Plugin that adds support to run NestJS Apps from Nest Monorepo, which is set up using Nest CLI with several microservices.

This plugin allows you to use Serverless framework with each microservice from monorepo. Each microservice gets separate configuration for Serverless framework.

Nest monorepo usually looks like this:

  • apps/
    • service1
    • service2
    • service3
  • lib/
    • library1
    • library2
  • node_modules/
  • dist/
    • apps/
      • service1
      • service2
      • service3

It is build using nest build <service> command and generates webpacked JavaScript file from TypeScript in dist/apps/service1 directory. Node modules are in root directory as are nest-cli.json or other configuration files.

This structure is fairly incompatible with Serverless framework serverless, since it does not work very good with microservices.

Installation

  1. Install with npm in monorepo root:

npm install --save-dev serverless-nest-monorepo

  1. Move existing serverless.yml file from monorepo root to correct NestJS app. Place it in apps/service1 directory.

mv serverless.yml apps/service1/serverless.yml

There are no updates needed in previous serverless.yml, if it worked from root of the directory. All paths should remain based on root.

  1. Create a new serverless.yml file in monorepo root then add the plugin to your root serverless.yml file:
service: '<your-monorepo-name-same-as-in-package.json>'

useDotenv: true # Remove if not in use
plugins:
  - serverless-nest-monorepo

provider:
  name: aws
  runtime: nodejs14.x

frameworkVersion: '3'

plugins:
  - serverless-nest-monorepo

Both provider and service settings are ignored, but are there for main Serverless framework config validation, since plugins are loaded after configuration.

Usage

Once root serverless.yml is set up and each microservice you wish to run has serverless.yml in it's app directory, run serverless mono from the root. Command has two required parameters:

  • nestApp Nest app to run
  • command Serverless command to run (interactive not supported yet)

Example 1: Run serverless offline for microservice service1

serverless mono --nestApp service1 --command offline

Example 2: Deploy microservice service3

serverless mono --nestApp service3 --command deploy

Example 2: Remove microservice service3

serverless mono --nestApp service3 --command remove

Caveats

  • If you are using useDotenv: true, ensure you set it to true in root serverless.yml and a symlink will be created for correct NestJS microservice.
  • It was only tested using Serverless Framework version 3.
  • Config in root must pass serverless's configuration check, so a provider must be picked. Both name and runtime are ignored in the root file.
  • Ensure you implemented handler correctly in your Nest microservice.
  • It is suggested to add .tml.yml to .gitignore, as the plugin creates symbolic links to prevent commiting them to repository.
  • Microservice serverless.yml paths need to be based on root, not where file resides.
  • It might affect the workings of other plugins.

How it works?

Once serverless mono command is executed the flow is following:

  1. NestJS app is build using nest build <app> command, generating dist/apps/<app>/main.js file.
  2. A temporary symbolic link (ln -s) is created in the root to the serverless file at: apps/<app>/serverless.yml. The original file remains untouched.
  3. A temporary symbolic link is created in the root to the .env file to the env at apps/<app>/.env if useDotenv is set to true.
  4. Serverless framework is executed in a separate child process using the command passed with --command parameter. STDOUT is displayed.
  5. Cleanup is done.

TODO

  • Check if serverless.yml for microservice exists before linking.
  • Pass STDIN input to enable interactive plugins.
  • Catch SIGABRT from user for to correctly cleanup links.
You might also like...

Project implements decorators for oak like Nest.js.

oak decorators Project implements decorators like implementation of Nest.js for Deno's web framework oak. Usage The following are the core files that

Dec 5, 2022

Nest accessory developed with HAP-NodeJS to allow devices into HomeKit

Nest_accfactory This is a HAP-NodeJS accessory I have developed to allow Nest devices to be used with HomeKit including having support for HomeKit Sec

Oct 9, 2022

A Nest rewrite of crystal, the SOS Discord bot

crystal-nest A Nest rewrite of crystal, the SOS Discord bot Explore the docs » Report Bug · Request Feature Table of Contents About The Project Built

Dec 22, 2022

next + nest webchat repository

next-webchat next + nest webchat repository 在线网络聊天室 前端:next + React 服务端渲染,使用@reduxjs/toolkit ++ hook 搭配使用 react-redux 使得状态管理更易上手, 外加socket.io-client 实

Dec 20, 2022

A Nest.js API for a restaurant storage management system.

Restaurant Storage Management System Description A Nest.js API for a restaurant storage management system. Installation $ yarn install Database Seeder

Sep 12, 2022

Tile38 module for Nest framework (node.js)

Description Tile38 module for Nest. This package is based on Tile38-ts, so visit its documentation for any questions about the API. Installation $ npm

Sep 22, 2022

Projen project type for Turborepo monorepo setup.

📦 projen-turborepo Projen project type for Turborepo monorepo setup. Getting Started To create a new project, run the following command and follow th

Oct 4, 2022

A demo for E2E build piplelines in Design Systems using monorepo's and automation :zap:.

A demo for E2E build piplelines in Design Systems using monorepo's and automation :zap:.

Design System Pipelines demo What is it? A working demonstration for end-to-end build piplelines in Design Systems using Primer Primitives, Primer CSS

Oct 20, 2022

A template application for setting up a mobile app video game with IONIC, PHASER, ANGULAR and a Monorepo strategy

OpenForge Ionic Monorepo Example This is a template project for all you aspiring video game developers out there! Want to use your web application ski

Dec 22, 2022
Comments
  • Seems like doesn't support nest libs :/

    Seems like doesn't support nest libs :/

    × { constructor: { name: 'Error' }, message: "Cannot find module '@app/database'\nRequire stack:\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\.build\\apps\\boats\\src\\boats.module.js\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\.build\\apps\\boats\\src\\main.js\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\node_modules\\serverless-offline\\dist\\lambda\\handler-runner\\in-process-runner\\InProcessRunner.js\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\node_modules\\serverless-offline\\dist\\lambda\\handler-runner\\in-process-runner\\index.js\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\node_modules\\serverless-offline\\dist\\lambda\\handler-runner\\child-process-runner\\childProcessHelper.js", stack: "Error: Cannot find module '@app/database'\nRequire stack:\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\.build\\apps\\boats\\src\\boats.module.js\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\.build\\apps\\boats\\src\\main.js\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\node_modules\\serverless-offline\\dist\\lambda\\handler-runner\\in-process-runner\\InProcessRunner.js\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\node_modules\\serverless-offline\\dist\\lambda\\handler-runner\\in-process-runner\\index.js\n- D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\node_modules\\serverless-offline\\dist\\lambda\\handler-runner\\child-process-runner\\childProcessHelper.js\n at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)\n at Function.Module._load (node:internal/modules/cjs/loader:778:27)\n at Module.require (node:internal/modules/cjs/loader:1005:19)\n at require (node:internal/modules/cjs/helpers:102:18)\n at Object.<anonymous> (D:\\Desarrollos\\RevStar\\templates\\serverless-aws-nodejs-and-rds\\.build\\apps\\boats\\src\\boats.module.js:10:20)\n at Module._compile (node:internal/modules/cjs/loader:1103:14)\n at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)\n at Module.load (node:internal/modules/cjs/loader:981:32)\n at Function.Module._load (node:internal/modules/cjs/loader:822:12)\n at Module.require (node:internal/modules/cjs/loader:1005:19)" } × Cannot find module '@app/database'

    opened by fricred 0
  • Can support other sls command option like stage parameter

    Can support other sls command option like stage parameter

    Can support other sls command options rather than just have the command input right now?

    For example: npx mono --nestApp $app --command deploy --stage=$stage

    opened by ryanelee 0
  • ln -s doest not auto create file on windows 10

    ln -s doest not auto create file on windows 10

    Running the commant ln -s in windoes enviroment does not create the temp file, so its throw Error: 'Invalid "--config" value: Cannot find "serverless-<app>-mono.tmp.yml" in service folder

    opened by Diegow3b 1
  • sls package includes all apps

    sls package includes all apps

    Description

    When using the package command of sls for a given app in the mono repo, the built package actually includes all the apps (whereas it should only include the targeted app in the package)

    To Reproduce

    #1 create multiple apps in the nestjs mono repo

    $ npx nest g app car
    $ npx nest g app boat
    

    #2 package only one app

    npx serverless mono --nestApp car --command package
    

    #3 check the .zip artefact in .serverless folder, it contains both car & boat apps (whereas it should only contain the selected packaged app in the previous command)

    opened by TurhanOz 0
Releases(v1.1.0)
  • v1.1.0(Oct 10, 2022)

    What's Changed

    • ADDED extra options by @sarovin in https://github.com/blubblub/serverless-nest-monorepo/pull/5
    • Added nestjs watch mode by @sarovin in https://github.com/blubblub/serverless-nest-monorepo/pull/6

    New Contributors

    • @sarovin made their first contribution in https://github.com/blubblub/serverless-nest-monorepo/pull/5

    Full Changelog: https://github.com/blubblub/serverless-nest-monorepo/compare/1.0.0...v1.1.0

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Feb 10, 2022)

Owner
Blub Blub
Services for making faces, first sounds and words.
Blub Blub
Run a command, watch the filesystem, stop the process on file change and then run the command again...

hubmon Run a command, watch the filesystem, stop the process on file change and then run the command again... Install You can install this command lin

Hubert SABLONNIÈRE 7 Jul 30, 2022
A very small app to run three.js apps on the desktop with Deno

3D visualization with Three.js on Deno on Desktop This is a small Deno app that renders a (webkit) webview on desktop. Along with a page running Three

Jorge Romero 23 Dec 30, 2022
A very small app to run three.js apps on the desktop with Deno

Birb Engine - 3D with Three.js on Deno on Desktop This is a project for making a game engine! Intended to be customizable and about as flexible as dev

Jorge Romero 5 Jun 17, 2022
Nest multiple blocks inside lists of any kind of list (ordered, unordered, no marker, etc), or do away with list markers and use it like a repeater!

Nest multiple blocks inside lists of any kind of list (ordered, unordered, no marker, etc), or do away with list markers and use it like a repeater!

Rani 15 Dec 26, 2022
OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:

A progressive Node.js framework for building efficient and scalable server-side applications. Description OpenAPI (Swagger) module for Nest. Installat

nestjs 1.3k Jan 6, 2023
Nest.js project used it implement login page functionality.

Basic Login Backend (API) Description This project is the backend (API) portion of a basic login application. It calls a local frontend project basic-

Chad D.S 2 Mar 21, 2022
Hashing library for Nest.Js

A progressive Node.js framework for building efficient and scalable server-side applications. Description Hashing library for NestJS. Installation $ n

Arthur Reis 2 Mar 29, 2022
API of my readings, developed in Nest.js, MongoDB, Nginx and Dockerized

My Readings step by step for configuration with docker OBS: required docker and docker-compose cp -r .env.sample .env docker-compose up example what t

William Koller 30 Jul 1, 2022