Built-in Dependencies
SCF runtime environments have provided some dependent libraries, which can be queried in the corresponding runtime development guide:
Installing Dependent Libraries
You can save the dependent libraries of the SCF code in the code package and upload it to the cloud for use by SCF. SCF supports the following runtimes and usage methods:
Node.js runtime
The Node.js runtime supports installing dependent libraries in the following three ways:
You can use a dependency manager (such as npm) to locally install the dependent library, package and upload it with the function code.
Note
Place the entry point file of the function in the root directory of the zip
package. If you package and upload the entire folder as a zip
package, the function creation will fail because the entry point file cannot be found in the unzipped root directory.
This document takes installing the
lodash
library as an example.
- Run the
mkdir test-package
command on your local computer to create a directory to store the function code and the dependent library. - Run the following commands to install the
lodash
dependent library in the directory.
cd test-package
npm install lodash
- Create the
index.js
entry point file of the function in the directory and import the lodash
library in codes.
'use strict';
const _ = require('lodash');
exports.main_handler = async (event, context) => {
console.log("Hello World")
console.log(event)
console.log(event["non-exist"])
console.log(context)
return event
};
- Compress the function code and dependent library to a zip package. Upload the zip package and create a function via the SCF console.
- Log in to the SCF console and click Functions on the left sidebar.
- Choose a region at the top of the Functions page and click Create.
- Enter the basic information of the function on the Create page.

- Creation method: Select From scratch.
- Runtime environment: Choose Node.js12.16.
- Submitting method: Choose Local ZIP file.
- Click Complete.
The Node.js runtime supports online dependency installation. You can install the dependent library online according to the configuration in package.json
. For details, see Online Dependency Installation
The SCF online editor Serverless Web IDE supports Terminal capability that has a built-in npm
package management tool.
Note
Serverless Web IDE has a delay in supporting newer versions of runtime environments. If the console does not open up the Serverless Web IDE for the corresponding runtime environment, package and upload the dependency library together with the code for dependency installation or install the dependency online.
This document takes installing the
lodash
library in the terminal as an example:
- Log in to the SCF console and click Functions on the left sidebar.
- In the function list, click a function name to enter the function details page.
- On the Function management page, select Function Code > Edit codes to view and edit the function.
- Select Terminal > New Terminal on the topbar of the IDE to open the terminal window.
- Run the following commands in the terminal window to install the
lodash
dependent library.
cd src
npm install lodash
- After the installation is complete, check
package.json
and node_modules
in the file tree on the left side of the IDE. - Click Deploy to package the dependency library and upload it to the cloud together with the function code.

Python runtime
The Python runtime supports installing dependent libraries in the following two ways.
You can use a dependency manager (such as pip) to locally install the dependent library, package and upload it with the function code.
Note
- Place the function’s entry point file in the root directory of the
zip
package. If you package and upload the entire folder as a zip
package, the function creation will fail because the entry point file cannot be found in the unzipped root directory. - Due to runtime environment differences, confirm that the installed dependency version is adapted to the function runtime environment.
- The function runtime environment is CentOS 7, and you need to install the dependencies in the same environment. If not, an error where the dependencies cannot be found may occur while running the function after upload.
- For dependencies requiring dynamic link library, you need to manually copy these dependencies to the installation directory, and then compress and upload them. You can install dependency with Docker or online IDE.
This document takes installing the
numpy
library as an example:
- Run the
mkdir test-package
command on your local computer to create a directory to store the function code and the dependent library. - Run the following commands to install the
numpy
dependent library in the directory.
cd test-package
pip install numpy -t .
- Create the
index.py
entry point file of the function in the directory and import the numpy
library in codes.
import json
import numpy
def main_handler(event, context):
print("Received event: " + json.dumps(event, indent = 2))
print("Received context: " + str(context))
print("Hello world")
return("Hello World")
- Compress the function code and dependent library to a zip package. Upload the zip package and create a function via the SCF console.
- Log in to the SCF console and click Functions on the left sidebar.
- Choose a region at the top of the Functions page and click Create.
- Enter the basic information of the function on the Create page.

- Creation method: Select From scratch.
- Runtime environment: Select Python 3.6.
- Submitting method: Choose Local ZIP file.
- Click Complete.
The SCF online editor Serverless Web IDE supports Terminal capability that has a built-in pip
package management tool.
Note
Serverless Web IDE has a delay in supporting newer versions of runtime environments. If the console does not open up the Serverless Web IDE for the corresponding runtime environment, package and upload the dependency library together with the code for dependency installation or install the dependency online.
This document takes installing the
numpy
library in the terminal as an example:
- Log in to the SCF console and click Functions on the left sidebar.
- In the function list, click a function name to enter the function details page.
- On the Function management page, select Function Code > Edit codes to view and edit the function.
- Select Terminal > New Terminal on the topbar of the IDE to open the terminal window.
- Run the following commands in the terminal to install the
numpy
dependent library.
cd src
pip install numpy -t .
- After the installation is complete, check the installed dependent library in the file tree on the left side of the IDE.
- Click Deploy to package and upload the dependent library with function codes to the cloud.
Note
- Use the
pip freeze > requirements.txt
command to generate a requirements.txt
file that contains all dependencies of the local environment. - Run the
pip install -r requirements.txt -t .
command in the IDE Terminal to install the dependency package according to the requirements.txt
configurations.
PHP runtime
Note:
The PHP versions supported by SCF are 5.6, 7.2, 7.4 and 8.0. Different minor versions of PHP may be incompatible. Please check the version number first before installing dependencies.
You can use a dependency manager (such as “composer”) to locally install the dependent library, package and upload it with the function code.
Note
Place the entry point file of the function in the root directory of the zip
package. If you package and upload the entire folder as a zip
package, the function creation will fail because the entry point file cannot be found in the unzipped root directory.
This document takes installing the
requests
library for PHP 7.2 as an example:
Run the mkdir test-package
command on your local computer to create a directory to store the function code and the dependent library.
Create Composer.json
under test-package
and specify the dependency library and version to be installed.
{
"require": {
"requests": ">=1.0"
}
}
Run the following command to install the requests
dependency library in this directory.
cd test-package
composer install
Create the function entry file index.php
in this directory and import the requests
library in the code.
<?php
require 'vendor/autoload.php';
function main_handler($event, $context) {
return "hello world";
}
?>
Compress the function code and dependent library to a zip package. Upload the zip package and create a function via the SCF console.
- Log in to the SCF console and click Functions on the left sidebar.
- Choose a region at the top of the Functions page and click Create.
- Enter the basic information of the function on the Create page.

- Creation method: Select From scratch.
- Runtime environment: Select Php7.2.
- Submitting method: Choose Local ZIP file.
- Click Complete.
Create the extension folder php_extension
in a directory at the same level as the function entry file, add the custom extension file .so
and configuration file php.ini
, and package and upload them together with the function code.
This document uses installing the custom extension swoole.so
for PHP 7.2 as an example.
- Run the
mkdir test-package
command on your local computer to create a directory to store the function code and the dependent library. - Run the following command to create the folder
php_extension
in test-package
and place the configuration file php.ini
and extension file .so
corresponding to the extension in this directory. The directory structure is as follows:
Note
- The extension folder
php_extension
and configuration file php.ini
are given fixed names. If other names are used, the extension may fail to load. - The extension folder
php_extension
, the configuration file php.ini
, and the custom extension file .so
need to have executable permissions.
|____php_extension
| |____php.ini
| |____swoole.so
|____index.php
- Custom extensions can be loaded from the code or layers. If an extension is uploaded as a layer, please make sure that the unzipped directory structure of the uploaded zip file is as follows:
|____php_extension
| |____swoole.so
php.ini
writing method:
- Create the function entry file
index.php
in this directory. Check whether the extension is loaded successfully through the extension_loaded( )
function, and if so, true
will be returned; otherwise, false
will be returned.
<?php
function main_handler($event, $context) {
var_dump(extension_loaded('swoole'));
return "hello world";
}
?>
- Compress the function code and dependent library to a zip package. Upload the zip package and create a function via the SCF console.
- Log in to the SCF console and click Functions on the left sidebar.
- Choose a region at the top of the Functions page and click Create.
- Enter the basic information of the function on the Create page.

- Creation method: Select From scratch.
- Runtime environment: Select Php7.2.
- Submitting method: Choose Local ZIP file.
- Click Complete.
Java runtime
You can use a dependency manager (such as maven) to locally install the dependent library, package and upload it with the function code.
Run the mkdir test-package
command on your local computer to create a directory to store the function code and the dependent library.
Create a `pom.xml file in the directory and configure dependency in the file.
Run the mvn package
command in the root directory of the project folder, and the compilation output should be as follows:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building java-example 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.785 s
[INFO] Finished at: 2017-08-25T10:53:54+08:00
[INFO] Final Memory: 17M/214M
[INFO] ------------------------------------------------------------------------
Compress the function code and dependent library to a jar package. Upload the jar package and create a function via the SCF console.
Log in to the SCF console and click Functions on the left sidebar.
Choose a region at the top of the Functions page and click Create.
Enter the basic information of the function on the Create page.

- Creation method: Select From scratch.
- Runtime environment: Choose Java8.
- Submitting method: Choose Local ZIP file.
Click Complete.
Go runtime
Instructions: upload the final binary file when packaging.
Compile the dependent library of the Go runtime with codes to generate a binary file. Upload the binary file and create a function via the SCF console.
- Log in to the SCF console and click Functions on the left sidebar.
- Choose a region at the top of the Functions page and click Create.
- Enter the basic information of the function on the Create page.

- Creation method: Select From scratch.
- Runtime environment: select Go 1.
- Submitting method: Choose Local ZIP file.
- Click Complete.
Was this page helpful?