Nest: npm run build —— support "ncc"

Created on 18 Jan 2019  ·  12Comments  ·  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

npm run build .

Expected behavior

npm run build and without node_modules.

Minimal reproduction of the problem with instructions

npm run build .

What is the motivation / use case for changing the behavior?

npm run build . Then, like:
image

https://github.com/zeit/ncc

needs clarification

Most helpful comment

I just wanted to add my own experience in here, i'm currently using nrwl/nx to manage a angular/nestjs mono repo project and i am building docker containers, currently its nothing more than a hello world app for quick testing/comparison.

To make things as fair as possible, i have a package.json that's used for installation in the docker container that only has the required packages to start the nestjs app.

From just an initial impression (and trying to shrink my docker containers as much as possible) i found this quite interesting for comparison.

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
proj/api            ngbuild           1fec569e672c        9 seconds ago         194MB
proj/api            nccbuild          c4fe3279cd5e        2 minutes ago         86.6MB
node                10-alpine         fe6ff768f798        3 weeks ago           70.7MB

I did notice however when running the ncc build, i get a deprecation warning about Buffer usage:

(node:1) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

I'm not running any of these in production but it would be interesting to see what impact/side effects it has on things like memory footprint and other performance metrics.

I did not have to do anything special to make ncc work, i simply ran my command pointing to the main.ts file.

nrwl/nx puts apps (include api's) in a apps folder so my structure is apps/api/src/main.ts for my main.ts file, i simply ran ncc build apps/api/src/main.ts -o dist/apps/api/ and had a index.js file located in dist/apps/api/, it also spit out a bunch of files/directories under dist/apps/api/lib but i did not include those in my docker image and it ran just fine (also runs fine doing node index.js).

Just putting it out there that i think this could be a very interesting case for trying to use docker files and trying to keep them as small as possible for both transfer times and boot times.

Dockerfile for ng build based app

FROM node:10-alpine
COPY dist/apps/api/package.json /app/package.json
COPY dist/apps/api/package-lock.json /app/package-lock.json

RUN cd /app && npm install --production # && ash ./prune.sh /app/

COPY dist/apps/api /app

EXPOSE 3000

CMD ["node", "/app/main.js"]

Dockerfile for ncc build based app

FROM node:10-alpine

COPY dist/apps/api-ncc/index.js /app/index.js

EXPOSE 3000

CMD ["node", "/app/index.js"]

edit1: fixed layout of docker images output
edit2: added docker file.

All 12 comments

@zuohuadong what kind of issue ncc solve?

Why would you like to use ncc?

Why would you like to use ncc?

@cojack @kamilmysliwiec

It can turn the entire project (including xxx) into a js file.

When running online, we only need to transfer a js file.

Easy to deploy and run at: https://aws.amazon.com/cn/serverless/

@zuohuadong yeah it's called bundling, and since you're transpiling your TS/JS anyway, there's 0 reason to use ncc at all.

@zuohuadong did you hear about npm pack?

btw, running nest as lambda on aws, is not best choose

顺便说一句,在aws上运行nest作为lambda,不是最佳选择

I want to be able to deploy just one file in a formal environment like golang, instead of executing npm install .
Not just in aws, vps.

Consider to use docker instead of running your application in VPS environment.

I just wanted to add my own experience in here, i'm currently using nrwl/nx to manage a angular/nestjs mono repo project and i am building docker containers, currently its nothing more than a hello world app for quick testing/comparison.

To make things as fair as possible, i have a package.json that's used for installation in the docker container that only has the required packages to start the nestjs app.

From just an initial impression (and trying to shrink my docker containers as much as possible) i found this quite interesting for comparison.

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
proj/api            ngbuild           1fec569e672c        9 seconds ago         194MB
proj/api            nccbuild          c4fe3279cd5e        2 minutes ago         86.6MB
node                10-alpine         fe6ff768f798        3 weeks ago           70.7MB

I did notice however when running the ncc build, i get a deprecation warning about Buffer usage:

(node:1) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

I'm not running any of these in production but it would be interesting to see what impact/side effects it has on things like memory footprint and other performance metrics.

I did not have to do anything special to make ncc work, i simply ran my command pointing to the main.ts file.

nrwl/nx puts apps (include api's) in a apps folder so my structure is apps/api/src/main.ts for my main.ts file, i simply ran ncc build apps/api/src/main.ts -o dist/apps/api/ and had a index.js file located in dist/apps/api/, it also spit out a bunch of files/directories under dist/apps/api/lib but i did not include those in my docker image and it ran just fine (also runs fine doing node index.js).

Just putting it out there that i think this could be a very interesting case for trying to use docker files and trying to keep them as small as possible for both transfer times and boot times.

Dockerfile for ng build based app

FROM node:10-alpine
COPY dist/apps/api/package.json /app/package.json
COPY dist/apps/api/package-lock.json /app/package-lock.json

RUN cd /app && npm install --production # && ash ./prune.sh /app/

COPY dist/apps/api /app

EXPOSE 3000

CMD ["node", "/app/main.js"]

Dockerfile for ncc build based app

FROM node:10-alpine

COPY dist/apps/api-ncc/index.js /app/index.js

EXPOSE 3000

CMD ["node", "/app/index.js"]

edit1: fixed layout of docker images output
edit2: added docker file.

考虑使用docker而不是在VPS环境中运行应用程序。

Of course you can deploy it in docker.
However, some customers do not have a docker environment, and distributing binary files is more convenient. Like: https://caddyserver.com/download
The other one is: https://github.com/zeit/pkg

in NCC we can: node nest.js (Only this one file.)
in PKG we can: ./nest (The node.js runtime environment is not required.)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings