@cloudflare/wrangler

中文文档
1.21.0发布于 2023/02/22MIT OR Apache-2.0CommonJS近 30 天下载 8860

Command-line interface for all things Cloudflare Workers

此软件包已被标记为弃用

This package is for Wrangler v1.x and is no longer supported. Please visit https://www.npmjs.com/package/wrangler for Wrangler v2+.

原始使用文档
文档目录17 节
  1. Installation
  2. Install with npm
  3. Install with cargo
  4. Install on Windows
  5. Updating
  6. Getting Started
  7. 🎙️ Top Level Commands
  8. 👯 generate
  9. 📥 init
  10. 🦀⚙️ build
  11. 🔓 login
  12. 🔧 config
  13. ☁️ 🆙 publish
  14. 🗂 kv
  15. 👂 dev
  16. Additional Documentation
  17. ✨Workers Sites

🤠 wrangler

Note: If you're looking for the latest version of the wrangler package, visit https://github.com/cloudflare/workers-sdk

Banner

crates.io   npm   GitHub Actions - Test Status   GitHub Actions - Linter Status  

wrangler is a CLI tool designed for folks who are interested in using Cloudflare Workers.

Wrangler Demo

Installation

You have many options to install wrangler!

For the latest version, see https://github.com/cloudflare/workers-sdk

Install with npm

We strongly recommend you install npm with a Node version manager like nvm, which puts the global node_modules in your home directory to eliminate permissions issues with npm install -g. Distribution-packaged npm installs often use /usr/lib/node_modules (which is root) for globally installed npm packages, and running npm install -g as root prevents wrangler from installing properly.

Once you've installed nvm and configured your system to use the nvm managed node install, run:

npm i @cloudflare/wrangler -g

If you are running an ARM based system (eg Raspberry Pi, Pinebook) you'll need to use the cargo installation method listed below to build wrangler from source.

Specify binary location

In case you need wrangler's npm installer to place the binary in a non-default location (such as when using wrangler in CI), you can use the following configuration options to specify an install location:

  • Environment variable: WRANGLER_INSTALL_PATH
  • NPM configuration: wrangler_install_path

Specify binary site URL

In case you need to store/mirror binaries on premise you will need to specify where wrangler should search for them by providing any of the following:

  • Environment variable: WRANGLER_BINARY_HOST
  • NPM configuration: wrangler_binary_host

Install with cargo

cargo install wrangler

If you don't have cargo or npm installed, you will need to follow these additional instructions.

Install on Windows

perl is an external dependency of crate openssl-sys. If installing wrangler with cargo, you will need to have perl installed. We've tested with Strawberry Perl. If you instead install perl via scoop, you may need to also run scoop install openssl in order to get the necessary openssl dlls. Installing wrangler with npm instead of cargo is another option if you don't want to install perl.

Updating

For information regarding updating Wrangler, click here.

Getting Started

Once you have installed Wrangler, spinning up and deploying your first Worker is easy!

$ wrangler generate my-worker
$ cd my-worker
# update your wrangler.toml with your Cloudflare Account ID
$ wrangler config
$ wrangler publish

🎙️ Top Level Commands

👯 generate

Scaffold a project, including boilerplate code for a Rust library and a Cloudflare Worker.

wrangler generate <name> <template> --type=["webpack", "javascript", "rust"]

All of the arguments and flags to this command are optional:

📥 init

Creates a skeleton wrangler.toml in an existing directory. This can be used as an alternative to generate if you prefer to clone a repository yourself.

wrangler init <name> --type=["webpack", "javascript", "rust"]

All of the arguments and flags to this command are optional:

  • name: defaults to the name of your working directory
  • type: defaults to "webpack".

🦀⚙️ build

Build your project. This command looks at your wrangler.toml file and runs the build steps associated with the "type" declared there.

Additionally, you can configure different environments.

🔓 login

Authorize Wrangler with your Cloudflare login. This will prompt you with a Cloudflare account login page and a permissions consent page. This command is the alternative to wrangler config and it uses OAuth tokens.

wrangler login --scopes-list --scopes <scopes>

All of the arguments and flags to this command are optional:

  • scopes-list: list all the available OAuth scopes with descriptions.
  • scopes: allows to choose your set of OAuth scopes.

Read more about this command in Wrangler Login Documentation.

🔧 config

Authenticate Wrangler with a Cloudflare API Token. This is an interactive command that will prompt you for your API token:

wrangler config
Enter API token:
superlongapitoken

You can also provide your email and global API key (this is not recommended for security reasons):

wrangler config --api-key
Enter email:
[email protected]
Enter global API key:
superlongapikey

You can also use environment variables to configure these values.

☁️ 🆙 publish

Publish your Worker to Cloudflare. Several keys in your wrangler.toml determine whether you are publishing to a workers.dev subdomain or your own registered domain, proxied through Cloudflare.

Additionally, you can configure different environments.

You can also use environment variables to handle authentication when you publish a Worker.

# e.g.
CF_API_TOKEN=superlongtoken wrangler publish
# where
# $CF_API_TOKEN -> your Cloudflare API token

CF_API_KEY=superlongapikey [email protected] wrangler publish
# where
# $CF_API_KEY -> your Cloudflare API key
# $CF_EMAIL -> your Cloudflare account email

🗂 kv

Interact with your Workers KV store. This is actually a whole suite of subcommands. Read more about in Wrangler KV Documentation.

👂 dev

wrangler dev works very similarly to wrangler preview except that instead of opening your browser to preview your worker, it will start a server on localhost that will execute your worker on incoming HTTP requests. From there you can use cURL, Postman, your browser, or any other HTTP client to test the behavior of your worker before publishing it.

You should run wrangler dev from your worker directory, and if your worker makes any requests to a backend, you should specify the host with --host example.com.

From here you should be able to send HTTP requests to localhost:8787 along with any headers and paths, and your worker should execute as expected. Additionally, you should see console.log messages and exceptions appearing in your terminal.

👂 Listening on http://localhost:8787
[2020-02-18 19:37:08] GET example.com/ HTTP/1.1 200 OK

All of the arguments and flags to this command are optional:

  • env: environment to build
  • host: domain to test behind your worker. defaults to example.com
  • ip: ip to listen on. defaults to localhost
  • port: port to listen on. defaults to 8787

Additional Documentation

All information regarding wrangler or Cloudflare Workers is located in the Cloudflare Workers Developer Docs. This includes:

  • Using wrangler commands
  • Wrangler configuration
  • General documentation surrounding Workers development
  • All wrangler features such as Workers Sites and KV

✨Workers Sites

To learn about deploying static assets using wrangler, see the Workers Sites Quickstart.

版本与发布记录

@cloudflare/wrangler 共发布 79 个版本,首次发布于 2019/06/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.21.0latest2023/02/223 年前
1.20.02022/10/044 年前
1.19.132022/09/154 年前
1.19.122022/04/214 年前
1.19.112022/03/224 年前
1.19.102022/03/224 年前

其他发布通道:beta → 1.19.3-rc.1。

关于 @cloudflare/wrangler 的常见问题

@cloudflare/wrangler 是什么?

Command-line interface for all things Cloudflare Workers注意:该包已被作者标记为弃用——This package is for Wrangler v1.x and is no longer supported. Please visit https://www.npmjs.com/package/wrangler for Wrangler v2+.

如何安装 @cloudflare/wrangler?

在项目目录运行 npm install @cloudflare/wrangler,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 wrangler、wrangler1,可以用 npx wrangler 直接运行。

@cloudflare/wrangler 支持 TypeScript 吗?

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

@cloudflare/wrangler 支持 ESM 还是 CommonJS?

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

@cloudflare/wrangler 可以商用吗?

@cloudflare/wrangler 使用 MIT OR Apache-2.0 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cloudflare/wrangler 的下载量有多少?

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

@cloudflare/wrangler 还在维护吗?

最新版本 1.21.0 发布于 2023/02/22(3 年前);至今共发布 79 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/06/02。作者已将其标记为弃用,新项目建议寻找替代方案。