npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

autoload-modules

v1.0.5

Published

這是用來自動載入 node.js 的模組的模組。

Downloads

2

Readme

autoload-modules

這是用來自動載入 node.js 的模組的模組。

NPM

NPM

簡介

  • 使用 ES6 的 Proxy 來 Lazy Load 模組。
  • 提供預先指定模組載入
  • 可指定一個名稱轉換的 callback 函數來猜測模組的名稱

執行需求

  • node.js 版本 6.0.0 以上

安裝

npm install autoload-modules --save

範例

let $ = require('autoload-modules')({
  paths: module.paths,
  guess: function(name) {
    return 'gulp-' + name;
  },
  mapping: {
    promise: 'bluebird',
    through: require('through2')
  }
});

$.async = $.asyncawait.async;
$.await = $.asyncawait.await;
$.gulpUtil.log('Start');

功能

autoload-modules 會根據一個名稱陣列來猜測模組名稱並將其自動載入。

會進行猜測的名稱最多有四個,依序為:mapping 名稱、屬性名稱、預設的轉換名稱、guessName 提供的轉換名稱。

  • mapping 名稱:請參考參數的 mapping 項目的說明。

  • 屬性名稱:以範例來說,就是 asyncawait,實際上會載入 asyncawait 模組。

  • 預設的轉換名稱:假設屬性名稱為一個駝峰式名稱,並將其轉換為小寫字母與連字號組合成的名稱。

    以範例來說,就是 gulpUtil,實際上會載入 gulp-util 模組。

  • guess 提供的轉換名稱:請參考參數的 guess 項目的說明。

參數

require('autoload-modules')(options) 會傳回一個 Proxy 的物件實體。

options 是非必填參數,目前提供的選項有以下幾個:

  • paths: 如果有某個模組在安裝時會有多個版本,可傳入一個絕對路徑目錄的陣列,程式會逐一嘗試載入。

    使用上指定 module.paths 即可,為避免載入錯誤的版本,強烈建議一定要指定此參數。

  • guess: 一個字串轉換函數,輸入一個屬性名稱,傳回一個 require 模組的名稱。

  • mapping: 一個名稱對應的 Key-Value 物件。

    如果 Value 是一個字串,則會被當作第一順位的猜測名稱。

    如果 Value 是一個函數或物件,則會被直接指定為 Proxy 的屬性 (屬性名稱是 Key)

    如果 Value 不是字串、函數或物件,則會直接忽略。

備註

如果在嘗試載入模組的過程中,發生了不是模組找不到 (MODULE_NOT_FOUND) 以外的錯誤,將直接拋出例外。