All Products
Search
Document Center

Function Compute:Deploy a code package

更新時間:Mar 20, 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. In the mycode directory, create the index.js or index.mjs file.

    Sample code:

    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 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 Create a function.

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 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 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 desired function. In the upper-right corner of the function details page, click Upload Code to upload the ZIP package.

Important

Runtimes of Function Compute use 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 project

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 of the file:

    edition: 1.0.0          # The version of the YAML specification. The version complies with semantic versioning.
    name: fcDeployApp       # The name of the project.
    access: "default"  # The alias of the key.
    
    services:
      fc-deploy-test: # The name of the service.
        component: fc  # The name of the component.
        props: # The attribute value of the component.
          region: cn-shanghai
          service:
            name: fctest
            description: 'test'
            internetAccess: true
          function:
            name: emoji
            description: this is a emoji
            runtime: nodejs14
            codeUri: ./
            handler: index.handler
            memorySize: 128
            timeout: 6
  3. Add the package.json file.

    The following sample code provides an example of 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/{serviceName}/{functionName} directory.

  5. Run sudo s deploy to deploy the project.

    After the execution, you can deploy functions to Function Compute.

More information

You can also use layers of Function Compute to install dependencies. We recommend that you use an official common layer or build a dependency layer online. For more information, see the following topics: