Socket
Socket
Sign inDemoInstall

file-eval

Package Overview
Dependencies
6
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    file-eval

Read file and eval it


Version published
Weekly downloads
57
increased by21.28%
Maintainers
1
Install size
395 kB
Created
Weekly downloads
 

Readme

Source

file-eval

NPM Status Travis Status Coverage Status Dependency Status

Read file and eval it. Uses any-eval.

Like require, but asynchronous and doesn't use the module cache.

Important: internally file-eval will resolve passed relative paths with path.resolve(), not require.resolve().

In addition to JSON and CommonJS supports JSON5 data format.

Install

$ npm install --save file-eval

Usage

const fileEval = require('file-eval');

fileEval('./path/to/file.js')
    .then(console.log)
    .catch(console.error);

API

fileEval(file[, options])

file

Type: String.

The filename or file descriptor.

The file-eval determinate format by extension. If filename ends with .js, its contents will be evaluating with vm.

If filename ends with .json extention, its contents will be parsing with JSON.parse. If filename ends with .json5 extention, its contents will be parsing with json5.

By default expected JS-expression or CommonJS module contents.

options

Type: Object, string.

Options or encoding.

options.encoding

Type: string.

Default: utf-8.

The file encoding.

options.flag

Type: string.

Default: r.

The flag mode.

options.context

Type: Object.

The object to provide into execute method.

If context is specified, then module contents will be evaluating with vm.runInNewContext.

If context is not specified, then module contents will be evaluating with vm.runInThisContext.

With context you can provide some like-a-global variables into file-eval.

const fileEval = require('file-eval');
const secretKey = '^___^';

// The file has the contents "module.exports = secretKey;"
fileEval('./path/to/file.js', {
    context: { secretKey }
});

// ➜ '^___^'

fileEval.sync(file[, options])

Synchronous version of fileEval.

Method signature is same.

Formats

Supports CommonJS, JSON and JSON5 formats.

See examples with evaluating files with different formats.

CommonJS

Evaluates CommonJS files with .js extention.

const fileEval = require('file-eval');

// export data with `module.exports` or `exports`
fileEval('./path/to/file.js');

JSON

Evaluates JSON files with .json extention.

const fileEval = require('file-eval');

fileEval('./path/to/file.json');

JSON5

Evaluates JSON5 files with .json5 extention.

JSON5 is not an official successor to JSON, and JSON5 content may not work with existing JSON parsers. For this reason, JSON5 files use a new .json5 extension.

const fileEval = require('file-eval');

fileEval('./path/to/file.json5');
  • node-eval — eval Node.js contents only (JS-expression, CommonJS modules and JSON).
  • any-eval — eval any contents (JS-expression, CommonJS modules and JSON/JSON5).
  • node-file-eval — read node.js file and eval it with node-eval.

License

MIT © Andrew Abramov

Keywords

FAQs

Last updated on 12 Nov 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc