This topic uses the installation of the third-party dependency emoji as an example to describe how to install dependencies for your Node.js code, package the code, and deploy it to Function Compute.
Preparations
Create a code directory for testing, such as
mycode
.Linux or macOS operating system
You can execute
mkdir -p /tmp/mycode
to create it.Windows operating system
Create a new folder in any location and name it
mycode
.
In the mycode directory, create a code file named
index.mjs
orindex.js
.The following sample code provides an example.
ES module
NoteThis example supports only Node.js 18 and 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 module
// 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 you have permissions to execute npm commands.
You have created a Node.js function in the Function Compute console. For more information, see Create an event function.
Procedure
In the
mycode
directory, executenpm install node-emoji
to install the emoji dependency library to the current directory.Package all files in the
mycode
directory.Linux or macOS operating system
Go to the
mycode
directory and executezip code.zip -r ./*
.NoteMake sure that you have the read and write permissions on the directory.
Windows operating system
Go to the
mycode
directory, select all files, right-click, and select to package them as a ZIP package.
NoteMake sure that the
index.js
file you created is located in the root directory of the package.In the Function Compute console, find the target function. Then, on the Function Details page, click the Code tab, click Upload Code in the upper-right corner to upload the ZIP package that you packaged in the previous step. After the upload is complete, click Test Function to verify the correctness of the code.
Because the runtime environment of Function Compute is a Linux operating system, if you install the emoji dependency library with binary files on a Windows or macOS operating system, your code package will fail to run after it is uploaded to Function Compute. Therefore, we recommend that you use WebIDE to package third-party dependencies for functions or use Serverless Devs to install dependencies and deploy the project.
Use Serverless Devs to install dependencies and deploy the project
Prerequisites
Procedure
Execute
cd /tmp/mycode
to go to themycode
directory.Add an
s.yaml
file.The following sample code provides an example of the file:
edition: 3.0.0 name: fcDeployApp access: "default" vars: # Global variables region: "cn-hangzhou" resources: hello_world: component: fc3 # The component name props: region: ${vars.region} # For more information about how to use variables, see: 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 a
package.json
file.The following sample code provides an example of how to edit the file:
{ "dependencies": { "node-emoji": "^1.11.0" } }
Execute
sudo s build --use-docker
to install dependencies.After the execution is complete, an
.s
directory is generated in themycode
directory, and the dependencies are installed in the.s/build/artifacts/{functionName}
directory.Execute
sudo s deploy
to deploy the project.After the execution is complete, the function is deployed to Function Compute.
Important notes
When you use the custom runtime Node.js language and the startup command executes a compiled executable file, directly modifying the source code in WebIDE will not take effect because the actual running file is still the old executable file. To make the code modifications take effect, you need to update the code locally, recompile and package it, upload the code package to WebIDE, and then re-execute the function.
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: