问题
I'm interested in using ol3 in a node.js app (e.g. the ol.format APIs for reading GeoJSON). Is that a supported use case?
I ask because it doesn't appear to work by just running npm install openlayers
and then requiring it. var ol3 = require('openlayers')
fails on code that's looking for the geolocation
property of the window.navigation
object since the window.navigation doesn't exist in a node.js environment
TypeError: Cannot use 'in' operator to search for 'geolocation' in undefined at /home/me/src/ol3/node_modules/openlayers/dist/ol.js:144:823 at OPENLAYERS (/home/me/src/ol3/node_modules/openlayers/dist/ol.js:9:22) at Object. (/home/me/src/ol3/node_modules/openlayers/dist/ol.js:13:2) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require (module.js:384:17)
回答1:
You can't use OpenLayers 3 in purely Node context (but it's possible with Browserify/WebPack for client side use)
You can always stub window
and other objects in Node but stub is for tests...
It's also possible to run OpenLayers 3 with PhantomJS (headless browser).
I'm not aware OpenLayers was designed for other uses than browser.
To get more insight, you should post your question on the OpenLayers 3 dev list to confirm.
Do not duplicate exactly your StackOverflow question! You'd better ask something like "Is openlayers3 designed to only be used in the browser?" as the OpenLayers dev list is really for dev question and not about classic library usage/simple assistance nowadays.
来源:https://stackoverflow.com/questions/30183186/openlayers-3-in-node-js-app