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 tests and specify a name for the directory. In this example,
mycodeis used.Linux and macOS
Run the
mkdir -p /tmp/mycodecommand to create the directory.Windows
Create a folder and name it
mycode.
Create the
index.mjsorindex.jsfile 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 Create an event function.
Procedure
Run the
npm install node-emojicommand in themycodedirectory to install the emoji dependency library to the current directory.Package all files in the
mycodedirectory.Linux and macOS
Go to the
mycodedirectory and runzip code.zip -r ./*.NoteMake sure that you have the read and write permissions on the directory.
Windows
Go to the
mycodedirectory, select all files, right-click the files, and then compress the files into a .zip file.
NoteMake sure that the
index.jsfile 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.
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
Serverless Devs and dependencies are installed. For more information, see Quick start.
Serverless Devs is configured. For more information, see Configure Serverless Devs.
Procedure
Run the
cd /tmp/mycodecommand to go to themycodedirectory.Create the
s.yamlfile.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: 30Add the
package.jsonfile.The following sample code provides an example on how to edit the file:
{ "dependencies": { "node-emoji": "^1.11.0" } }Run
sudo s build --use-dockerto install dependencies.After the execution is complete, the
.sdirectory is generated in themycodedirectory, and the dependencies are installed in the.s/build/artifacts/{functionName}directory.Run
sudo s deployto deploy the project.After the execution, you can deploy your function to Function Compute.
Important note
When using a custom runtime for Node.js and specifying a compiled executable as the startup command, modifying the source code directly in WebIDE will not take effect, as the function will continue to run the previously compiled executable. To apply code changes, you must update the source code locally, recompile and package the executable, upload the updated package to WebIDE, and then re-run 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: