@panosoft/require-string
v0.1.5
Published
Require a module from a string in memory instead of loading it from the file system.
Downloads
16
Readme
require-string
Require a module from a string in memory instead of loading it from the file system.
Installation
npm install @panosoft/require-string
Usage
var requireString = require('@panosoft/require-string');
var module = requireString('module.exports = {number: 1}');
console.log(module.number); // 1
API
requireString ( content [, filename] )
Works just like Node's require()
except it loads a module from a string instead of reading it from the file system.
A filename
can be supplied which provides a base path from which relative require()
paths in the string module can be resolved.
Returns the string module's module.exports
object just like Node's require()
.
Arguments
content
- The string to load as a module.filename
- The filename to apply to the module. This is used in resolving relative paths passed torequire()
within the string module. Defaults to''
which results in relativerequire()
paths being resolved relative to the current working directory of the parent Node process.
Example
Assuming we have the following file locally, path/to/relative/index.js
:
var path = require('path');
module.exports = path;
Then,
var requireString = require('@panosoft/require-string');
var filename = 'path/to/string/module.js';
var moduleString = ''
+ ' var relativeModule = require(\'../relative\');'
+ ' module.exports = relativeModule.resolve;';
var module = requireString(moduleString, filename);
console.log(module('.')); // current working directory