简体   繁体   中英

How to avoid browser variables in node.js

Recently I discovered Flux architecture and found this, React starter kit . Good structure, but, the application need to be compiled for browsers, from node.js with gulp.

I'm using reqwest for making ajax calls to server, reqwest need window , which on node.js isn't available. When I compile (using gulp-render ) from ES 6 to ES 5.1, react render evaluate all require(..) files and give me error because window is not defined, how could I avoid this? Have you had to deal with something like this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM