@discordx/importer

中文文档
1.3.3发布于 2025/07/03Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 7764

A solution to import files on the module and commonjs at the same time

原始使用文档
文档目录6 节
  1. Resolve glob paths
  2. Import glob paths
  3. Module - CommonJS
  4. Module - ESNext
  5. Combine - CommonJS and ESNext
  6. Use relative path

Discord server NPM version NPM downloads Build status paypal

Create a discord bot with TypeScript and Decorators!

📖 Introduction

You can use this library without discordx

Support esm and cjs at the same time

💻 Installation

Version 16.6.0 or newer of Node.js is required

npm install @discordx/importer
yarn add @discordx/importer

Usage

If you use this code with esm or ejs, it will tell you about your environment.

import { isESM } from "@discordx/importer";

console.log(`isESM: ${isESM()}`);

Resolve glob paths

const resolvedPaths = await resolve(`${__dirname}/commands/**.js`);

console.log(resolvedPaths);

Import glob paths

Here is an example that could be used with the commonjs or esm modules

Module - CommonJS

importx(`${__dirname}/commands/**.js`).then(() =>
  console.log("All files imported"),
);

Module - ESNext

Remember: In esm, __dirname is not defined, so here is a workaround

import { dirname, importx } from "@discordx/importer";
const __dirname = dirname(import.meta.url);

importx(`${__dirname}/commands/**.js`).then(() =>
  console.log("All files imported"),
);

Combine - CommonJS and ESNext

If you are creating a module or extension of your own library, you can set it to auto-import paths based on the user module

import { dirname, importx, isESM } from "@discordx/importer";
const folder = isESM() ? dirname(import.meta.url) : __dirname;

importx(`${folder}/commands/**.js`).then(() =>
  console.log("All files imported"),
);

Use relative path

You can use relative path, which will be more convenient to write code by eliminating DIRNAME

import { importx } from "@discordx/importer";

// relative path start from root folder
importx("./tests/commands/**.js").then(() => console.log("All files imported"));

📜 Documentation

☎️ Need help?

💖 Thank you

You can support discordx by giving it a GitHub star.

版本与发布记录

@discordx/importer 共发布 27 个版本,首次发布于 2021/11/13;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.3latest2025/07/031.4 年前
1.3.22025/05/031.5 年前
1.3.12024/06/182 年前
1.3.02023/11/053 年前
1.2.32023/08/123 年前
1.2.22023/04/143 年前

关于 @discordx/importer 的常见问题

@discordx/importer 是什么?

A solution to import files on the module and commonjs at the same time

如何安装 @discordx/importer?

在项目目录运行 npm install @discordx/importer,或使用 pnpm add / yarn add / bun add。

@discordx/importer 支持 TypeScript 吗?

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

@discordx/importer 支持 ESM 还是 CommonJS?

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

@discordx/importer 可以商用吗?

@discordx/importer 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@discordx/importer 的下载量有多少?

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

@discordx/importer 还在维护吗?

最新版本 1.3.3 发布于 2025/07/03(1.4 年前);至今共发布 27 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/11/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。