@elementor/utils

中文文档
4.4.1发布于 2026/11/16GPL-3.0-or-laterTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2.1万

This package contains utility functions that are being used across the Elementor packages

原始使用文档
文档目录3 节
  1. Errors
  2. createError
  3. ensureError

Utils

This package contains utility functions that are being used across the Elementor packages.

Errors

This module contains a standard API for creating custom error classes in Elementor packages.

createError

To ensure consistency across the Elementor packages, we recommend creating a new error class that extends the ElementorError class using the createError function that does it for you.

Creating a new error type

For convenience, we've exposed a createError utility function that will create a custom error type for you. This function will force your error code and message to be static (per our guidelines), and will shorten your code a little bit:

// errors.ts
import { createError } from '@elementor/utils';

export const CannotRender = createError<{ id: string; }>( 'cannot-render', 'Cannot render element' );
export const CannotSave = createError<{ id: number; status: string; }>( 'cannot-save', 'Cannot save document' );
export const ElementNotFound = createError<{ id: string; }>( 'element-not-found', 'Element not found' );

Creating a new error type manually

If you prefer to create a new error type manually, you can extend the ElementorError class.

Inside your class, you'll need to define a unique error code and message that should be passed to the parent constructor.

We recommend having static string values for both the code and the message, so it'll then be easier to filter them in the error-tracking tools (e.g. message = 'cannot render element'; rather than message = 'Cannot render element' + id;). If you need to pass more information about the error, attach it to the context.

import { ElementorError, type ElementorErrorOptions } from '@elementor/utils';

type CannotRenderContext = {
    id: string;
};

type CannotRenderOptions = {
    context: CannotRenderContext;
    cause?: ElementorErrorOptions['cause'];
};

class CannotRender extends ElementorError {
    constructor( { context, cause }: CannotRenderOptions ) {
        const code = 'cannot-render';
        const message = 'Cannot render element';

        super( message, { cause, context, code } );
    }
}

Throwing your custom error type

After creating your custom error type (as described above), you can throw it in your code:

Basic example:

export const ElementNotFound = createError<{ id: string; }>( 'element-not-found', 'Element not found' );

function renderElement( id: string ): string {
    const element = findElementById( id );

    if ( ! element ) {
        throw new ElementNotFound({ context: { id } });
    }

    return element.render();
}

Example with a cause:

export const CannotSave = createError<{ id: number; status: string; }>( 'cannot-save', 'Cannot save document' );

try {
     thirdPartyService.save( id );
} catch ( error ) {
    throw new CannotSave({ context: { id, status: error.status }, cause: error });
}

ensureError

The ensureError function is a utility function that ensures that the given value is an error. If the value is already an error, it will be returned as is. If the value is not an error, it will be wrapped with an Error that includes the thrown value.

import { ensureError } from '@elementor/utils';

const CannotUpdate = createError<{ id: number; }>( 'cannot-update', 'Cannot update document' );

try {
    thirdPartyService.update( id );
} catch ( error ) {
    const errorInstance = ensureError( error );

    throw new CannotUpdate({ context: { id }, cause: errorInstance });
}

版本与发布记录

@elementor/utils 共发布 1,105 个版本,首次发布于 2024/07/24;过去 12 个月发布了 858 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.4.1latest2026/11/16今天
4.5.0-1192build2026/11/16今天
4.5.0-11912026/11/16今天
4.5.0-11902026/11/16今天
4.5.0-11892026/11/16今天
4.5.0-11882026/11/151 天前

其他发布通道:manual → 4.1.0-manual,4.2-beta1 → 4.2.0-beta1,beta → 4.4.0-beta3,build → 4.5.0-1192。

关于 @elementor/utils 的常见问题

@elementor/utils 是什么?

This package contains utility functions that are being used across the Elementor packages

如何安装 @elementor/utils?

在项目目录运行 npm install @elementor/utils,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.3.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@elementor/utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@elementor/utils 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@elementor/utils 可以商用吗?

@elementor/utils 使用 GPL-3.0-or-later 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@elementor/utils 的下载量有多少?

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

@elementor/utils 还在维护吗?

最新版本 4.4.1 发布于 2026/11/16(今天);至今共发布 1105 个版本,其中过去 12 个月发布了 858 个;首次发布于 2024/07/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。