Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

enable-browser-mode

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enable-browser-mode

Simulate the browser with ease by setting the global object to a JSDOM window and painlessly run browser JS without errors.

  • 1.20.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

Enable browser mode

The goal of this package is to work as a quick-and-dirty one-liner that will allow Node to import and otherwise execute traditional browser code without throwing errors. It also sets the global object to window, and aims to simulate the browser as realistically as possible.

/* [CommonJS] */
require('enable-browser-mode');

- or -

/* [ES6] */
import 'enable-browser-mode'

No variable assignment required, just call it! You can then require browser JS with:

include('./jquery.min.js');

Which will evaluate that script in the global context. (Make sure the scripts supplied to window.include() are trusted.)

Example

Won't work:

console.log(document.createElement('a'));

>   ReferenceError: document is not defined

Works like a charm:

require('enable-browser-mode');
console.log(document.createElement('a'));

>   HTMLAnchorElement {Symbol(impl): HTMLAnchorElementImpl}

Use cases

The specific need for this functionality came from the web-widgets package, which generates widget trees using DOM operations like document.createElement(). The Node runtime cannot build out this widget tree by default, as it does not have access to the window and document variables, so it throws a ReferenceError.

By importing this package (which depends on JSDOM), we can expose the window and document globals to the whole project, meaning we can write all of our browser-optimized (and DOM-heavy) code in a file like browser.js, but still use that same code for server-side rendering in Node with require('browser.js').

In Node, web-widgets builds out the widget tree on the virtual DOM and then exports it as flat HTML using the Node.outerHTML property, and in the browser, the DOM is manipulated directly on-the-fly (i.e. with Node.appendChild). With enable-browser-window, all that is needed to reuse the original browser library is creating an separate JS file for Node, importing this package, and then importing your browser code:

require('enable-browser-mode');
require('browser.js');
myBrowserObject.doBrowserStuff(); 

// code like you're in the browser =)

Keywords

FAQs

Package last updated on 13 Jun 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc