In a custom runtime, Function Compute mounts layer contents under /opt but does not automatically add that path to the runtime's module search path. Unlike managed runtimes, custom runtimes do not include built-in path configuration, so you must register the layer directory with your runtime language — either through an environment variable or a code-level path call.
This page covers Python, Node.js, Java, and PHP.
How it works
When you attach a layer to a function, Function Compute extracts the layer's .zip file into /opt. The subdirectory depends on how you packaged the layer:
If you followed the Create a custom layer guide, the contents land in the default subdirectory for your language (for example,
/opt/python).If you used a custom directory name (for example,
my-layer-code), the contents land in/opt/my-layer-code.
Use the actual directory path when configuring environment variables or setting the search path in code.
Quick reference
| Language | Configuration method | Value |
|---|---|---|
| Python | PYTHONPATH environment variable | /opt/python |
| Python | sys.path.append in handler file | /opt/python |
| Node.js | NODE_PATH environment variable | /opt/nodejs/node_modules |
| Java | -classpath in startup command | /code/:/opt/java/lib/* |
| Java | CLASSPATH environment variable | /code/:/opt/java/lib/* |
| PHP | set_include_path in handler file | /opt/php |
Python
Choose one of the following methods.
Method 1: Set the `PYTHONPATH` environment variable
Add the layer directory to the PYTHONPATH environment variable on your function:
PYTHONPATH=/opt/pythonMethod 2: Modify the handler file
Add the following lines at the top of your handler file, before any imports from the layer:
import sys
sys.path.append('/opt/python')
# import <PackageFromLayer>For a working example, see python-demo-with-lib-in-layer.
Node.js
Set the NODE_PATH environment variable on your function:
NODE_PATH=/opt/nodejs/node_modulesFor a working example, see nodejs-demo-with-lib-in-layer.
Java
Choose one of the following methods.
Method 1: Use `-classpath` in the startup command
Add /opt/java/lib/* to the -classpath parameter in your startup command:
java -Dserver.port=9000 -classpath /code/:/opt/java/lib/* com.example.demo.DemoApplicationMethod 2: Set the `CLASSPATH` environment variable
Add the layer directory to the CLASSPATH environment variable on your function:
CLASSPATH=/code/:/opt/java/lib/*If you use CLASSPATH, do not use the -jar parameter to launch your application. When Java runs with -jar, the JVM reads the entry point from MANIFEST.MF and ignores all classpath settings from environment variables and command-line arguments. For example, the following command does not load the layer dependencies:
java -classpath ${CLASSPATH} -jar yourJarExe.jarPHP
Add the following lines at the top of your handler file, before any imports from the layer:
<?php
$path = '/opt/php';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);