Giter Club home page Giter Club logo

require-from-string's Introduction

require-from-string Build Status

Load module from string in Node.

Install

$ npm install --save require-from-string

Usage

var requireFromString = require('require-from-string');

requireFromString('module.exports = 1');
//=> 1

API

requireFromString(code, [filename], [options])

code

Required
Type: string

Module code.

filename

Type: string
Default: ''

Optional filename.

options

Type: object

appendPaths

Type: Array

List of paths, that will be appended to module paths. Useful, when you want to be able require modules from these paths.

prependPaths

Type: Array

Same as appendPaths, but paths will be prepended.

License

MIT © Vsevolod Strukchinsky

require-from-string's People

Contributors

chinesedfan avatar eligolding avatar floatdrop avatar joehua87 avatar richardscarrott avatar sudo-suhas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

require-from-string's Issues

add babel require hook support?

when I require jsx, SyntaxError thrown

'use strict';

require('babel-core/register')({
    plugins: [
        'syntax-jsx',
        'transform-react-jsx'
    ]
});

var requireString = require('require-from-string');

var input = `
'use strict';

var React = require('react');

var Hello = React.createClass({
    render: function() {
        return (
            <p>Hello World</p>
        );
    }
});

module.exports = Hello;
`;

var Hello = requireString(input);
console.log(Hello);
❯ node a.js
evalmachine.<anonymous>:9
            <p>Hello World</p>
            ^

SyntaxError: Unexpected token <

Test fails with nodejs >= 10.16

Hi,

I has to patch test:

--- a/test/index.js
+++ b/test/index.js
@@ -17,11 +17,13 @@
        assert.equal(requireFromString('module.exports = 1;'), 1);
 });
 
+/*
 it('should accept filename', function () {
        assert.throws(function () {
                requireFromString('module.exports = ', 'bug.js');
        }, /bug\.js|Unexpected token }/);
 });
+*/
 
 it('should work with relative require in file', function () {
        var file = path.join(__dirname, '/fixture/module.js');
@@ -49,13 +51,13 @@
        try {
                requireFromString('throw new Error("Boom!");');
        } catch (err) {
-               assert.ok(/\(<anonymous>:1:69\)/.test(err.stack), 'should contain (<anonymous>:1:69) in stack');
+               assert.ok(/Boom!/.test(err.stack), 'should contain Boom! in stack');
        }
 
        try {
                requireFromString('throw new Error("Boom!");', '');
        } catch (err) {
-               assert.ok(/\(<anonymous>:1:69\)/.test(err.stack), 'should contain (<anonymous>:1:69) in stack');
+               assert.ok(/Boom!/.test(err.stack), 'should contain Boom! in stack');
        }
 });

[Memory Leak] Excessive calls to `requireFromString` leads to heap allocation error.

Hey!

I've been tracing down a memory leak in my web app for a while now. We use webpack-hot-server-middleware which consumes this package. Our webapp is quite large and after a certain number of file edits and webpack dev server compilations the app eventually calls it quits.

I've isolated the issue in the following code snippet:

#!/usr/bin/env node

var requireFromString = require('require-from-string');
var cycles = 100000;
var delay = 10;

(function step() {
  requireFromString(`/* ${'x'.repeat(10*512*1024)} */`, '/fake/path/to/file.js');

  if (--cycles > 0) {
    setTimeout(step, delay);
  }
}());

I've taken a look at the code in this repo and can't see any thing that jumps out at me. Im hoping that the issue isn't in the "Module" object.

Anything that would help would be appreaciated. Thank you

module error with webpack

This commit 80783b2 seems to have have broken compatibility with Webpack projects when resolving dependencies

./node_modules/metascraper/node_modules/require-from-string/index.js
Module not found: Can't resolve 'module' in '/project/node_modules/metascraper/node_modules/require-from-string'

Error when installing v2.0.2 from NPM

Had this come in from our project via gulp-postcss -> postcss-load-config -> cosmiconfig -> require-from-string and it seems the 2.0.2 publish broke things. Reverting to 2.0.1 fixed our issue.

npm install [email protected]

events.js:183░░░░░░⸩ ⠏ extract:require-from-string: sill extract [email protected]
      throw er; // Unhandled 'error' event
      ^

Error: sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw== integrity checksum failed when using sha512: wanted sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw== but got sha512-kJjj+Pl9OA1vwhjwIr6tVjYyiWU6wmBPVCHQ3VeYkdv/h23l3gkFHffnyQWO32FSh2SlAe9gx5Su/5vpT27OlQ==. (1814 bytes)
    at Transform.on (/usr/local/lib/node_modules/npm/node_modules/ssri/index.js:275:19)
    at emitNone (events.js:111:20)
    at Transform.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1055:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
events.js:183░░░░░░⸩ ⠏ extract:require-from-string: sill extract [email protected]
      throw er; // Unhandled 'error' event
      ^

Error: sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw== integrity checksum failed when using sha512: wanted sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw== but got sha512-kJjj+Pl9OA1vwhjwIr6tVjYyiWU6wmBPVCHQ3VeYkdv/h23l3gkFHffnyQWO32FSh2SlAe9gx5Su/5vpT27OlQ==. (1814 bytes)
    at Transform.on (/usr/local/lib/node_modules/npm/node_modules/ssri/index.js:275:19)
    at emitNone (events.js:111:20)
    at Transform.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1055:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
npm WARN [email protected] requires a peer of [email protected] but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of [email protected] but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of chai@^4.0.0 but none is installed. You must install peer dependencies yourself.

npm ERR! cancel after 1 retries!

npm ERR! A complete log of this run can be found in:
npm ERR!     .../2018-04-09T20_50_00_086Z-debug.log

default filename should be a uuid

something like this:

function requireFromString2(src, filename) {
  var Module = module.constructor;
  filename = filename || uuid();    // <<<<  instead of defaulting to '', default to a uuid
  var m = new Module();
  m._compile(src, filename);
  return m;  // return module not module.exports, so the user can get to the filename or whatnot
}

Question about "filename"

What exactly is filename used for? Module caching (i.e. prevents string-loaded module with filename "X" from being reloaded)? If so, does it need to be a "real" filename or could it be any unique string?

And/or is it used to determine the relative file system root for 'require' statements within the string-loaded code?

npm install fails

Hopefully this is self explanatory :-)

npm install --save require-from-string
npm ERR! file /usr/share/nodejs/dev/xxx/package.json
npm ERR! code EJSONPARSE
npm ERR! Failed to parse json
npm ERR! Unexpected token 'n' at 57:1
npm ERR! npm install "git+https://mygitserver:8443/scm/od/mymod.git"
npm ERR! ^
npm ERR! File: /usr/share/nodejs/dev/xxx/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR! 
npm ERR! Tell the package author to fix their package.json file. JSON.parse

Fails for files with win32 line endings

Actual behaviour

Files with win32 line endings fail with SyntaxError

echo "\r\nconsole.log('hello')\r\n" > test.js
node -e "require('require-from-string')(fs.readFileSync('./test.js').toString())"

:1
(function (exports, require, module, __filename, __dirname) { \r\nconsole.log('hello')\r\n
                                                              ^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:537:28)
    at requireFromString (~/node_modules/require-from-string/index.js:28:4)
    at [eval]:1:31
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at Object.runInThisContext (vm.js:116:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:573:30)
    at evalScript (bootstrap_node.js:452:27)

Expected behaviour

Files with win32 do not fail if otherwise syntactially correct.

Related

marionebl/commitlint#270

How to unload ?

Thanks, it works great, but how to unload the loaded code ?

TypeError: process.version is undefined

I'm trying to use this in a Quasar (https://quasar.dev/) environment to load a Vue.js component on the fly at runtime. Quasar uses Webpack and Babel. When I call it, it throws the error: "TypeError: process.version is undefined". Does anyone have ideas about what might be going on here?
Thanks.

Error in electron+webpack

electron provides a module module like node.js, so it differs with the pure browser environment described in #18.

But webpack's modules do not have module.parent(Refer webpack3). Can we add an undefined check for the following line?

parent.children && parent.children.splice(parent.children.indexOf(m), 1);

Module not found: Error: Cannot resolve module 'module'

After installing package when I am trying to load using
var requireFromString = require('require-from-string');

I am getting following error :

Module not found: Error: Cannot resolve module 'module' in D:\Nilay\RnD\node_modules\require-from-string
@ ./~/require-from-string/index.js 9:13-30

here is my webpack config

'use strict';

const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin')

var entry = [path.resolve(__dirname, 'app/bootstrap.js')];
var output = {
  path: path.resolve(__dirname, 'public/dist'),
  filename: 'bundle-[name].js',
  publicPath: "dist/"
};
var modules = {
  loaders: [
    { test: /\.json$/, loader: "json-loader" },
    {
      test: /\.js$/,
      loader: ['babel-loader'],
      exclude: '/(node_modules)/',
      query: {
        presets: ['react', 'es2015']
      }
    },
    { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader!autoprefixer-loader") },
    { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader" },
    { test: /\.(woff|woff2)$/, loader: "file-loader" },
    { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader" },
    { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader" }
  ]
};

var plugins = [
  new ExtractTextPlugin("bundle.css"),
  new webpack.DefinePlugin({
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
  }),
  new webpack.ProvidePlugin({
    jQuery: 'jquery',
    $: 'jquery',
    jquery: 'jquery'
  })
]

module.exports = {
  entry: entry,
  resolve: {
    modulesDirectories: ["node_modules"]
  },
  resolveLoader: {
    root: path.resolve(__dirname, 'node_modules')
  },
  output: output,
  stats: { colors: true },
  watch: true,
  module: modules,
  plugins: plugins
};

How can I resolve it?

Manually set `require.cache`

Might use this lib, but I have a question. If we do

require('./package.json');

then the require.cache object will be set for this path.

however, if we do:

const requirefs = require('require-from-string');

fs.readFile('./package.json', function(err,data){
    requirefs(data);
});

will the require.cache be set?

it's not clear from the docs and my guess is no. But maybe the Module constructor takes care of that? Not sure.

How to load an custom module, referenced by the module text?

I am not sure If this is a question or a bug.

In the text (that I am trying to load the module via requireFromString), I am importing a module like the following:

const DBClient = require('../DBClient');

The file 'DBClient.js' is present but the requireFromString is throwing the following error:

Error: Cannot find module '../DBClient'

Please advice.

What is `appendPaths` for?

Hi,

Thanks for this module. I'm trying to get imports (relative at the mo) working in the compiled code but not sure how to set a base path - appendPaths doesn't seems to help (I tried appending the path where the file resides). Is there a way of doing this?

If I copy file to where the CLI is executed (process.cwd()) which is the base of the project, it finds it. But I want to find it at the location I copied the file contents from.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.