# node-window-polyfill

> Polyfill for the problem `window is not defined` in node.js

Latest version **1.0.4** (published 2025-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-window-polyfill
pnpm add node-window-polyfill
yarn add node-window-polyfill
bun add node-window-polyfill
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2025-01-06 |
| First published | 2018-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 21.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Tomasz Gorka |
| Maintainers | tgorka |

## Links

- npm: https://www.npmjs.com/package/node-window-polyfill
- Repository: https://github.com/tgorka/node-window-polyfill
- npm.io page: https://npm.io/package/node-window-polyfill

## Dependencies (1)

- [ws](https://npm.io/package/ws.md) ^7.4.3

## Recent versions

- 1.0.4 (latest) — 2025-01-06
- 1.0.3 — 2025-01-06
- 1.0.2 — 2021-03-06
- 1.0.0 — 2018-04-26

## README

# node-window-polyfill

[![Version npm](https://img.shields.io/npm/v/node-window-polyfill.svg)](https://www.npmjs.com/package/node-window-polyfill)

Polyfill for the problem `window is not defined` in node.js.
It's inserting window object on globals 
and inputing all needed sub-elements 
with requiring web sockets dependency.

## Installation

Yarn
```
yarn add node-window-polyfill
```

Npm
```
npm install node-window-polyfill
```

### Requirements 
Minimum ES5.

To use it in different environment it's required to clone and build it with the proper flag.

## Usage

Import it in global file, or in every file 
where the source code requires the `window` object.

```typescript
import nodeWindowPolyfill from "node-window-polyfill";

nodeWindowPolyfill.register();
```

For shorter import that will call register directrly
```typescript
import "node-window-polyfill/register";
```

If you don't want to import 'ws' module and just create empty objects:
```typescript
import nodeWindowPolyfill from "node-window-polyfill";

nodeWindowPolyfill.register(false);
```

And the version previous ES versions:
```javascript
require('node-window-polyfill').register();
```

## What is polyfilled?

- usage of `window` in the code or libraries
- `window.setTimeout` - default `setTimeout` node function
- `window.clearTimeout` - default `clearTimeout` node function
- `window.WebSocket` - version from global object 
- `window.ArrayBuffer` - version from global object only
- `window.addEventListener` - empty void function
- `window.navigator` - `{ onLine: true }`
- `window.isNodeJS` - to check if polyfills were aplied
- `window.localStorage` - in memory storage
- `window.Date` - version from global object
- `global.WebSocket` - using [ws](https://www.npmjs.com/package/ws) if the right flag is not set
- if the above functions/properties are already set in the `global` object then they will be taken into the account

## Configure

To configure development environment and run the tests, 
first clone the repository:

```
git clone https://github.com/tgorka/node-window-polyfill.git
```

then (once you have `yarn` and node installed) install dependencies

```
yarn
```

## Build

To build the distribution and types

```
yarn build
```

The distribution is in the `dist` folder and types in the `types` folder.

To change defauld ES standard use `--target` flag. Ex.:
```
yarn build -target es3
```

## Test

After configuring we can run tests.

```
yarn test
```

We can use debug information logging with setting up env variable

```
DEBUG=nodeWindowPolyfill-* yarn test
```

## Author
Tomasz Górka <http://tomasz.gorka.org.pl>

## License
&copy; 2018 Tomasz Górka

MIT licensed.

---
_Source: https://npm.io/package/node-window-polyfill · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
