window-or-global

1.0.1last stable release 10 years ago
Complexity Score
Medium
Open Issues
2
Dependent Projects
90
Weekly Downloadsglobal
113,359

License

  • MIT
    • Yesattribution
    • Permissivelinking
    • Permissivedistribution
    • Permissivemodification
    • Nopatent grant
    • Yesprivate use
    • Permissivesublicensing
    • Notrademark grant

Downloads

Readme

window-or-global

Use this module to get the global object both on server and client side. No more window is not defined errors just peace and flowers.

The code is borrowed from megawac/underscore@365311c.

Advantages:

  • You can easily mock it by mockery or proxyquire in your tests
  • It’s really useful in case of universal (or isomorphic) code, for example, when you’d like to render a React component both on client and server side

Install

$ npm i window-or-global

Usage

import root from 'window-or-global'

React example

components/my-component.jsx:

import React, { Component } from 'react'
// in node, you'll get the global object instead of crashing by an error
import root from 'window-or-global'

class MyComponent extends Component {

  // this method is only invoked in the browser environment
  componentDidMount() {
    root.addEventListener(/*...*/)
  }

  componentWillUnmount() {
    root.removeEventListener(/*...*/)
  }

  render() {}

}

// Voilà. Enjoy your universal react component! ;)
// No more 'window is not defined' errors when you render your component
// on server side.

Unit-testing example

You can find a complete test example here. The test passes of course, just type

$ npm test

in your terminal.

License

MIT © Purpose Industries

Dependencies

Loading dependencies...

CVE IssuesActive
0
Scorecards Score
3.40
Test Coverage
No Data
Follows Semver
Yes
Github Stars
79
Dependenciestotal
7
DependenciesOutdated
5
DependenciesDeprecated
0
Threat Modelling
No
Repo Audits
No

Learn how to distribute window-or-global in your own private NPM registry

npm config set registry  https://npm.cloudsmith.com/owner/repo
Processing...
Done
npm install window-or-global
Processing...
Done

2 Releases

NPM on Cloudsmith

Getting started with NPM on Cloudsmith is fast and easy.