WebIDE runs in the same environment as the Function Compute execution environment, so packages you install in the WebIDE terminal behave identically at runtime. This topic explains how to install and package third-party dependencies for Python, Node.js, PHP, and custom runtimes.
Choose an installation approach
For each runtime, you can install dependencies using one of two approaches:
| Approach | When to use | Directory |
|---|---|---|
| Install directly into the code directory | Small dependencies, frequent deployments | /code |
| Package dependencies as a layer | Large dependencies, infrequent changes | Runtime-specific (see sections below) |
Large dependency sets increase cold start latency, function code package size, and deployment time. Packaging them as a layer separates the dependency lifecycle from your function code — you update the layer only when dependencies change, not on every deployment.
Prerequisites
Before you begin, make sure you have:
A function created in Function Compute with a WebIDE instance running
Access to the WebIDE terminal
Python
Python runtimes include a set of built-in Python modules. For any module not in that list, install it from the WebIDE terminal.
Install directly into /code
Run the following command in the /code directory of the WebIDE terminal:
pip install -t . flask
# Or, using a requirements file:
pip install -t . -r requirements.txtInstalling packages directly into /code places them alongside your function code. With many packages installed, locating index.py in the resource manager becomes difficult as the directory fills with package files and folders. If this is a concern, install into a separate directory instead.
Install into a separate directory
To keep your code directory clean, install packages into a /python subdirectory, then set an environment variable so the runtime can find them:
mkdir python
cd python
pip install -t . flaskAdd the PYTHONPATH=/code/python environment variable to your function configuration.
Package as a layer
If the dependency set is large, package the /python directory as a layer. This reduces the function code package size and speeds up subsequent deployments.
When you use/pythonas your layer directory, Function Compute automatically adds it to the Python search path. You do not need to setPYTHONPATH=/opt/python.
Run the following commands in sequence:
# Step 1: Archive the python directory
zip -ry python.zip python
# Step 2: Publish the archive as a layer
s cli fc3 layer publish \
--layer-name myPythonLibLayer \
--code /code/python.zip \
--compatible-runtime python3.10,python3.9,python3.6,custom,custom.debian10 \
--region cn-hangzhou \
-a defaultThe command outputs the layer ARN, for example:
395da10bf789aa49dd035db01bab****#myPythonLibLayer#1After the layer is published:
Update your function configuration to reference the new layer version.
Delete the local
/pythondirectory and archive to reduce the function code package size:rm -rf python python.zipDeploy the updated function.
For instructions on creating and referencing layers, see Create a custom layer and Configure a custom layer.
Node.js
Node.js runtimes include a set of built-in Node.js modules. For any module not in that list, install it from the WebIDE terminal.
Install directly into /code
Make sure package.json exists in /code, then run:
npm installPackage as a layer
If the dependency set is large, move node_modules into a /nodejs directory and package it as a layer:
# Step 1: Move node_modules into the nodejs directory
mkdir nodejs
mv node_modules ./nodejs
# Step 2: Archive the directory
zip -ry nodejs.zip nodejs
# Step 3: Publish the archive as a layer
s cli fc3 layer publish \
--layer-name myNodeLibLayer \
--code /code/nodejs.zip \
--compatible-runtime nodejs16,nodejs14,nodejs12,nodejs10,custom,custom.debian10 \
--region cn-hangzhou \
-a defaultThe command outputs the layer ARN, for example:
395da10bf789aa49dd035db01bab****#myNodeLibLayer#1After the layer is published:
Update your function configuration to reference the new layer version.
Delete the local
/nodejsdirectory and archive:rm -rf nodejs nodejs.zipDeploy the updated function.
For instructions on creating and referencing layers, see Create a custom layer and Configure a custom layer.
PHP
The installation process for PHP follows the same pattern as Python and Node.js. The difference is the layer directory: PHP uses /opt/php instead of /python or /nodejs.
For the full list of layer directories by runtime, see the "Directories that can be added in each runtime" section in Create a custom layer.
Custom runtimes
If your custom runtime uses Python, Node.js, or PHP: Follow the steps in the corresponding sections above.
If your custom runtime uses a compiled language such as Java or Go: Use the SDK provided by WebIDE to compile and package your dependencies.