npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

enable-browser-mode

v1.37.0

Published

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

Downloads

32

Readme

Enable browser mode

The goal of this package is to work as a quick-and-dirty one-liner that will allow a Node process to imitate a browser, letting you import traditional browser JS by setting the global object to window and binding relevant native prototype methods like window.Event.

/* [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:

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

- or -

/* [CLI] */
$ enable-browser-mode test/jquery.js test/jquery2.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

By importing this package (which depends on JSDOM), we can expose the necessary globals and bind window as the global object, meaning we can write all of our browser-optimized (and DOM-dependent) code in a file like browser.js and reuse that same code in Node with include('browser.js') or window.include('browser.js').

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, resulting in a ReferenceError.

For server-side rendering in Node, web-widgets builds out the widget tree on the virtual DOM and then exports it as flat HTML using the HTMLElement.outerHTML property; in the browser, the DOM is manipulated directly on-the-fly (i.e. with HTMLElement.appendChild). By simulating the browser global, we can use the exact same core library for both cases, the difference being just Widget.render(HTMLElement) to render into a DOM element and Widget.export() to dump the outerHTML.

Using enable-browser-mode, all that is needed to reuse original browser libraries (including jQuery, HammerJS, etc.) is creating an separate JS file for your Node logic, importing this package, and then importing your browser code with include:

require('enable-browser-mode');

window.include('browser.js');
myBrowserObject.doBrowserStuff(); 

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

Unsafe Mode

By default, JSDOM is called with runScripts: 'outside-only'. If you need to execute external JS (and not just add a <script> element to the DOM), set global.UNSAFE_MODE before your require('enable-browser-mode') call.