In today's world, you pick an environment that contains the Javascript interpreter and then you can program, not only using what is built into the Javascript language, but also what other capabilities are built into that specific environment. In your case, the browser environment is where things like window
and document
come from. So, to program with those objects, you need a Javascript environment that supports those objects. Those objects are not built into the Javascript language itself - they are part of the browser environment.
node.js is also a Javascript environment, but it comes with a different set of APIs that do not include window
and document
, but it does include all sorts of things that the browser environment does not include such as the modules for http, net, streams, fs, etc...
So, if you wanted to run code designed for a browser in node.js, you would have to get a browser simulator (a library that makes the browser-specific things available so you can run them in nodejs such as puppeteer) - it uses the Chromium engine from Google to make browser functionality available from nodejs. If what you're trying to do is to test web page Javascript, then it may be easiest to use a program specifically designed for testing web page Javascript from nodejs such as Mocha.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…