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 🙏

© 2024 – Pkg Stats / Ryan Hefner

weare-npm-assets

v0.1.6

Published

Expose NPM modules assets folder.

Downloads

3

Readme

Repository https://npm.dev.studioweare.com

WE_ARE NPM Assets

Expose NPM modules assets folder.

Installation

$ npm install weare-npm-assets

Usage

Say we're in a app named module that have lodash as a dependency.

var express = require('express');
var app = express();
var npmAssets = require('weare-npm-assets')();

var lodashScriptPath = npmAssets.getFilePath(require, 'lodash');
// -> /app/modules/lodash/index.js
var myAppScriptPath = npmAssets.getFilePath(require, null, 'javascripts/app.js', 'src');
// -> /app/src/javascripts/app.js

npmAssets.expressServe(app);
// Will create 2 virtual paths:
//   "/app/modules/lodash" real path -> "/{root-of-the-app}/node_modules/lodash"
//   "/app/src"            real path -> "/{root-of-the-app}/src"

app.use('/', function (req, res) {
  res.send(
    '<script src="'+lodashScriptPath+'" />' +
    '<script src="'+myAppScriptPath+'" />'
  );
});

Debug

To see the virtual paths generated by "getFilePath" and the static paths (virtual/hard) that are created, you can setup the environment variable DEBUG to npm-assets when starting your app:

$ DEBUG=npm-assets node bin/www

Function references

Most useful:

Expert mode:

getFilePath(cRequire, dependency, file, relPath)

/**
 * Get the virtual path of a file contain in a module or one of his dependency.
 *
 * By passing the reference of the "require" function, the contextual dependency
 * will be processed by "require" to retrieve the right module folder.
 *
 * This will create a public static path that expose the entire module or one
 * of his subfolder.
 *
 * Ex: Module X require module A and B. Module A can be found at the same level
 * that module X but module B have a specific version so it's found in X/node_modules/B.
 *
 * @param Function cRequire
 *   The contextual "require" function.
 * @param String dependency
 *   (optionnal) The name of the main module dependency. When used, the final
 *   path of the file looks like this:
 *   /{main-module}/modules/{dependency}/{file}
 * @param String file
 *   (optionnal) The module relative file path. By default, this is the main
 *   module's file.
 * @param String relPath
 *   (optionnal) The relative folder path in which to get the file. Useful for
 *   not exposing the entire module folder, see this example:
 *     getFilePath(require, null, 'src/javascripts/app.js')
 *       Will expose the folder of the entire module (/).
 *     getFilePath(require, null, 'javascripts/app.js', 'src')
 *      Will only expose the "src" folder of the module (/src).
 *   For non-weird results, don't use if "file" is undefined.
 *
 * @return String
 *   The virtual path of the file.
 */

getFileHardPath(cRequire, dependency, file)

/**
 * Get the hardpath of a file contain in a module or one of his dependency.
 *
 * @param Function cRequire
 *   The contextual "require" function.
 * @param String dependency
 *   (optionnal) The name of the main module dependency. If it's undefined, the
 *   file will be search in the main module.
 * @param String file
 *   (optionnal) The module relative file path. By default, this is the main
 *   module's file.
 *
 * @return String
 *   The hardpath of the file.
 */

expressServe(app)

/**
 * Serve all the static path with Express.
 *
 * @param Function express
 *   Express reference.
 * @param Object app
 *   The Express application.
 */

addStaticPath (cRequire, dependency, relVirtualPath, relHardPath, mainRelPath)

/**
 * Add a static path for a module or one of it's dependency and return the
 * resulting virtual path.
 *
 * @param Function cRequire
 *   The main module contextual "require" function.
 * @param String dependency
 *   (optionnal) The name of the main module dependency.
 * @param String relVirtualPath
 *   (optionnal) A path that will be append to the main module/dependency
 *   virtual path folder. Useful to follow the same hierarchy of the module's
 *   folders when exposing only a specific folder with the "relHardPath" argument
 *   ("src" folder for example).
 * @param String relHardPath
 *   (optionnal) A path that will be append to the main module/dependency
 *   hard path folder. Useful to not expose the entire module folder
 *   (only "src" folder for example).
 * @param String mainRelPath
 *   (optionnal) The relative path of the main file of the module. Useful for
 *   NPM module that don't have main file. In that case, point to any file
 *   inside the module folder. Default: Will try to find the main file in
 *   "package.json" file, then default to "index.js".
 */

getMainFileRelPath(cRequire, dependency)

/**
 * Get the relative path of the main file of the main module/dependency.
 *
 * @param Function cRequire
 *   The main module contextual "require" function.
 * @param String dependency
 *   (optionnal) The name of the main module dependency.
 *
 * @return String
 *   Return the relative path of the main file of the module.
 */

getFolderPath(cRequire, dependency, mainRelPath)

/**
 * Get the absolute folder path of the main module/dependency.
 *
 * @param Function cRequire
 *   The main module contextual "require" function.
 * @param String dependency
 *   (optionnal) The name of the main module dependency.
 * @param String mainRelPath
 *   (optionnal) The relative path of the main file of the module. Useful for
 *   NPM module that don't have main file. In that case, point to any file
 *   inside the module folder. Default: Will try to find the main file in
 *   "package.json" file, then default to "index.js".
 *
 * @return String
 *   Return the absolute folder path of the module.
 */