1. eval
Evaluate node require() module content directly
eval
Package: eval
Created by: pierrec
Last modified: Fri, 17 Jun 2022 21:21:05 GMT
Version: 0.1.8
Downloads: 3,194,009
Repository: https://github.com/pierrec/node-eval

Install

npm install eval
yarn add eval

Eval - require() for module content!

Overview

This module is a simple way to evaluate a module content in the same way as require() but without loading it from a file. Effectively, it mimicks the javascript evil eval function but leverages Node's VM module instead.

Benefits

Why would you be using the eval module over the nativerequire? Most of the time require is fine but in some situations, I have found myself wishing for the following:

  • Ability to supply a context to a module
  • Ability to load the module file(s) from non node standard places

Or simply to leverage JavaScript's eval but with sandboxing.

Download

It is published on node package manager (npm). To install, do:

npm install eval

Usage

 var _eval = require('eval')
var res = _eval(content /*, filename, scope, includeGlobals */)

The following options are available:

  • content (String): the content to be evaluated
  • filename (String): optional dummy name to be given (used in stacktraces)
  • scope (Object): scope properties are provided as variables to the content
  • includeGlobals (Boolean): allow/disallow global variables (and require) to be supplied to the content (default=false)

Examples

 var _eval = require('eval')
var res = _eval('var x = 123; exports.x = x')
// => res === { x: 123 }

res = _eval('module.exports = function () { return 123 }')
// => res() === 123

res = _eval('module.exports = require("events")', true)
// => res === require('events')

res = _eval('exports.x = process', true)
// => res.x === process

License

Here

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin