All Products
Search
Document Center

Function Compute:Deploy a code package

Last Updated:Mar 28, 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 testing 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

  • npm is installed on your on-premises machine, and the permissions to execute npm commands are obtained.

  • A Node.js function is created in the Function Compute console. For more information, see the "Create a function" section of the Manage functions topic.

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 choose the option to compress all files into a ZIP package.

    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

Runtimes of Function Compute run with Linux OSs. If you installed the emoji dependency library on a Windows or macOS OS and the library contains binary files, your code fails to run after it is uploaded to 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

Before you start

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: # Global variables.
      region: "cn-hangzhou"
    
    resources:
      hello_world:
        component: fc3 # The name of the component.
        props:
          region: ${vars.region} # For more 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: