Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

snapsvg and requirejs, Cannot read property 'on' of undefined`

Tags:

snap.svg

I'm using require and webpack on a project and want to use snapsvg but when I include it I get the error:

Uncaught TypeError: Cannot read property 'on' of undefined

Code is:

var Snap = require('snapsvg');
SVG = Snap('#svg');
like image 365
James Avatar asked Dec 08 '22 23:12

James


2 Answers

You can also use snapsvg-cjs, which is actually wrapper writter in snapsvg with common js.

Steps:

npm install snapsvg-cjs

Import in component like below:-

import "snapsvg-cjs";

declare const Snap: any;

Now you have Snap object.

like image 25
Sagar Kharche Avatar answered Mar 08 '23 01:03

Sagar Kharche


Ah the way to do it is to add:

var webpack = require('webpack');
module.exports = {
  ...
  module: {
    loaders: [{
      test: require.resolve('snapsvg'),
      loader: 'imports-loader?this=>window,fix=>module.exports=0'
    }]
  }
  ...
};

in webpack.config.js using imports-loader

like image 167
James Avatar answered Mar 08 '23 02:03

James