# @alicloud/fc2 中文文档

规范地址：https://npmcn.com/package/%40alicloud/fc2/

版本：2\.6\.6
npm 元数据更新时间：2023-08-04T03:49:15.837Z
中文译文更新时间：2026-11-06T18:39:32.329Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40alicloud/fc2
原文对照：https://npmcn.com/package/%40alicloud/fc2/?lang=original
项目主页：https://github.com/aliyun/fc-nodejs-sdk#readme
源码仓库：https://github.com/aliyun/fc-nodejs-sdk

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：119 kB，9 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：7 个
- 首次发布：2017-12-19T07:30:01.740Z
- 最新版本发布：2023-08-04T03:49:15.700Z
- 近 30 天下载量：2465（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：28（近 12 个月 0 个）

## 中文简介

阿里云函数计算 API SDK2

## 安装

```sh
npm install @alicloud/fc2
```

也可以使用：`pnpm add @alicloud/fc2`、`yarn add @alicloud/fc2`、`bun add @alicloud/fc2`

## README

# fc-nodejs-sdk (英语)

[![NPM version](https://img.shields.io/npm/v/@alicloud/fc2.svg?style=flat-square)](https://npmjs.org/package/@alicloud/fc2) [![build status](https://img.shields.io/travis/aliyun/fc-nodejs-sdk/master.svg?style=flat-square)](https://travis-ci.org/aliyun/fc-nodejs-sdk.svg?branch=master) [![coverage](https://coveralls.io/repos/aliyun/fc-nodejs-sdk/badge.svg?branch=master&service=github)](https://coveralls.io/github/aliyun/fc-nodejs-sdk?branch=master)

联合国 [http://doxmate.cool/aliyun/fc-nodejs-sdk/api.html 页面存档备份,存于互联网档案馆](http://doxmate.cool/aliyun/fc-nodejs-sdk/api.html)

## 通知

我们建议使用fc2, fc与fc2的主要区别是:

用户返回的响应是以下对象.

```js
{
     'headers': headers,
     'data': data,
}

```

对于引用函数,数据是您代码返回的结果。 默认情况下,如果想要获得原始缓冲,数据由utf8解码,那么在引用函数时,只需在选项中设置 {rawBuf: true} .

实例见以下代码:, 对于其他类人鱼,数据为对象.

安装正式的 fc2 发布版本 :

```bash
npm install @alicloud/fc2 --save
```

## 安装旧版本

fc 版本在 1.x 分支中,您可以安装 fc 像这样使用 “ npm ”

```bash
npm install @alicloud/fc --save
```

## 许可证

[麻省理工学院](https://npmcn.com/package/%40alicloud/fc2/LICENSE)

## 实例

### 答应

```js
'use strict';

var FCClient = require('@alicloud/fc2');

var client = new FCClient('<account id>', {
  accessKeyID: '<access key id>',
  accessKeySecret: '<access key secret>',
  region: 'cn-shanghai',
  timeout: 10000 // Request timeout in milliseconds, default is 10s
});

var serviceName = '<service name>';
var funcName = '<function name>';

client.createService(serviceName).then(function(resp) {
  console.log('create service: %j', resp);
  return client.createFunction(serviceName, {
    functionName: funcName,
    handler: 'index.handler',
    memorySize: 128,
    runtime: 'nodejs4.4',
    code: {
      zipFile: fs.readFileSync('/tmp/index.zip', 'base64'),
    },
  });
}).then(function(resp) {
  console.log('create function: %j', resp);
  return client.invokeFunction(serviceName, funcName, 'event');
}).then(function(resp) {
  console.log('invoke function: %j', resp);
}).catch(function(err) {
  console.error(err);
});

```

### aync/awa (节点 qq 7.6)

```js
'use strict';

const FCClient = require('@alicloud/fc2');

var client = new FCClient('<account id>', {
  accessKeyID: '<access key id>',
  accessKeySecret: '<access key secret>',
  region: 'cn-shanghai',
});

var serviceName = '<service name>';
var funcName = '<function name>';

async function test () {
  try {
    var resp = await client.createService(serviceName);
    console.log('create service: %j', resp);

    resp = await client.createFunction(serviceName, {
      functionName: funcName,
      handler: 'counter.handler',
      memorySize: 128,
      runtime: 'nodejs4.4',
      initializer: 'counter.initializer',
      code: {
        zipFile: fs.readFileSync('/tmp/counter.zip', 'base64'),
      },
    });
    console.log('create function: %j', resp);

    // by default , resp is decoded by utf8
    resp = await client.invokeFunction(serviceName, funcName, null);
    console.log('invoke function: %j', resp);

    // respWithBuf is returned as buffer if isRawBuf is set to be true in opts
    respWithBuf = await client.invokeFunction(serviceName, funcName, null, {}, 'LATEST', {rawBuf:true});

    uResp = await client.updateFunction(serviceName, funcName, {
      description: 'updated function desc',
      initializationTimeout: 60,
    });
    console.log('update function: %j', resp);
  } catch (err) {
    console.error(err);
  }
}
test().then();
```

### 自定义信头

我们提供两种方式来定制请求信头.

一种方法是通过客户端构建器传递信头. 您应当将通过构建器的页眉视为默认自定义页眉, 因为所有请求都会使用此页眉 .

```js
var client = new FCClient('<account id>', {
  accessKeyID: '<access key id>',
  accessKeySecret: '<access key secret>',
  region: 'cn-shanghai',
  headers: {
    'x-fc-invocation-type': 'Async'
  }
});

await client.invokeFunction(serviceName, funcName, 'event');
```

另一种方法是通过函数的参数传递头. 需要通过特定函数的页眉时,请使用这种方式.

```js
await client.invokeFunction(serviceName, funcName, 'event', {
  'x-fc-invocation-type': 'Async'
});
```

在使用两种方式时,会合并标题. 但对于有相同密钥的头头,由函数参数提供的头头会比起由构造器提供的头头.

## API 光谱

见: [https://help.aliyun.com/document detail/52877.html (中文(简体) )](https://help.aliyun.com/document_detail/52877.html)

## 测试

```sh
ACCOUNT_ID=<ACCOUNT_ID> ACCESS_KEY_ID=<ACCESS_KEY_ID> ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET> make test
```

## 常见问题

### @alicloud/fc2 是什么？

阿里云函数计算 API SDK2

### 如何安装 @alicloud/fc2？

在项目目录运行 npm install @alicloud/fc2，或使用 pnpm add / yarn add / bun add。

### @alicloud/fc2 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/alicloud\_\_fc2 是否存在。

### @alicloud/fc2 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @alicloud/fc2 可以商用吗？

@alicloud/fc2 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @alicloud/fc2 的下载量有多少？

根据 npm 官方下载统计，@alicloud/fc2 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,465 次，日均约 82 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @alicloud/fc2 还在维护吗？

最新版本 2\.6\.6 发布于 2023/08/04（3 年前）；至今共发布 28 个版本，其中过去 12 个月发布了 0 个；首次发布于 2017/12/19。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

