Creates Zip Archives From Node.js Or Go Source Files

Creates Zip Archives From Node.js Or Go Source Files

zip-it-and-ship-it Creates Zip archives from Node.js or Go source files. Those archives are ready to be uploaded to AWS Lambda. Intelligently prepare Node.js Lambda functions for deployment

zip-it-and-ship-it

Creates Zip archives from Node.js or Go source files. Those archives are ready to be uploaded to AWS Lambda.

This library is used under the hood by several Netlify features, including production CI builds, Netlify CLI and the JavaScript client.

Check Netlify documentation for:

Installation

npm install @netlify/zip-it-and-ship-it

Usage (Node.js)

zipFunctions(srcFolder, destFolder, options?)

srcFolder: string destFolder: string options: object? Return value: Promise<object[]>

const { zipFunctions } = require('@netlify/zip-it-and-ship-it')

const archives = await zipFunctions('functions', 'functions-dist')

Creates Zip archives from Node.js or Go source files. Those archives are ready to be uploaded to AWS Lambda.

srcFolder is the source files directory. It must exist. In Netlify, this is the "Functions folder".

srcFolder can contain:

  • Sub-directories with a main file called either index.js or {dir}.js where {dir} is the sub-directory name.
  • .js files (Node.js)
  • .zip archives with Node.js already ready to upload to AWS Lambda.
  • Go files, already built. If the options.zipGo is true, those are zipped. Otherwise, those are copied as is.

When using Node.js files, only the dependencies required by the main file are bundled, in order to keep the archive as small as possible, which improves the Function runtime performance:

  • All files/directories within the same directory (except node_modules) are included
  • All the require()'d files are included
  • All the require()'d node_modules are included, recursively
  • The following modules are never included:
    • @types/* TypeScript definitions
    • aws-sdk
  • Temporary files like *~, *.swp, etc. are not included

This is done by parsing the JavaScript source in each Function file, and reading the package.json of each Node module.

destFolder is the directory where each .zip archive should be output. It is created if it does not exist. In Netlify CI, this is an unspecified temporary directory inside the CI machine. In Netlify CLI, this is a .netlify/functions directory in your build directory.

Options

zipGo

Type: boolean Default value: false

Whether to zip Go files. If false, the Go files are copied as is and the filename remains the same.

parallelLimit

Type: number Default value: 5

Maximum number of Functions to bundle at the same time.

Return value

This returns a Promise resolving to an array of objects describing each archive. Each object has the following properties.

path

Type: string

Absolute file path to the archive file.

runtime

Type: string

Either "js" or "go".

zipFunction(srcPath, destFolder, options?)

srcPath: string destFolder: string options: object? Return value: object | undefined

const { zipFunction } = require('@netlify/zip-it-and-ship-it')

const archive = await zipFunctions('functions/function.js', 'functions-dist')

This is like zipFunctions() except it bundles a single Function.

The return value is undefined if the Function is invalid.

listFunctions(srcFolder)

srcFolder: string Return value: Promise<object[]>

Returns the list of Functions to bundle.

const { listFunctions } = require('@netlify/zip-it-and-ship-it')

const functions = await listFunctions('functions/function.js')

Return value

Each object has the following properties.

mainFile

Type: string

Absolute path to the Function's main file. If the Function is a Node.js directory, this is its index.js or {dir}.js file.

runtime

Type: string

Either "js" or "go".

extension

Type: string

Source file extension. For Node.js, this is either .js or .zip. For Go, this can be anything.

listFunctionsFiles(srcFolder)

srcFolder: string Return value: Promise<object[]>

Like listFunctions(), except it returns not only the Functions main files, but also all their required files. This is much slower.

const { listFunctionsFiles } = require('@netlify/zip-it-and-ship-it')

const functions = await listFunctionsFiles('functions/function.js')

Return value

The return value is the same as listFunctions() but with the following additional properties.

srcFile

Type: string

Absolute file to the source file.

Usage (CLI)

$ zip-it-and-ship-it srcFolder destFolder

The CLI performs the same logic as zipFunctions(). The archives are printed on stdout as a JSON array.

The following options are available:

  • --zip-go: see the zipGo option

Troubleshooting

Build step

zip-it-and-ship-it does not build, transpile nor install the dependencies of the Functions. This needs to be done before calling zip-it-and-ship-it.

Missing dependencies

If a Node module require() another Node module but does not list it in its package.json (dependencies, peerDependencies or optionalDependencies), it is not bundled, which might make the Function fail.

More information in this issue.

Conditional require

Files required with a require() statement inside an if or try/catch block are always bundled.

More information in this issue.

Dynamic require

Files required with a require() statement whose argument is not a string literal, e.g. require(variable), are never bundled.

More information in this issue.

Node.js native modules

If your Function or one of its dependencies uses Node.js native modules, the Node.js version used in AWS Lambda might need to be the same as the one used when installing those native modules.

In Netlify, this is done by ensuring that the following Node.js versions are the same:

Note that this problem might not apply for Node.js native modules using the N-API.

More information in this issue.

File Serving

As of v0.3.0 the serveFunctions capability has been extracted out to Netlify Dev.

Download Details:

Author: netlify

Source Code: https://github.com/netlify/zip-it-and-ship-it

nodejs javascript node

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Node canvas is a Cairo backed Canvas implementation for NodeJS.

node-canvas is a Cairo-backed Canvas implementation for Node.js.

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

Decoding Nodejs

The main goal of this blog is to explain the “Architecture of Nodejs” and to know how the Nodejs works behind the scenes. Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, and including Nodejs follows multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

Node.js for Beginners - Learn Node.js from Scratch (Step by Step)

Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.