0.1.5 • Published 8 years ago

@panosoft/require-string v0.1.5

Weekly downloads
6
License
MIT
Repository
github
Last release
8 years ago

require-string

Require a module from a string in memory instead of loading it from the file system.

npm version npm license Travis David npm downloads

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 to require() within the string module. Defaults to '' which results in relative require() 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