All Products
Search
Document Center

Function Compute:Deploy a code package

Last Updated:Jul 18, 2024

This topic describes how to install third-party dependencies for your Node.js code, package code, and deploy code to Function Compute. In this topic, a third-party emoji dependency library is used as an example.

Preparations

  1. Create a code directory for tests and specify a name for the directory. In this example, mycode is used.

    • Linux and macOS

      Run the mkdir -p /tmp/mycode command to create the directory.

    • Windows

      Create a folder and name it mycode.

  2. Create the index.mjs or index.js file in the mycode directory.

    The following code shows an example:

    ECMAScript modules

    Note

    This example supports only Node.js 18 or later.

    // index.mjs
    'use strict';
    import * as emoji from 'node-emoji'
    
    export const handler = async (event, context) => {
      console.log('hello world');
      return emoji.get(':unicorn:');
    }
    

    CommonJS modules

    // index.js
    'use strict';
    var emoji = require('node-emoji')
    
    exports.handler = (event, context, callback) => {
      console.log('hello world');
      callback(null, emoji.get(':unicorn:'));
    }
    

Use npm to install dependencies and deploy the code

Prerequisites

Procedure

  1. Run the npm install node-emoji command in the mycode directory to install the emoji dependency library to the current directory.

  2. Package all files in the mycode directory.

    • Linux and macOS

      Go to the mycode directory and run zip code.zip -r ./*.

      Note

      Make sure that you have the read and write permissions on the directory.

    • Windows

      Go to the mycode directory, select all files, right-click the files, and then compress the files into a .zip file.

    Note

    Make sure that the index.js file that you created is located in the root directory of the package.

  3. In the Function Compute console, find the function that you want to manage. In the upper-right corner of the function details page, click Upload Code and upload the ZIP package that you prepared.

    After the code is uploaded, you can click Test Function on the Code tab to check whether the code is correct.

Important

Function Compute runs in a Linux environment. If a binary file is included when you install the emoji dependency library on a Windows or macOS device, the code package fails to run.Function Compute Therefore, we recommend that you use WebIDE to package third-party dependencies or use Serverless Devs to install dependencies and deploy the project. For more information, see Use WebIDE to package third-party dependencies of a function and Use Serverless Devs to install dependencies and deploy the project.

Use Serverless Devs to install dependencies and deploy the code

Prerequisites

Procedure

  1. Run the cd /tmp/mycode command to go to the mycode directory.

  2. Create the s.yaml file.

    The following sample code provides an example on how to edit the file:

    edition: 3.0.0
    name: fcDeployApp
    access: "default"
    
    vars: # The global variables.
      region: "cn-hangzhou"
    
    resources:
      hello_world:
        component: fc3 # The name of the component.
        props:
          region: ${vars.region}              # For information about how to use variables, visit: https://docs.serverless-devs.com/serverless-devs/yaml#%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC.
          functionName: "emoji"
          description: 'this is emoji'
          runtime: "nodejs18"
          code: ./
          handler: index.handler
          memorySize: 128
          timeout: 30
  3. Add the package.json file.

    The following sample code provides an example on how to edit the file:

    {
      "dependencies": {
        "node-emoji": "^1.11.0"
      }
    }
  4. Run sudo s build --use-docker to install dependencies.

    After the execution is complete, the .s directory is generated in the mycode directory, and the dependencies are installed in the .s/build/artifacts/{functionName} directory.

  5. Run sudo s deploy to deploy the project.

    After the execution, you can deploy your function to Function Compute.

More information

If your code package is too large, you can separate the dependencies, build a layer, and upload only business code. For more information, see the following topics: