question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Universal javascript compatibility

See original GitHub issue

My project is partially rendered server-side. I stumble on importation issue while behind my express server.

  import Datamap from 'datamaps';

Triggers an error “ReferenceError: window is not defined”

  var d3 = window.d3, topojson = window.topojson;

How do I properly import datamaps ?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
markmarkohcommented, Mar 17, 2016

@Guibod that’s a great question - I’ve never tried running it on the server before.

Can you give this branch a shot and see if it gets around the error?

0reactions
darkalorcommented, May 11, 2016

We’re using react with some of the stuff rendered on server side, and we actually managed to run this fine with the current version just by deferring any rendering of maps by calling require("datamaps") only when components have mounted (i.e. things got sent to client side). Alternatively if you use webpack you can inject some variables and have a fake “window” object.

@markmarkoh I’m a bit confused by your solution. In the code you have

  if (typeof exports === 'object') {
    d3 = require('d3');
    topojson = require('topojson');
    module.exports = Datamap;
  }
  else if ( typeof define === "function" && define.amd ) {
    define( "datamaps", ["require", "d3", "topojson"], function(require) {
      d3 = require('d3');
      topojson = require('topojson');

      return Datamap;
    });
  }
  else {
    window.Datamap = window.Datamaps = Datamap;
  }

Meaning that you already have proper handling for server side imports.

So what exactly is the point of fetching your dependencies here (outisde the environment check) instead of in the above block?

  // Save off default references
  var d3 = root.d3, topojson = root.topojson;
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript modules via script tag | Can I use... Support tables ...
Loading JavaScript module scripts (aka ES6 modules) using <script type="module"> Includes support for the nomodule attribute. Usage % of. all users, all tracked ......
Read more >
Isomorphic JavaScript - Wikipedia
Isomorphic JavaScript, also known as Universal JavaScript, describes JavaScript applications which run both on the client and the server.
Read more >
Asm.js, universal compatibility to applications - Scriptol.com
Asm.js is compatible with JavaScript, and provides no additional function but to be validated and compiled the code must only contain the subset...
Read more >
The road to universal JavaScript - fettblog.eu
Early on we decided to focus on web standards and support fetch , EcmaScript modules, etc. to make sure we have a common...
Read more >
Compatibility - Universal AR
Compatibility. Getting Started · JavaScript · Universal AR. This Zappar platform and Universal AR SDKs work well on the browsers that enjoy the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found