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

enable-window-document

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enable-window-document

Enables "window" and "document" globals so browser code does not throw errors in Node. Built with JSDOM.

  • 1.19.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Enable window and document

This package enables the window and document globals for executing most browser JS. For fully simulating the browser, see the enable-browser-mode plugin.

Usage

/* [CommonJS] */
require('enable-window-document');

- or -

/* [ES6] */
import 'enable-window-document'

No variable assignment required, just call it! The variables are stored on the global object so you can refer to them as you normally would in browser JS.

Example

Won't work:

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

>   ReferenceError: document is not defined

Works like a charm:

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

>   HTMLAnchorElement {Symbol(impl): HTMLAnchorElementImpl}

Implementation

This package simply creates a blank JSDOM with a few lines of code, and stores the global window and document variables, which point to the empty DOM:

let JSDOM = require('jsdom'),
    DOM = new JSDOM.JSDOM(`<html><body></body></html>`, {
        url: 'https://localhost',
        resources: 'usable',
        runScripts: global.UNSAFE_MODE 
            ? 'dangerously' 
            : 'outside-only'
    });

global.window = DOM.window,
global.document = window.document;

The url param is set for compatibility reasons related to localStorage, and resources and runScripts are set by default so that external scripts work as expected.

Unsafe Mode

By default, JSDOM is called with runScripts: 'outside-only'. Set global.UNSAFE_MODE before your require('enable-window-document') call to enable dangerous mode and execute external scripts when added to DOM.

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