Dependencies for Node.js functions

This section describes how to create dependencies for Node.js functions in FunctionGraph.

Example

Before creating a dependency, ensure that Node.js matching the function runtime has been installed in the environment.

The following uses Node.js 20.15 as an example to describe how to create a MySQL dependency package.

  1. Create a directory for your function and navigate to it.

    # Create a directory for your function
    mkdir mysql-dependency
    cd mysql-dependency
    
  2. Create a package.json file and add dependencies.

    {
      "name": "mysql-dependency",
      "version": "1.0.0",
      "description": "Package for mysql dependency",
      "main": "",
      "scripts": {
        "prepack": "npm install",    
        "postpack": "export DEPENDENCY_NAME=${npm_package_name}-${npm_package_version} && rm -f ${DEPENDENCY_NAME}.zip  && tar -tf ${DEPENDENCY_NAME}.tgz | sed 's/^package\\///' | zip -@r ${DEPENDENCY_NAME}.zip; rm ${DEPENDENCY_NAME}.tgz",
        "zip": "zip -r ${npm_package_name}-${npm_package_version}.zip ./node_modules ./package.json ./README.md",
        "zip:ps1": "powershell -Command Compress-Archive ./node_modules/, ./package.json, ./README.md -Force -DestinationPath %npm_package_name%-%npm_package_version%.zip"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "type": "commonjs",
      "dependencies": {
        "mysql": "^2.18.1"
      },
      "bundleDependencies": [
        "mysql"
      ],
      "files": []
    }
    
  3. Install the dependencies and create zip.

npm pack is a command to create a tarball of your package.

As FunctionGraph supports zip files only, the package.json provides following scripts to install dependencies and convert the tarball to a zip file (see: How npm handles the “scripts” file).

  • prepack script to install the dependencies and

  • postpack script to convert the tarball created by npm pack to a zip.

npm pack

This will create a zip file named ${npm_package_name}-${npm_package_version}.zip (in our cas: mysql-dependency-1.0.0.zip) in the current directory, containing the dependencies specified in the package.json file.

For more details, see Configuring Dependency Packages in the FunctionGraph user manual.

Native module dependencies

Note

This section is in draft state and might not be fully accurate or complete.

Update will follow.

If your Node.js function depends on native modules that require .dll or .so files, you can include these files in your dependency package.

You are advised to create function dependencies in EulerOS. If other OSs are used, an error may occur due to underlying dependent libraries. For example, the dynamic link library cannot be found.

Ensure that the native modules are compatible with the runtime environment of your function.

When packaging your dependencies, make sure to include the .dll or .so files in the appropriate directory structure within your zip file.

Setting Up the EulerOS Environment

EulerOS is an enterprise-grade Linux OS based on open-source technology. It features high security, scalability, and performance, meeting customers’ requirements for IT infrastructure and cloud computing services.

  1. Buy a EulerOS ECS on T Cloud Public by referring to Creating and Logging In to a Linux ECS. On the Configure Basic Settings page, select Public Image, and select EulerOS and an image version.

  2. Download the EulerOS image, and use virtualization software to set up the EulerOS VM on a local PC. EulerOS images

  3. Use openEuler Docker images to set up a Docker environment on your local PC.