GithubHelp home page GithubHelp logo

adrian-d-hidalgo / bundled-nest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zensoftware/bundled-nest

0.0 1.0 0.0 141 KB

๐Ÿ’ฅ Nest ๐Ÿ”ฐ Webpack ๐Ÿ”ฐ Docker ๐Ÿ’ฅ --- ๐Ÿฏ Now archived for historical reference โ›ฉ

Dockerfile 12.82% TypeScript 48.69% JavaScript 38.49%

bundled-nest's Introduction

๐Ÿ’ฅ Nest ๐Ÿ”ฐ Webpack ๐Ÿ”ฐ Docker ๐Ÿ’ฅ

๐Ÿฏ Now archived for historical reference โ›ฉ

We've concluded that it is not recommended to bundle NestJS, or actually, NodeJS web servers in general. This is archived for historical reference during the period of time when the community was attempting to tree-shake, and bundle NestJS apps. Refer to @kamilmysliwiec comment for details:

In many real-world scenarios (depending on what libraries are being used), you should not bundle Node.js applications (not only NestJS applications) with all dependencies (external packages located in the node_modules folder). Although this may make your docker images smaller (due to tree-shaking), somewhat reduce the memory consumption, slightly increase the bootstrap time (which is particularly useful in the serverless environments), it won't work in combination with many popular libraries commonly used in the ecosystem. For instance, if you try to build NestJS (or just express) application with MongoDB, you will see the following error in your console:

Error: Cannot find module './drivers/node-mongodb-native/connection'
at webpackEmptyContext

Why? Because mongoose depends on mongodb which depends on kerberos (C++) and node-gyp.


For an example of how to build an enterprise-grade tech stack, please check out my new project:


Historical reference...

This repo is a starting point for others who wish to build a Nest app with minimal footprint. Bundling dependencies with Webpack, and containerized for Docker.

Size
production bundle of a main.js "Hello World!" 1.49 MB
Containerized as a Docker image node:12-alpine 81.9 MB

The Alpine variant of Node.js - Docker base image is being used for minimal footprint. Node.js v12 has improved startup times over an already performant startup process. This enables Nest to be an excellent solution for a microservice architecture. Allowing for efficient spinning up of new Kubernetes pods to meet compute demands, and disposed as necessary. Written in Typescript, Nest allows for a more familiar, and declarative development experience for writing modern web services. Check out the official docs to learn more.

This repo demonstrates the minimal amount needed to integrate a bundling system and containerize the app as a Docker image. Though for commercial projects, I would strongly suggest using a project management tool like Nx, by the company Nrwl. Getting Started with Nx. It makes full stack development with Nest simple. Nest schematics even work with Angular Console - VS Code Extension, with zero configuration needed.

This repo is produced from a fresh nest new command, using nest-cli, with minimal modifications. Currently there are some intricacies with bundling dependencies for a Nest app. (Issue: #1706) Comments are included in the Dockerfile and webpack.config.json for clarification. This repo addresses the known issues for you. ๐Ÿฅ‚

The following packages were added to the default package.json.

webpack@latest
webpack-cli@latest
ts-loader@latest
tsconfig-paths-webpack-plugin@latest  # For targeting "tsconfig.build.json"

3 scripts have been added to the package.json

# Bundle with webpack
npm run build:prod

# Build the docker image
npm run docker:build

# Run the docker image
npm run docker:run

For containerization, ensure your app uses the port from the environment if it exists.

"main.ts"

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // For Docker containerization
  const PORT = process.env.PORT || 3000;
  await app.listen(PORT);

  console.log(`Server is running at http://localhost:${PORT}`);
}
bootstrap();

bundled-nest's People

Contributors

zensoftware avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.