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

yearn

v2.3.1

Published

Override node's require mechanism.

Downloads

161

Readme

yearn

NPM Version NPM License NPM Downloads

Dependencies Dev Dependencies

Build Status Coverage Status GitHub issues

Overview

yearn is a library that injects functionality into Node's require mechanism providing the ability to transform the require before it's passed to load. A default mechanism is injected so that all requires look for ./node_modules/module/version instead of node's default ./node_modules/module location. There is also support for orgs, which allows for a more central placement of all modules or separation of public and private modules (default org points to ./node_modules for backward compatibility). yearn also provides wrappers around node (ynode) and npm (ynpm) to automaticaly enforce yearn requiring.

Installation

Local installation:

$ npm install yearn

Global installation:

$ npm install yearn -g

Local Usage

To use yearn for a single project it needs to be installed locally. Due to the fact that it needs to be bootstrapped it must be installed in the traditional npm structure. All other modules may be installed in any way compatible with the yearn implementation.

Once installed locally require it and provide initialization options before using the new require mechanism.

Example initialization with a few options and then some requires:

var yearn = require( 'yearn' )({ 
	orgs: {
		test: '/usr/bin/test_modules'
		local: '/usr/bin/my_modules'
	},
	override: true
});

// Simple backward compatible yearning 
var log4js = yearn( 'log4js' );

// Explicit yearning for a semantic version with modern syntax
var nodeunit = yearn( { org: 'test', module: 'nodeunit', version: '0.9.x' } );

// Implicit require for a specific version with string syntax
var lodash = require( '[email protected]' );

// Explicit require without org or version (default with pull version and org from package.json) 
var loca_module = require( { module: 'my_local_module' } );

Global Usage

See ynode and ynpm below for the global scripts that are provided with yearn.

YEARN_CONFIG is an evironment variable that is read when ever ynode and ynpm are started up. This environment variable should point to a .json file containing the global yearn config. All the options for local yearn below are supported.

Options

var yearn = require( 'yearn' )({
	orgs: { '', './node_modules' },
	override: true,
	prompt: 'ynode> ',
	delimiters: {
		org: ':',
		semver: '@',
		file: '/'
	},
	aliases: [
	],
	npmconfig: {
	}
});
  • orgs: The orgs config object is a mapping of organization names to their locations. The default location ( '' ) will point to the module's local node_modules folder (./node_modules) unless overridden.

    Example:

      var yearn = require( 'yearn' )({
      	orgs: { 
      		'', '//usr/bin/default_node_modules_location' 
      		'other_loc': '//some/other/location'	
      	}
      });
    
      // require looking in '//usr/bin/default_node_modules_location/module1' for a version specified in the package.json for module1.
      var regular_module = require( 'module1' );  
    
      // require looking in '//some/other/location/module2' for a version specified in the package.json for module2.
      var other_modules = require( 'other:module2' );
  • override: This boolean specifies if yearn should override Node's require mechanism or should only return the yearn functionality. Additionaly if a function is provided than yearn will ignore all of it's default functionality and override require with that functionality instead.

    There are a few thing that one should know if your going to override the functionality yourself. Firstly this does not override the most outer form of require and thus you end up limited to two arguments getting passed to your function (what the user passes to require or resolve and the parent module of the call). The function you are infact overriding is module.constructor._resolveFilename so that your code works in both the require and require.resolve contexts. Yearn will also override some functionality in require so that it doesn't restrict passed arguments to strings. Secondly yearn does not load or compile the modules itself it is only overriding the resolve aspect, node's internal compiler and loader should take it from there. The result of your override functionality should be the full path to the module's file.

  • prompt: Set the prompt of the ynode REPL. Defaults to ynode>.

  • delimiters: The delimiters option is a way to set the delimiters between org, module, version and specific file parts of a string based require. By default these values are : between org and module, @ between module and version similarly to how semver does their versioning and / to state that the require is for a particular file in the module. These should not be the same delimiter (prior to version 0.2.0 of yearn they were all / and that lead to problems with pre existing modules like npm and grunt).

    • org: the separator between an org and module (defaults to :).
    • semver: the separator between a module and the desired semver to match (defaults to @).
    • file: the separator to determine the path to find the file within the found module (defaults to /).
  • aliases: This option allows for globally mapping whole orgs, specific modules or even specific versions of modules to other orgs, modules or versions. These aliases not only affect runtime but also are applied when installing new modules via ynpm.

    Example:

      var yearn = require( 'yearn' )({
      	orgs: { 
      		'', '//usr/bin/default_node_modules_location' 
      		'other_loc': '//some/other/location'	
      	},
      	aliases: [
      		{
      			from: { org: 'other_loc' },
      			to: { org: '' }
      		},{
      			from: { module: 'nodeunit', version: '<0.9.0' },
      			to: { module: 'nodeunit', version: '^0.9.1' }
      		}		
      	]
      });
  • npmconfig: This option only pertains to ynpm. This object can be populated with parameters that ynpm should initialize it's internal npm library with when it executes npm commands.

ynode

ynode is a small wrapper around node itself that will enforce a YEARN_CONFIG. Running with arguments will interpret the first as a script and perform a require of it. Executing ynode without any arguments will open a REPL with a YEARN_CONFIG already interpreted.

If one wants to force the use of ynode over node then change the globally installed scripts from ynode and ynode.cmd to node and node.cmd. Then make sure that the folder where the scripts appears before the folder where node is installed. Finally go into the altered ynode script and change the #! to point to the actual node instead of /usr/bin/env node. Then whenever node is called or a node script is run it will be run through the ynode script.

ynpm

ynpm is a small wrapper around some of npm's functionality. This script can be used to install modules into the org/module/version structure that yearn uses by default. There are only a few functionalities supported thus far:

  • ynpm check [orgs_or_specific_modules...]: Print the modules that need to be updated and to what version.

  • ynpm help: Print the ynpm usage information.

  • ynpm install [module]: Search for and list all available versions of the module.

  • ynpm list [args](IN DEVELOPMENT): Manage npm configuration.

  • ynpm orgs: Print the current orgs as specified by the YEARN_CONFIG.

  • ynpm version: Print the version of yearn currently associated with ynpm.