Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/panosoft/require-string
Require a module from a string in memory instead of loading it from the file system.
https://github.com/panosoft/require-string
Last synced: 12 days ago
JSON representation
Require a module from a string in memory instead of loading it from the file system.
- Host: GitHub
- URL: https://github.com/panosoft/require-string
- Owner: panosoft
- Created: 2015-08-03T19:54:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-17T23:12:30.000Z (about 9 years ago)
- Last Synced: 2024-12-09T19:52:02.735Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# require-string
Require a module from a string in memory instead of loading it from the file system.
[![npm version](https://img.shields.io/npm/v/@panosoft/require-string.svg)](https://www.npmjs.com/package/@panosoft/require-string)
[![npm license](https://img.shields.io/npm/l/@panosoft/require-string.svg)](https://www.npmjs.com/package/@panosoft/require-string)
[![Travis](https://img.shields.io/travis/panosoft/require-string.svg)](https://travis-ci.org/panosoft/require-string)
[![David](https://img.shields.io/david/panosoft/require-string.svg)](https://david-dm.org/panosoft/require-string)
[![npm downloads](https://img.shields.io/npm/dm/@panosoft/require-string.svg)](https://www.npmjs.com/package/@panosoft/require-string)## Installation
```sh
npm install @panosoft/require-string
```## Usage
```js
var requireString = require('@panosoft/require-string');var module = requireString('module.exports = {number: 1}');
console.log(module.number); // 1
```## API
- [`requireString`](#requireString)
---
#### 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`:
```js
var path = require('path');module.exports = path;
```Then,
```js
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
```