1

请帮忙!!我知道我遗漏了一些非常简单的东西,但我对这一切都很陌生,而且我阅读了很多不同的帖子和教程,但没有弄错。

我在 Ubuntu 14.04 上启动了一个新项目,导航到我希望我的应用程序所在的文件夹。

这是我采取的步骤:

sudo apt-get install nodejs-legacy
sudo npm install express-generator -g  (framework)

express -e --ejs (Establece el lenguaje ejs como base) 
https://www.npmjs.com/package/ejs
npm install

运行应用程序DEBUG=myapp:* npm start

然后在浏览器中加载http://localhost:3000/以访问该应用程序。

npm install --save leaflet
npm install --save leaflet-draw

到这里一切正常!

在 app.js 中,我添加了这一行:

var L = require('leaflet');

并得到我粘贴的丑陋错误

/home/diego/Escritorio/fundacion/node_modules/leaflet/dist/leaflet-src.js:9168
}(window, document));
  ^
ReferenceError: window is not defined
    at Object.<anonymous> (/home/diego/Escritorio/fundacion/node_modules/leaflet/dist/leaflet-src.js:9168:3)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/diego/Escritorio/fundacion/app.js:10:9)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
npm ERR! weird error 8
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
4

3 回答 3

3

这是 Leaflet 本身的问题。Leaflet 尝试加载 DOM 而不检查它是否可用。我认为您可以通过在应用程序中模拟浏览器来加载传单。

// Create globals so leaflet can load
GLOBAL.window = {};
GLOBAL.document = {
  documentElement: {
    style: {}
  },
  getElementsByTagName: function() { return []; },
  createElement: function() { return {}; }
};
GLOBAL.navigator = {
  userAgent: 'nodejs'
};
GLOBAL.L = require('leaflet');

你也可以看看这个:leaflet-headless

于 2016-04-06T22:25:26.577 回答
1

看起来这个模块仅适用于浏览器。它在 npm 上,因此您可以使用 browserify 或 webpack 在前端要求它

于 2016-04-06T21:46:17.963 回答
1

上面 Sk Arif 的回答是在不引入包装模块的情况下执行此操作的最快方法。

navigator.platform这是我使用最新版本的传单(> 1.5.x)的实现,如果window.screen未定义,您将收到错误消息:

global.window = { screen: {} }
global.document = {
  documentElement: { style: {} },
  getElementsByTagName: () => { return [] },
  createElement: () => { return {} }
}
global.navigator = { userAgent: 'nodejs', platform: 'nodejs' }

const L = require('leaflet')
于 2019-07-02T11:04:54.990 回答