Search code examples
javascriptnode.jspolyfills

Emulate browser in node js


I have JavaScript code that was written for browser. It uses document, history, navigator and other browser API. I want to run this code in node.js. I don't want to display HTMLElements, draw on canvas etc. I just want to run code, emulating xhr requests, fetch, websockets etc. Please, list all variables and functions that I need to polyfill. And are there other ways to run browser js in node?


Solution

  • If might be better if you provide more context or code examples, but the mock-browser package on npm is built to handle some of the behavior you mention. I'm not sure if it handles websockets, requests, etc, but it should serve as a good starting point.

    If you're just trying to list the functions and classes that are available globally on the window, you can try running console.log(Object.keys(window)).