Unable to import webtorrent npm package
SanpuiRonak opened this issue · comments
Ronak Sanpui commented
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
- Create basic solid.js app with typescript
npx degit solidjs/templates/ts my-app
- Install dependencies
npm i
- Install webtorrent
npm install webtorrent
- Install webtorrent types
npm i --save-dev @types/webtorrent
- 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')
})
});
...
- Start the app
npm start
Are you willing to submit a pull request to fix this bug?
Yes
Tim 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)),
},
},