Skip to content

panosoft/require-string

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published