question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

svelte:head remote js module not accessible,

See original GitHub issue

When testing this code :

<svelte:head>
  <title>Plotly</title>
  <meta name="description" content="">
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>

</svelte:head>

<script>

  import { onMount } from 'svelte';

  let data = [{
    x: ['giraffes', 'orangutans', 'monkeys'],
    y: [20, 14, 23],
    type: 'bar'
  }];

  onMount(() => {
    Plotly.newPlot('myDiv', data, {}, {showSendToCloud:true});
  });
            
</script>

<div>
  <h1>Plotly Example</h1>
</div>

<div id="myDiv"><!-- Plotly chart will be drawn inside this DIV --></div>

I get a “Plotly is not defined” error in the console… I can add the plotly script reference to the server.js file…and then the script works and my graph is rendered.

Oddly enough, if I copy and paste this into the : https://svelte.dev/examples#hello-world page…the graph will render. ???

Running on CentOS 7, tried Firefox 60.7.2esr and Chrome 75.0.3770.90 same effect.

On Svelte 3.12.1, using Rollup v1.25.1

Not a serious issue, new to Svelte just trying to get some old tools working. It looks like Svelte is going to power my next front end project. Love the project, keep up the good work guys.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
viper6277commented, Oct 27, 2019

Hi Conduitry, thanks for the reply… I took your suggestion I ended up changing the code to this… works like a charm… I wanted to post a working solution for others…

In fact this solution has now helped me with several other external modules…

<svelte:head>
  <title>Plotly</title>
  <meta name="description" content="">
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</svelte:head>

<script>

  import { onMount } from 'svelte';

  let data = [{
    x: ['giraffes', 'orangutans', 'monkeys'],
    y: [20, 14, 23],
    type: 'bar'
  }];

  onMount(() => {

   let script = document.createElement('script');
   script.src = "https://cdn.plot.ly/plotly-latest.min.js"
   document.head.append(script);

   script.onload = function() {
     Plotly.newPlot('myDiv', data, {}, {showSendToCloud:true});
   };

  });
            
</script>

<div>
  <h1>Plotly Example</h1>
</div>

<div id="myDiv"><!-- Plotly chart will be drawn inside this DIV --></div>

UPDATE !!! : I ended up changing the code again as I am using the “svelte-routing” module and the moment I navigated around it all got out of wack again… this solves my issues.

4reactions
saml-devcommented, Jul 10, 2020

@viper6277 thank you for posting the solution! You’ve saved at least one dev a headache 😁

Read more comments on GitHub >

github_iconTop Results From Across the Web

Docs • Svelte
Complete documentation for Svelte.
Read more >
How do I load an external JS library in Svelte/Sapper?
I hacked together a component to load external legacy JS libraries when I first started playing with Svelte2 and just refactored it to ......
Read more >
Working with Svelte stores - Learn web development | MDN
Sometimes, your app state will need to be accessed by multiple components that are not hierarchically related, or by a regular JavaScript module...
Read more >
Web Components with Svelte - This Dot Labs
Finally, we need to tell the compiler that we want to compile our Svelte components as custom elements. Go to vite.config.js and update...
Read more >
A Beginner's Guide to SvelteKit - SitePoint
In other words, Svelte already compiles your code during the build process and the final bundle only contains JavaScript that your application ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found