webtorrent / webtorrent

⚡️ Streaming torrent client for the web

Home Page:https://webtorrent.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Integrate with video players tutorial doesnt work - fails to import webtorrent from esm.js

mitra42 opened this issue · comments

What version of this package are you using?
2.0.18

What operating system, Node.js, and npm version?
OSX - browser = Chrome 113.0.5672.92. yarn 1.22.19

What happened?
After problems trying to use the tutorial as an example for my own app (after being pointed there after I posted a bug), I just tried running the tutorial https://github.com/webtorrent/webtorrent/blob/master/docs/tutorials.md#integrate-webtorrent-with-video-players by copying the code to a new html file and opening in Chrome.

It showed one of the errors I was hitting in my app as well, specifically.

caught TypeError: Cannot read properties of null (reading 'tmpdir')
    at server.js:22:52

This should be trivially replicable. The problem seems to be in the line

import WebTorrent from 'https://esm.sh/webtorrent';

but I'm not sure what should be happening.

If I replace this with the line

import WebTorrent from '/node_modules/webtorrent/dist/webtorrent.min.js'

It imports OK, though fails to play for other reasons.

What did you expect to happen?

Are you willing to submit a pull request to fix this bug?
Sorry, I'm not sure what is happening and not familiar enough with the code to fix it.

Another couple of problems with the tutorial

  • it should probably be explicity about needing a copy of sw.min.js in the root directory of the server serving this HTML, otherwise if its loaded from anywhere else it doesnt' seem to work (I think it is problems with scope when it tries to get /webtorrent/xxx, but I'm not sure).
  • the Quick Example at https://github.com/webtorrent/webtorrent/blob/master/docs/api.md#quick-example probably needs updating. This was what I was trying to adapt, without realizing it really doesnt work e.g. because the service worker isn't ready -so it needs the longer code that is in the tutorial.
commented

this is one of those "it will fix itself" issues, esm.sh is aware of their bundling issues and are fixing them, so it will probs work sometime this week

the quick example doesn't work on first load, but works on the subsequent loads, because its a quick example, i didnt feel like having the need to add full functionality to it, but it works

Ok understood re ESM.

Also understood re poor example, just be aware I wasted three hours the day before yesterday trying to make a version of that code work in my web component, thinking the problem was in how I'd adapted it to my web component, rather than a problem in the example itself. Hopefully others don't hit that issue and give up.

commented

issue solved itself