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
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.
Create the
index.mjs
orindex.js
file in the mycode directory.The following code shows an example:
ECMAScript modules
NoteThis 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
Run the
npm install node-emoji
command in themycode
directory to install the emoji dependency library to the current directory.Package all files in the
mycode
directory.Linux and macOS
Go to the
mycode
directory and runzip code.zip -r ./*
.NoteMake 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.
NoteMake sure that the
index.js
file that you created is located in the root directory of the package.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.
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
Run the
cd /tmp/mycode
command to go to themycode
directory.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
Add the
package.json
file.The following sample code provides an example on how to edit the file:
{ "dependencies": { "node-emoji": "^1.11.0" } }
Run
sudo s build --use-docker
to install dependencies.After the execution is complete, the
.s
directory is generated in themycode
directory, and the dependencies are installed in the.s/build/artifacts/{functionName}
directory.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: