Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
local-require
Advanced tools
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.
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
.
npm install local-require
npm test
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
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.
please contribute, You can contribute by either creating issue
or just send pull request for feature
you want to include in module.
(The MIT License)
FAQs
access local module using require as of they are core lib
The npm package local-require receives a total of 0 weekly downloads. As such, local-require popularity was classified as not popular.
We found that local-require demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.