Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

load-module

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

load-module - npm Package Compare versions

Comparing version 0.2.5 to 1.0.0

98

index.js

@@ -1,77 +0,53 @@

'use strict'
const path = require('path')
const arrayify = require('array-back')
/**
* Typically used by cli app loading plugins.
*
* Like node's `require` but with a few extra features:
* - Seaches `node_modules` in the current working directory by default.
* - You can specify your folders in which to search for modules
* Node's `require` with a few extra features:
* - You can specify additional folders in which to search for modules
* - You can specify a module prefix
* @module load-module
* @example
* const loadModule = require('load-module')
* > const loadModule = require('load-module')
*
* > loadModule('react-dom')
*
* > loadModule('dom', { prefix: 'react-' })
*
* > loadModule('something.js', { paths: '.' })
*
* > loadModule('something.js', { paths: [ '.', '~/my-modules' ] })
*/
module.exports = loadModule
const attempted = []
/**
* @alias module:load-module
* @param {string} - module identifier
* @param {string} - The module name, directory or file to load.
* @param {object} [options]
* @param {string} [options.modulePrefix] - If the input `moduleID` is `rewrite` and the `module-prefix` is `lws`, load-module will attempt to laod `lws-rewrite` then `rewrite`.
* @param {string|string[]} [options.moduleDir] - An additional location to search for modules.
* @param {string} [options.prefix] - Also attempt to load the given module name with this prefix.
* @param {string|string[]} [options.paths] - One or more additional directories in which to search for modules.
*/
function loadModule (modulePath, options) {
options = Object.assign({ modulePrefix: '' }, options)
options.moduleDir = arrayify(options.moduleDir)
let result
function loadModule (request, options) {
if (typeof request !== 'string') {
throw new Error('request expected')
}
options = options || {}
const arrayify = require('array-back')
const prefix = options.prefix
const paths = options.paths ? arrayify(options.paths) : undefined
const origModulePaths = module.paths
if (paths && paths.length) {
module.paths = module.paths.concat(paths)
}
let output
/* Specific module directories were supplied */
if (options.moduleDir && options.moduleDir.length) {
for (const dir of arrayify(options.moduleDir)) {
try {
result = loadModule(path.resolve(dir, modulePath), { modulePrefix: options.modulePrefix })
break
} catch (err) {
attempted.push(err.attempted)
}
if (prefix) {
/* Try first with the prefix then without */
try {
output = require(require.resolve(`${options.prefix}${request}`, { paths }))
} catch (err) {
output = require(require.resolve(request, { paths }))
}
if (!result) {
return loadModule(modulePath, { modulePrefix: options.modulePrefix })
}
/* Search default module directories */
} else {
if (modulePath.startsWith('.')) modulePath = path.resolve(modulePath)
const pathsToTry = [
modulePath,
path.resolve(process.cwd(), modulePath),
path.resolve(process.cwd(), 'node_modules', modulePath)
]
if (options.modulePrefix) {
pathsToTry.push(options.modulePrefix + modulePath)
pathsToTry.push(path.resolve(path.dirname(modulePath), options.modulePrefix + path.basename(modulePath)))
pathsToTry.push(path.resolve(path.dirname(modulePath), 'node_modules', options.modulePrefix + path.basename(modulePath)))
}
for (const potentialPath of pathsToTry) {
try {
result = require(potentialPath)
break
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err
}
}
if (!result) {
let msg = `Module not found: ${modulePath}. Module paths attempted: `
msg += JSON.stringify(pathsToTry, null, ' ')
const err = new Error(msg)
err.attempted = pathsToTry
err.code = 'MODULE_NOT_FOUND'
throw err
}
output = require(require.resolve(request, { paths }))
}
return result
module.paths = origModulePaths
return output
}
{
"name": "load-module",
"author": "Lloyd Brookes <75pound@gmail.com>",
"version": "0.2.5",
"description": "Like node's require but with a few extra features",
"version": "1.0.0",
"description": "Node's `require` with a few extra features",
"repository": "https://github.com/75lb/load-module.git",

@@ -15,9 +15,14 @@ "license": "MIT",

"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},
"files": [
"index.js"
],
"scripts": {
"test": "test-runner test/*.js",
"docs": "jsdoc2md -t README.hbs index.js > README.md; echo"
"docs": "jsdoc2md -t README.hbs index.js > README.md; echo",
"cover": "nyc --reporter=text-lcov npm test | coveralls"
},
"devDependencies": {
"coveralls": "^3.0.2",
"jsdoc-to-markdown": "^4.0.1",

@@ -28,6 +33,3 @@ "test-runner": "^0.5.0"

"array-back": "^2.0.0"
},
"files": [
"index.js"
]
}
}
[![view on npm](https://img.shields.io/npm/v/load-module.svg)](https://www.npmjs.org/package/load-module)
[![npm module downloads](https://img.shields.io/npm/dt/load-module.svg)](https://www.npmjs.org/package/load-module)
[![Build Status](https://travis-ci.org/75lb/load-module.svg?branch=master)](https://travis-ci.org/75lb/load-module)
[![Coverage Status](https://coveralls.io/repos/github/75lb/load-module/badge.svg)](https://coveralls.io/github/75lb/load-module)
[![Dependency Status](https://david-dm.org/75lb/load-module.svg)](https://david-dm.org/75lb/load-module)

@@ -10,4 +11,4 @@ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)

## load-module
Like node's `require` but with a few extra features:
- You can specify your folders in which to search for modules
Node's `require` with a few extra features:
- You can specify additional folders in which to search for modules
- You can specify a module prefix

@@ -17,7 +18,15 @@

```js
const loadModule = require('load-module')
> const loadModule = require('load-module')
> loadModule('react-dom')
> loadModule('dom', { prefix: 'react-' })
> loadModule('something.js', { paths: '.' })
> loadModule('something.js', { paths: [ '.', '~/my-modules' ] })
```
<a name="exp_module_load-module--loadModule"></a>
### loadModule(modulePath, [options]) ⏏
### loadModule(request, [options]) ⏏
**Kind**: Exported function

@@ -27,6 +36,6 @@

| --- | --- | --- |
| modulePath | <code>string</code> | module identifier |
| request | <code>string</code> | The module name, directory or file to load. |
| [options] | <code>object</code> | |
| [options.modulePrefix] | <code>string</code> | If the input `moduleID` is `rewrite` and the `module-prefix` is `lws`, load-module will attempt to laod `lws-rewrite` then `rewrite`. |
| [options.moduleDir] | <code>string</code> \| <code>Array.&lt;string&gt;</code> | An additional location to search for modules. |
| [options.prefix] | <code>string</code> | Also attempt to load the given module name with this prefix. |
| [options.paths] | <code>string</code> \| <code>Array.&lt;string&gt;</code> | One or more additional directories in which to search for modules. |

@@ -33,0 +42,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc