Reputation: 1290
I am importing the d3.js library in my React app like this:
import * as d3 from "d3";
I am creating a basic world map, which is also displayed correctly in the browser. However, when I try to do something like this in Chrome console:
d3.select("path")
I am getting an error:
Uncaught ReferenceError: d3 is not defined
I guess it has something to do how React works, and being a novice in React right now I don't know what i am missing here right now. Anybody can help?
Upvotes: 0
Views: 796
Reputation: 1012
Because webpack (or any other bunder) holds it in module scope. To prevent collisions between modules, bundlers wrap those with functions. You can consider all imports, as local function variables:
function yourModule(require) {
const d3 = require('d3');
// you can use d3 here within your module
}
you're not able to use it here, somewhere outside
Upvotes: 1