@netlify/zip-it-and-ship-it v9.41.1
Creates Zip archives from Node.js, Go, and Rust programs. 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-itUsage (Node.js)
zipFunctions(srcFolders, destFolder, options?)
srcFolders:string|Array<string>destFolder:stringoptions:object?- Return value:
Promise<object[]>
import { zipFunctions } from '@netlify/zip-it-and-ship-it'
const archives = await zipFunctions('functions', 'functions-dist', {
archiveFormat: 'zip',
})Creates Zip archives from Node.js, Go, and Rust programs. Those archives are ready to be uploaded to AWS Lambda.
srcFolders
A directory or a list of directories containing the source files. If a string is provided, the corresponding directory must exist. If an array of strings is provided, at least one directory must exist.
In Netlify, this directory is the "Functions folder".
A source folder can contain:
- Sub-directories with a main file called
index.jsor{dir}.jswhere{dir}is the sub-directory name. .js,.mjs,.cjs,.ts,.tsx,.mtsor.ctsfiles (Node.js).ziparchives with Node.js already ready to upload to AWS Lambda.- Go programs already compiled. Those are copied as is.
- Rust programs already compiled. Those are zipped.
destFolder
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
An optional object for customizing the behavior of the archive creation process.
archiveFormat
- Type:
string - Default value:
zip
Format of the archive created for each function. Defaults to ZIP archives.
If set to none, the output of each function will be a directory containing all the bundled files.
basePath
- Type:
string - Default value:
undefined
The directory which all relative paths will be resolved from. These include paths in the includedFiles config
property, as well as imports using dynamic expressions such as require(\./files/${name}`)`.
config
- Type:
object - Default value:
{}
An object matching glob-like expressions to objects containing configuration properties. Whenever a function name matches one of the expressions, it inherits the configuration properties.
The following properties are accepted:
externalNodeModules- Type:
array<string>
List of Node modules to include separately inside a node_modules directory.
- Type:
ignoredNodeModules- Type:
array<string>
List of Node modules to keep out of the bundle.
- Type:
nodeBundler- Type:
string - Default value:
zisi
The bundler to use when processing JavaScript functions. Possible values:
zisi,esbuild,esbuild_zisiornft.When the value is
esbuild_zisi,esbuildwill be used with a fallback tozisiin case of an error.- Type:
nodeSourcemap- Type:
boolean - Default value:
false
Whether to include a sourcemap file in the generated archive.
Available only when
nodeBundleris set toesbuildoresbuild_zisi.- Type:
nodeVersion- Type:
string\ - Default value:
18
The version of Node.js to use as the compilation target for bundlers. This is also used to determine the runtime reported in the manifest. Any valid and supported Node.js version is accepted. Examples:
14.x16.1.0v18nodejs18.x(for backwards compatibility)
- Type:
rustTargetDirectory- Type:
string - Default value: Path to a temporary directory
The path to use as the Cargo target directory when bundling Rust functions from source. When a value is not specified, a random temporary directory will be used.
The
[name]placeholder will be replaced by the name of the function, allowing you to use it to construct the path to the target directory.- Type:
name- Type:
string - Default value: undefined
A name to use when displaying the function in the Netlify UI. Populates the
displayNameproperty in the functions manifest for the specified function.- Type:
generator- Type:
string - Default value: undefined
A field to use if the function has been autogenerated by a plugin or integration. A recommended format is
@netlify/fake-plugin@1.0.0, where adding the version is highly appreciated.- Type:
featureFlags
See feature flags.
manifest
- Type:
string - Default value:
undefined
Defines the full path, including the file name, to use for the manifest file that will be created with the functions
bundling results. For example, path/to/manifest.json. This file is a JSON-formatted string with the following
properties:
functions: An array with the functions created, in the same format as returned byzipFunctionssystem.arch: The operating system CPU architecture, as returned byprocess.archsystem.platform: The operating system, as returned byprocess.platformtimestamp: The timestamp (in milliseconds) at the time of the functions bundling processversion: The version of the manifest file (current version is1)
parallelLimit
- Type:
number - Default value:
5
Maximum number of functions to bundle at the same time.
internalSrcFolder
- Type:
string - Default value:
undefined
Defines the path to the folder with internal functions. Used to populate a function's generator property if
generator is not configured in the function's config itself, if its path is within this specified internal functions
folder.
Return value
This returns a Promise resolving to an array of objects describing each archive. Every object has the following
properties.
mainFile:stringThe path to the function's entry file.
name:stringThe name of the function.
path:stringAbsolute file path to the archive file.
runtimestringEither
"js","go", or"rs".size:numberThe size of the generated archive, in bytes.
displayName:stringIf there was a user-defined configuration object applied to the function, and it had a
namedefined. This will be returned here.generator:stringIf there was a user-defined configuration object applied to the function, and it had
generatordefined. This will be returned here. If there was nothing defined, but aninternalSrcFolderwas passed and the function was defined in there, it will return a string to specify it was an internal function.
Additionally, the following properties also exist for Node.js functions:
bundler:stringContains the name of the bundler that was used to prepare the function.
bundlerErrors:Array<object>Contains any errors that were generated by the bundler when preparing the function.
bundlerWarnings:Array<object>Contains any warnings that were generated by the bundler when preparing the function.
config:objectThe user-defined configuration object that was applied to a particular function.
inputs:Array<string>A list of file paths that were visited as part of the dependency traversal. For example, if
my-function.jscontainsrequire('./my-supporting-file.js'), theinputsarray will contain bothmy-function.jsandmy-supporting-file.js.nativeNodeModules:objectA list of Node modules with native dependencies that were found during the module traversal. This is a two-level object, mapping module names to an object that maps the module path to its version. For example:
{ "nativeNodeModules": { "module-one": { "/full/path/to/the/module": "1.0.0", "/another/instance/of/this/module": "2.0.0" } } }runtimeAPIVersionnumber | undefinedWhen a function with the new API will be detected this is set to
2. Otherwise it is set to1.undefinedis only returned in case of an error.runtimeVersionstring | undefinedWhich exact runtime this function should be using. (e.g.
nodejs18.x). This value is detected based on the inputnodeVersion.
zipFunction(srcPath, destFolder, options?)
srcPath:stringdestFolder:stringoptions:object?- Return value:
object | undefined
import { zipFunction } from '@netlify/zip-it-and-ship-it'
const archive = await zipFunction('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(srcFolders, options?)
Returns the list of functions to bundle.
import { listFunctions } from '@netlify/zip-it-and-ship-it'
const functions = await listFunctions('functions/function.js')srcFolders
A directory or a list of directories containing the source files. If a string is provided, the corresponding directory must exist. If an array of strings is provided, at least one directory must exist.
options
An optional options object.
featureFlags
See feature flags.
Return value
Each object has the following properties:
name:stringFunction's name. This is the one used in the Function URL. For example, if a Function is a
myFunc.jsregular file, thenameismyFuncand the URL ishttps://{hostname}/.netlify/functions/myFunc.displayName:stringIf there was a user-defined configuration object applied to the function, and it had a
namedefined. This will be returned here.mainFile:stringAbsolute path to the Function's main file. If the Function is a Node.js directory, this is its
index.jsor{dir}.jsfile.runtime:stringEither
"js","go", or"rs".extension:stringSource file extension. For Node.js, this is either
.js,.tsor.zip. For Go, this can be anything.generator:stringIf there was a user-defined configuration object applied to the function, and it had
generatordefined. This will be returned here.
listFunctionsFiles(srcFolders)
Like listFunctions(), except it returns not only the Functions main files, but
also all their required files. This is much slower.
import { listFunctionsFiles } from '@netlify/zip-it-and-ship-it'
const functions = await listFunctionsFiles('functions/function.js')srcFolders
A directory or a list of directories containing the source files. If a string is provided, the corresponding directory must exist. If an array of strings is provided, at least one directory must exist.
options
An optional options object.
featureFlags
See feature flags.
Return value
The return value is the same as listFunctions() but with the following additional
properties.
srcFile:stringAbsolute file to the source file.
Usage (CLI)
$ zip-it-and-ship-it srcFolder destFolderThe CLI performs the same logic as zipFunctions(). The archives are
printed on stdout as a JSON array.
Bundling Node.js functions
zip-it-and-ship-it uses two different mechanisms (bundlers) for preparing Node.js functions for deployment. You can
choose which one to use for all functions or on a per-function basis.
zisi
This is the default bundler.
When using the zisi bundler, the following files are included in the generated archive:
- All files/directories within the same directory (except
node_modules) - All the files referenced using static
require()calls- Example (user file):
require('./lib/my-file') - Example (Node module):
require('date-fns')
- Example (user file):
The following files are excluded:
@types/*TypeScript definitionsaws-sdk(on Node v16 and earlier)@aws-sdk/*(on Node v18 and later)- Temporary files like
*~,*.swp, etc.
esbuild
The esbuild bundler can generate smaller archives due to its tree-shaking step. It's
also a lot faster. You can read more about it on
the Netlify Blog.
When using esbuild, only the files that are directly required by a function or one of its dependencies will be included. For example, a Node module has 1,000 files but your function only requires one of them, the other 999 will not be included in the bundle.
You can enable esbuild by setting the config option when calling zipFunction or zipFunctions:
import { zipFunctions } from '@netlify/zip-it-and-ship-it'
const archives = await zipFunctions('functions', 'functions-dist', {
config: {
// Applying these settings to all functions.
'*': {
nodeBundler: 'esbuild',
},
},
})Feature flags
zip-it-and-ship-it uses feature flags to enable or disable features during their testing or deprecation periods.
These are supplied to each of the entrypoint functions (zipFunction, zipFunctions, listFunctions and
listFunctionsFiles) as a named parameter called featureFlags. It consists of an object where each key is the name of
a feature flag and the values are Booleans indicating whether each feature flag is enabled or disabled.
The list of all feature flags currently being used can be found here.
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:
- Build-time Node.js version: this defaults to Node
18, but can be overridden with a.nvmrcorNODE_VERSIONenvironment variable. - Function runtime Node.js version: this defaults to
nodejs18.xbut can be overridden with aAWS_LAMBDA_JS_RUNTIMEenvironment variable.
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.
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
