如何避免节点.js中的浏览器变量

How to avoid browser variables in node.js

本文关键字:浏览器 变量 js 何避免 节点      更新时间:2023-09-26

最近我发现了 Flux 架构并找到了这个,React 入门套件。良好的结构,但是,应用程序需要针对浏览器进行编译,从节点.js使用gulp。

我正在使用 reqwest 对服务器进行 ajax 调用,reqwest 需要窗口,它在节点上.js不可用。当我从 ES 6 编译(使用 gulp-render(到 ES 5.1 时,react render 评估所有 require(..( 文件并给我错误,因为没有定义窗口,我该如何避免这种情况?你不得不处理这样的事情吗?

events.js:72
    throw er; // Unhandled 'error' event
          ^
ReferenceError: window is not defined
    at E:'projects'micaimportanta'node_modules'reqwest'reqwest.js:13:13
    at win (E:'projects'micaimportanta'node_modules'reqwest'reqwest.js:8:72)
    at Object.<anonymous> (E:'projects'micaimportanta'node_modules'reqwest'reqwest.js:11:2)
    at Module._compile (module.js:456:26)
    at Module._extensions..js (module.js:474:10)
    at reactTransform (E:'projects'micaimportanta'gulpfile.js:65:5)
    at Object.Plugin.reactTransform (E:'projects'micaimportanta'node_modules'gulp-render'index.js:72:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)

你可能想看看Browserify-shim