webtorrent / bittorrent-tracker

🌊 Simple, robust, BitTorrent tracker (client & server) implementation
https://webtorrent.io
MIT License
1.73k stars 315 forks source link

ERR_PACKAGE_PATH_NOT_EXPORTED on require #454

Closed rageshkrishna closed 1 year ago

rageshkrishna commented 1 year ago

What version of this package are you using? ^10.0.2

What operating system, Node.js, and npm version? Node 18.14.2, npm 9.5.0

What happened? I did npm install bittorrent-tracker and tried to run the following code with node index.js

var Client = require('bittorrent-tracker')
console.log(Client);

The output is:

node:internal/modules/cjs/loader:571
      throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in <snip>\node_modules\bittorrent-tracker\package.json
    at new NodeError (node:internal/errors:399:5)
    at exportsNotFound (node:internal/modules/esm/resolve:361:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:641:13)
    at resolveExports (node:internal/modules/cjs/loader:565:36)
    at Module._findPath (node:internal/modules/cjs/loader:634:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1061:27)
    at Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (C:\Users\rages\code\rs-prototypes\tracker\index.js:1:14) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

What did you expect to happen? Something gets dumped to the console.

Are you willing to submit a pull request to fix this bug? Yes, but I'm not sure I understand what's broken yet.

rageshkrishna commented 1 year ago

This is user error. I missed the fact that 10.x is ESM only and hadn't updated my configs to reflect that. Sorry about the noise.

For anyone else that runs into this, set "type": "module" in your package.json.

draeder commented 1 year ago

For others running into this, after setting "type": "module" in your package.json file, you import like so: import { Server, Client } from 'bittorrent-tracker'