
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
<<<<<<< HEAD Node.js methods to help in getting paths relative to current script and in requiring relative modules
Some simple methods to require and export Node.js modules from a 'lib' subdirectory.
Using '.' when requiring relative paths does not always work correctly when running a node.js script from a cwd other than the one the original script is in, e.g. if running npm test in the project root, require statements with '.' in the paths will fail in Windows. This being the case, I wrote my code to require modules something like
b3b6d9bcebd0e7d6755b00ed998270531e6fa2ba
var path = require('path');
var lib1 = require(path.join(__dirname, 'lib', 'lib1');
As I began to use a standard structure for Node.js projects that had the main entrypoint consisting only of lines of code to require and exporting library modules, where the real code resides, e.g. suppose you have a directory structure like
b3b6d9bcebd0e7d6755b00ed998270531e6fa2ba
index.js
/lib
- lib1.js
- lib2.js
- lib3.js
<<<<<<< HEAD suppose you have a directory structure like
/lib - index.js - foo.js - bar.js
and assuming this module is required like so
var h = require('helpers')(__dirname, exports);
then the following method examples would apply
require relative modules
var lib = h.require('lib'),
foo = h.require('lib/foo'),
bar = h.require('lib/bar');
export a module by name relative to the current module's directory
normally the code for this might be something like
exports.lib = require(__dirname + '/lib');
exports.libFoo = require(__dirname + '/lib/foo');
exports.libBar = require(__dirname + '/lib/bar');
// etc ad nauseum...
using the helpers exports method reduces this to
h.exports('lib')
h.exports('lib/lib1')
h.exports('lib/lib2')
The above examples are equivalent. Dash, slash, space, and '.' separated file-names will be converted to camelCase, e.g.
h.exports('foo/bar/buz')
will result in the buz module being exported as a property with identifier 'fooBarBuz' from the current module
extends the properties of a module's exports object with the exported properties of a module at a relative path to the current script
h.imports('lib')
if the lib module had two exported properties 'foo' & 'bar', the current module will now be exporting those properties in addition to any existing and later exports, if any.
####tests More detailed examples can be seen in the test/test.js file. If you wish to actually run the tests, cd into the dir node-helpers is installed in. If you do not already have mocha, chai, and should.js installed globally, you can install them by typing
npm install
although for mocha, at least, it is recommended that it be installed globally, so you can simply type
mocha
my index.js file began to always look something like
var path = require('path');
exports.lib1 = require(path.join(__dirname, 'lib', 'lib1');
exports.lib2 = require(path.join(__dirname, 'lib', 'lib2');
exports.lib3 = require(path.join(__dirname, 'lib', 'lib3');
// etc. ad nauseum
In my continuing effort to speed up coding I wrote this little module so that my index.js files now look like
var h = require('helpers')(__dirname, exports);
h.exp('lib1');
h.exp('lib2');
h.exp('lib3');
Its faster for me to type, quicker for me to scan, and works (even on Windows). There are two other usage methods as well - sub and mixin, which can be used as follows
// to simply require a module in relative 'lib' subdir
var lib1 = h.sub('lib1');
// to add every exported member of a submodule in lib to the current script's export object
h.mixin('lib1');
The above, or various combinations thereof, gives me enough flexibility to very quickly write my index.js entry-point for a Node.js code library in every scenario I've run into so far.
tl;dr - it saves typing and just works
npm install helpers
b3b6d9bcebd0e7d6755b00ed998270531e6fa2ba
see LICENSE.txt for more info
FAQs
helper functions for Node.js
The npm package helpers receives a total of 993 weekly downloads. As such, helpers popularity was classified as not popular.
We found that helpers 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
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.