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');
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');
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
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.
To load with webpack 2.x and 3.x, install Imports Loader (npm i -D imports-loader), and add the following to your webpack config:
module: {
rules: [
{
test: require.resolve('snapsvg/dist/snap.svg.js'),
use: 'imports-loader?this=>window,fix=>module.exports=0',
},
],
},
resolve: {
alias: {
snapsvg: 'snapsvg/dist/snap.svg.js',
},
},
Then, in any module you’d like to require Snap, use:
import Snap from 'snapsvg';
SRC: SnapSVG GitHub