npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serverless-nest-monorepo

v1.1.0

Published

Serverless plugin to support NestJS monorepo

Downloads

3,573

Readme

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.