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

local-require

v0.1.0

Published

access local module using require as of they are core lib

Downloads

4

Readme

local-require Build Status npm

NPM

Problem

Working in large node.js project usually contain complex and nested directory structure. Directory structure helps in organizing your code but it bring many drawback with that. This module solve one of the drawback, To understand problem lets consider following directory structure

projecct_name
|
|-foo
|  |-foo1
|  |   |-f1.js
|  |-f.js
|     
|-bar
|  |-bar1
|  |  |-b1.js
|  |-b.js
|
|_server.js

many time we endup requiring f.js and f1.js in b1.js (may be it is result of bad programming but this is not point). Given nature of node.js we will be doing somethign like require('../../foo/foo1/f1.js'). Problem with this is require statement is solely depend on directory structure if position of f1.js changes then above require method wont work. and if we are requiring f1.js from various part of code then we have to give relative path from module. which might be painful.

Solution

My solution is simple it uses awesomeness of require. node.js find modules in particular fashion algorithm is explain in doc. Given the nature of algorithm any thing in node_modules folder can access without relative path. So at start of your server (usually in server.js) just create symboliclink to your local files in node_module folder then you can access these local file without relative path from anywhere in codebase. But all symbolic link in node_modules folder wont look nice and it will hard to maintain. So why not create directory for these symbolicslink of local files and access them by directory_name/file_name.

Installation

npm install local-require

Test

npm test

Api

local-require module provides nonblocking api.

var lrequire = require('local-require');
lrequire.register(opts, function (err) {
  if (err) {
      //problem in creating symlink
  } else {
      //ready to use awesomeness of local-require
  }
});

Or you can also use blocking api

var lrequire = require('local-require')
lrequire.registerSync(opts);

as any nice blocking api this will throws an exception if any thing went wrong.

where opts is

{
  namespace: 'demo',     //required, directory in which you want to add all symlink
  basedir  : __direname  //optional, directory from which function called default : process.cwd()
  config   : {             
     'f1' : './foo/foo1/f1/js',
     'bar1': './bar/bar1' 
  }                     //requied , it is an object in which name and location mapping done 
}

So you can add f1.js from any part of codebase by require('demo/f1'). We can also use this method to include directory by require('demo/bar1')

Use of namespace here is just to segregate symboliclink files which will lead to simple maintain.You can create any number of namespace just avoid conlicting name with previous namespace or installed package name

ToDo

  • if you have use same namespace more than once, last namespace options will override, which also means it will override any install package who has same name as your namespace we can handle these condition more civilized way.

Contribution

please contribute, You can contribute by either creating issue or just send pull request for feature you want to include in module.

License

(The MIT License)