webtorrent / webtorrent

⚡️ Streaming torrent client for the web

Home Page:https://webtorrent.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to import webtorrent npm package

SanpuiRonak opened this issue · comments

What version of this package are you using?
version: 2.1.11

What operating system, Node.js, and npm version?
OS: Windows 11
Node.js: v18.16.1
npm: 9.5.1

What happened?
The package is throwing errors. Not sure which logs to paste, so I will give steps to reproduce

What did you expect to happen?
Package should import normally without errors.

Steps to reproduce

  1. Create basic solid.js app with typescript
    npx degit solidjs/templates/ts my-app
  2. Install dependencies
    npm i
  3. Install webtorrent
    npm install webtorrent
  4. Install webtorrent types
    npm i --save-dev @types/webtorrent
  5. Follow get started and add the following code to App.tsx
import WebTorrent form "webtorrent"
...
const App: Component = () => {
	onMount(() => {
		const client = new WebTorrent()

		// Sintel, a free, Creative Commons movie
		const torrentId = 'magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent'

		client.add(torrentId, function (torrent) {
			// Torrents can contain many files. Let's use the .mp4 file
			const file = torrent.files.find(function (file) {
				return file.name.endsWith('.mp4')
			})

			// Display the file by adding it to the DOM.
			// Supports video, audio, image files, and more!
			console.log(file)
			file.appendTo('body')
		})
	});
...
  1. Start the app
    npm start

Are you willing to submit a pull request to fix this bug?
Yes

commented

Try to add this in your vite config:

resolve: {
   alias: {
      'webtorrent': fileURLToPath(new URL('./node_modules/webtorrent/dist/webtorrent.min.js', import.meta.url)),
   },
},