Search code examples
javascripthtmlsvgweb-frontendsvg.js

"SVG is not defined" when using svg.js


I am trying to make a basic rectangle inside of my webpage using svg.js. I have the library installed via npm and i have refrenced the source file in a script tag, but my SVG drawing will not show up.

The console provides the error, "Uncaught ReferenceError: SVG is not defined",

Here is a snippet of my project

var draw = SVG('drawing').size(300, 300);
var rect = draw.rect(100, 100).attr({
  fill: '#f06'
});
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" type="text/css" href="main.css">

  <title>Breathe</title>
</head>

<body>
  <div class="container">
    <div class="header-container">
      <div class="title-container">
        <div>Title</div>
        <div></div>
        <div></div>
      </div>
    </div>
    <div class="main-container">
      <div id="drawing"></div>
    </div>
    <div class="footer-container">

    </div>

  </div>
  <script src="main.js" type="text/javascript"></script>
  <script src="node_modules\svg.js\dist\svg.js"></script>
</body>

</html>


Solution

  • You loaded the main.js file first and then svg.js. Try reversing the order.

    <script src="node_modules\svg.js\dist\svg.js"></script>
    <script src="main.js" type="text/javascript"></script>