Julien Leray
Julien Leray

Reputation: 1687

Import NPM module having dependency

Currently switching to Meteor 1.3 and his npm module support, I've got this issue:

TypeError: Cannot set property 'tip' of undefined

Here the related code myFile.js:

import d3 from 'd3';
import d3tip from 'd3-tip';

//...
chart.tip = d3tip()
      .attr('class', 'd3-tip')
      .offset([-10, 0])
      .html(function(d) {
         //...
      });

The error come from the package d3-tip:

(function (root, factory) {                                                                                            
  if (typeof define === 'function' && define.amd) {                                                                    
    // AMD. Register as an anonymous module with d3 as a dependency.                                                   
    define(['d3'], factory)                                                                                            
  } else if (typeof module === 'object' && module.exports) {                                                           
    // CommonJS                                                                                                        
    module.exports = function(d3) {                                                                                    
      d3.tip = factory(d3)  // HERE THE ERROR (d3 probably not defined)                                                                                           
      return d3.tip                                                                                                    
    }                                                                                                                  
  } else {                                                                                                             
    // Browser global.                                                                                                 
    root.d3.tip = factory(root.d3)                                                                                     
  }                                                                                                                    
}(this, function (d3) { //...}

I guess the package d3-tip don't find d3, but d3 as d3-tip are finded on myFile.js.

Should I do something to manually inject d3 on d3-tooltip?

Upvotes: 0

Views: 325

Answers (2)

i--
i--

Reputation: 4359

Had an issue with this failing:

import d3 from "d3";

This was the solution:

import * as d3 from "d3";

I hope it will help someone.

Upvotes: 1

Nicolas Galler
Nicolas Galler

Reputation: 1309

I think you actually have to do

import d3tip from 'd3-tip';
d3tip(d3);

to get it to attach to d3.tip()

Upvotes: 2

Related Questions