Creating Node.js Code-only Functions
Find out how to package and create Node.js code-only functions in OCI Functions.
Use a Node.js code-only function when you want to deploy JavaScript code and dependencies without building a container image.
Node.js source code must be packaged under a root-level function/ directory.
Before creating a Node.js code-only function, package the function code and any dependencies using the archive layout required for Node.js code-only functions. For general code-only function prerequisites, see Creating Code-only Functions. For supported Node.js runtime versions and runtime names, see Languages supported for code-only functions.
In addition, for packaging and creating Node.js code-only functions, you need:
- A Node.js development environment.
- A JavaScript function file and handler.
- If the function has Node.js dependencies, those dependencies installed into
node_modules/. - A valid Node.js handler value.
Node.js archive requirements
For Node.js code-only functions:
- The archive must contain a root-level
function/directory. - The
function/directory contains the handler file and application source code. - The root-level
node_modules/directory is optional. Include it only when the function has Node.js dependencies. - The root-level
resources/directory is optional and contains runtime resources. - The root-level
native/directory is optional only for multi-architecture native dependency use cases. - For single-architecture Node.js functions, root-level
native/is not allowed. - For single-architecture Node.js functions with native dependencies, package native dependencies directly under
node_modules/. - For multi-architecture Node.js functions, if
native/exists, place architecture-specific native dependency files only undernative/fn-arch-x86/andnative/fn-arch-arm/. - A handler is required.
Node.js handler
The handler tells OCI Functions which JavaScript file to invoke.
Example handler:
func.js
Single-architecture Node.js archive
archive-root/
├── function/
└── func.js
├── node_modules/ # optional
└── resources/ # optional
Rules:
function/is required.node_modules/is optional.resources/is optional.native/is not allowed for single-architecture Node.js functions.- For single-architecture native dependencies, package the dependency under
node_modules/. - The handler must refer to a JavaScript file in the
function/directory.
Package Node.js source without dependencies
mkdir -p build/archive/function
cp func.js build/archive/function/
cd build/archive
zip -r ../function.zip function/
Handler:
func.js
Package Node.js source with dependencies
mkdir -p build/archive/function
cp func.js build/archive/function/
cp package.json package-lock.json build/archive/
cd build/archive
npm ci --omit=dev
zip -r ../function.zip function/ node_modules/
If using resources, include resources/ at the archive root.
Dependency packaging
OCI Functions does not download Node.js dependencies during function creation. Package required dependencies in the archive before creating or updating the function.
The Node.js FDK is included in the managed Node.js runtime for code-only functions. You do not have to package the FDK in the archive. If you package a different FDK version in node_modules/, your packaged version overrides the FDK included in the runtime. Validate compatibility before deploying to production.
Node.js archive with native dependencies
Node.js functions can depend on packages that include compiled native code, such as .node binaries or native add-ons built with tools such as node-gyp.
For single-architecture Node.js functions:
- Do not include root-level
native/. - Package native dependencies directly under
node_modules/. - Make sure dependencies are built for the correct Linux platform and architecture.
For multi-architecture Node.js functions:
- Package source under
function/. - Package shared dependencies under
node_modules/, if needed. - Package architecture-specific native dependencies under:
native/fn-arch-x86/native/fn-arch-arm/
archive-root/
├── function/
└── func.js
├── node_modules/ # optional
├── resources/ # optional
└── native/
├── fn-arch-x86/
└── fn-arch-arm/
Rules:
native/is required only if the function uses architecture-specific native dependencies in a multi-architecture application.- If
native/exists for a multi-architecture application, it must contain bothfn-arch-x86/andfn-arch-arm/. - Place native dependency files only under the matching architecture directory.
node_modules/can still be used for non-native or shared dependencies.function/is always required.
Node.js runtime behavior
The Node.js FDK is included in the managed Node.js runtime for code-only functions. Package the handler file, application code, and any required dependencies in the archive.
Creating a Node.js code-only function
Use the shared code-only functions creation steps. Select a supported Node.js runtime, enter the Node.js handler, and provide the Node.js archive.
The Node.js function is created and can be invoked like any other function.
To create a Node.js code-only function, follow the instructions in Creating Code-only Functions. For example, to create a Node.js code-only function using the Console:
- On the Applications list page, select the application in which you want to create the Node.js code-only function. If you need help finding the list page or the application, see Listing Applications.
-
Select the Functions tab.
The Functions list page opens. All functions in the selected application are displayed in a table.
- Select Create from archive and specify details for the Node.js code-only function, including the file source, the Node.js archive, a supported Node.js runtime, and a Node.js handler.
For Node.js code-only functions, you must specify a handler when you create the function.
- Specify other details for the function as required (see Creating Code-only Functions).
- Select Create to create the function.
The Node.js code-only function is created and can be invoked like any other function.
Troubleshooting
| Error Code | Problem | Likely cause | Recommended fix |
|---|---|---|---|
400 |
Archive validation fails because function/ is missing |
Source files are at the ZIP root or in the wrong folder | Put handler and application code under function/. |
400 |
Handler cannot be found | Handler file does not match the archive | Confirm that the handler file exists under function/. |
400 |
Function fails because dependency is missing | Dependency was not packaged under node_modules/ |
Run npm ci --omit=dev and include node_modules/ in the ZIP. |
400 |
Function fails because native dependency is incompatible | Dependency was built for macOS, Windows, or the wrong Linux architecture | Build or download dependencies for the correct Linux architecture. |
400 |
Single-architecture archive fails validation | Root-level native/ directory is present |
Remove native/; package single-architecture native dependencies under node_modules/. |
400 |
Multi-architecture native archive fails validation | One architecture directory is missing | Include both native/fn-arch-x86/ and native/fn-arch-arm/. |
400 |
Native archive validation fails | Files were placed directly under native/ |
Move files under native/fn-arch-x86/ or native/fn-arch-arm/. |