npm install @vercel/ncc

Simple CLI for compiling a Node.js module into a single file, together with all its dependencies, gcc-style.

About @vercel/ncc

The npm package @vercel/ncc offers a streamlined solution for Node.js developers aiming to simplify deployment processes and improve performance. This tool compiles a Node.js module into a single file, bundling all its dependencies, much like how gcc compiles C programs. This feature significantly reduces the complexity of deployment and the runtime size, as it minimizes the number of files involved in the deployment package. By using @vercel/ncc, developers benefit from faster startup times since Node.js no longer needs to resolve and load multiple modules and dependencies at runtime. This package also enhances the portability of applications, making it easier to share or deploy across different environments without worrying about dependency mismatches or missing modules.

To start using @vercel/ncc, developers can easily integrate it into their projects by running the command npm install @vercel/ncc. This installation adds the capability to compile entire Node.js projects into a single executable file, which includes all JavaScript, JSON, and asset files. The @vercel/ncc package is particularly useful for applications that need to be deployed in serverless environments or distributed as self-contained executables. Additionally, the use of @vercel/ncc can lead to improved security by reducing the surface area for attacks, as fewer files and external dependencies mean fewer vectors for malicious exploits. This npm module supports source maps, ensuring that even after compilation, developers can debug their applications effectively, maintaining the original source structure in a compiled output file.

The @vercel/ncc module is designed with simplicity and efficiency in mind, providing a powerful tool for developers looking to optimize their Node.js applications. It seamlessly integrates into development workflows, offering a robust solution for compiling and deploying Node.js modules with ease. With its ability to create single-file executables, @vercel/ncc not only simplifies the deployment process but also enhances the overall security and performance of Node.js applications. Whether for development or production, @vercel/ncc is an indispensable tool for modern Node.js development, ensuring applications are lightweight, fast, and easy to maintain.

More from vercel

vercel npm packages

Find the best node modules for your project.

Search npm

@next/env

Next...

Read more
,

next

The React...

Read more
,

styled-jsx

Full CSS support for JSX without...

Read more
,

@next/eslint-plugin-next

ESLint plugin for Next.js...

Read more
,

eslint-config-next

ESLint configuration used by Next.js...

Read more
,

@edge-runtime/primitives

A set of primitives to build Vercel Edge Runtime...

Read more
,

@vercel/analytics

Gain real-time traffic insights with Vercel Web...

Read more
,

vercel

The command-line interface for...

Read more
,

@vercel/speed-insights

Speed Insights is a tool for measuring web performance and providing suggestions for improvement...

Read more
,

@vercel/error-utils

A collection of error utilities for...

Read more
,

@vercel/gatsby-plugin-vercel-analytics

Track Core Web Vitals in Gatsby projects with Vercel Speed Insights...

Read more
,

@vercel/ncc

Simple CLI for compiling a Node...

Read more

Dependencies

Core dependencies of this npm package and its dev dependencies.

@azure/cosmos, @bugsnag/js, @ffmpeg-installer/ffmpeg, @google-cloud/bigquery, @google-cloud/firestore, @sentry/node, @slack/web-api, @vercel/webpack-asset-relocator-loader, analytics-node, apollo-server-express, arg, auth0, aws-sdk, axios, azure-storage, browserify-middleware, bytes, canvas, chromeless, consolidate, copy, core-js, cowsay, esm, express, fetch-h2, firebase, firebase-admin, fluent-ffmpeg, fontkit, get-folder-size, glob, got, graceful-fs, graphql, hot-shots, ioredis, isomorphic-unfetch, jest, jimp, jugglingdb, koa, leveldown, license-webpack-plugin, lighthouse, loopback, mailgun, mariadb, memcached, memory-fs, mkdirp, mongoose, mysql, node-gyp, npm, oracledb, passport, passport-google-oauth, path-platform, pdfkit, pg, pug, react, react-dom, redis, request, rxjs, saslprep, sequelize, sharp, shebang-loader, socket.io, source-map-support, stripe, swig, terser, the-answer, tiny-json-http, ts-loader, tsconfig-paths, tsconfig-paths-webpack-plugin, twilio, typescript, vm2, vue, vue-server-renderer, web-vitals, webpack, when

Documentation

A README file for the @vercel/ncc code repository. View Code

ncc

CI Status

Simple CLI for compiling a Node.js module into a single file, together with all its dependencies, gcc-style.

Motivation

Design goals

Usage

Installation

npm i -g @vercel/ncc

Usage

$ ncc <cmd> <opts>

Eg:

$ ncc build input.js -o dist

If building an .mjs or .js module inside a "type": "module" package boundary, an ES module output will be created automatically.

Outputs the Node.js compact build of input.js into dist/index.js.

Note: If the input file is using a .cjs extension, then so will the corresponding output file. This is useful for packages that want to use .js files as modules in native Node.js using a "type": "module" in the package.json file.

Commands:

  build <input-file> [opts]
  run <input-file> [opts]
  cache clean|dir|size
  help
  version

Options:

  -o, --out [dir]          Output directory for build (defaults to dist)
  -m, --minify             Minify output
  -C, --no-cache           Skip build cache population
  -s, --source-map         Generate source map
  -a, --asset-builds       Build nested JS assets recursively, useful for
                           when code is loaded as an asset eg for workers.
  --no-source-map-register Skip source-map-register source map support
  -e, --external [mod]     Skip bundling 'mod'. Can be used many times
  -q, --quiet              Disable build summaries / non-error outputs
  -w, --watch              Start a watched build
  -t, --transpile-only     Use transpileOnly option with the ts-loader
  --v8-cache               Emit a build using the v8 compile cache
  --license [file]         Adds a file containing licensing information to the output
  --stats-out [file]       Emit webpack stats as json to the specified output file
  --target [es]            ECMAScript target to use for output (default: es2015)
                           Learn more: https://webpack.js.org/configuration/target
  -d, --debug              Show debug logs

Execution Testing

For testing and debugging, a file can be built into a temporary directory and executed with full source maps support with the command:

$ ncc run input.js

With TypeScript

The only requirement is to point ncc to .ts or .tsx files. A tsconfig.json file is necessary. Most likely you want to indicate es2015 support:

{
  "compilerOptions": {
    "target": "es2015",
    "moduleResolution": "node"
  }
}

If typescript is found in devDependencies, that version will be used.

Package Support

Some packages may need some extra options for ncc support in order to better work with the static analysis.

See package-support.md for some common packages and their usage with ncc.

Programmatically From Node.js

require('@vercel/ncc')('/path/to/input', {
  // provide a custom cache path or disable caching
  cache: "./custom/cache/path" | false,
  // externals to leave as requires of the build
  externals: ["externalpackage"],
  // directory outside of which never to emit assets
  filterAssetBase: process.cwd(), // default
  minify: false, // default
  sourceMap: false, // default
  assetBuilds: false, // default
  sourceMapBasePrefix: '../', // default treats sources as output-relative
  // when outputting a sourcemap, automatically include
  // source-map-support in the output file (increases output by 32kB).
  sourceMapRegister: true, // default
  watch: false, // default
  license: '', // default does not generate a license file
  target: 'es2015', // default
  v8cache: false, // default
  quiet: false, // default
  debugLog: false // default
}).then(({ code, map, assets }) => {
  console.log(code);
  // Assets is an object of asset file names to { source, permissions, symlinks }
  // expected relative to the output code (if any)
})

When watch: true is set, the build object is not a promise, but has the following signature:

{
  // handler re-run on each build completion
  // watch errors are reported on "err"
  handler (({ err, code, map, assets }) => { ... })
  // handler re-run on each rebuild start
  rebuild (() => {})
  // close the watcher
  void close ();
}

Caveats