bluesky-social / atproto

Social networking technology created by Bluesky
Other
6.18k stars 433 forks source link

[@atproto/api] SyntaxError: Named export 'BskyAgent' not found. #910

Open xnuk opened 1 year ago

xnuk commented 1 year ago

Minimal reproduction:

// bar.mjs
import { BskyAgent } from '@atproto/api'  // 0.2.10
console.log(BskyAgent)

Note that this code is .mjs so it's ES Module. Running node bar.mjs shows this error, probably typescript/babel/etc bundling issue:

file:///home/xnuk/scratchpad/bar.mjs:1
import { BskyAgent } from '@atproto/api'
         ^^^^^^^^^
SyntaxError: Named export 'BskyAgent' not found. The requested module '@atproto/api' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@atproto/api';
const { BskyAgent } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)

Node.js v19.9.0
wesbos commented 1 year ago

This is because they are shipping Common.js code, and module.exports exporting 1 object with all the values on it.

The docs are incorrect - to import this in a esm project you need to do this:

import Proto from '@proto/api'
const { BskyAgent } = Proto

likely the docs are incorrect because they are using typescript with "allowSyntheticDefaultImports": true, - which is something you can use to make this syntax work.

This lib should be ideally be updated to ship both esm and cjs and have an exports property in the package.json:

  "exports": {
    ".": {
      "import": "./dist/my-lib.js",
      "require": "./dist/my-lib.umd.cjs"
    }
  }
apppel commented 11 months ago

This error generating way of importing the API is still mentioned in the API readme’s on this repo and on NPM. Would be nice to change this so that new devs can onboard more quickly (and spare the “I am going nuts?!” thoughts) 🙂.

pbaner16 commented 10 months ago

Does this issue still need to be looked at? I'm a first time open sourcer and would love to contribute! @xnuk

xnuk commented 10 months ago

Does this issue still need to be looked at? I'm a first time open sourcer and would love to contribute! @xnuk

@pbaner16 Yes, I still can reproduce this in node v21.4.0 and @atproto/api 0.7.2.

bnewbold commented 10 months ago

If folks have a recommendation for small changes to the package README which would reduce confusion around this, i'd be interested to review! Unlike others on the team, i'm not a very experience Typescript/Javascript developer myself and also get tripped up with this sort of thing. It is unlikely that we are going to make changing to the actual packaging or build pipeline for now.

apppel commented 10 months ago

Does this issue still need to be looked at? I'm a first time open sourcer and would love to contribute! @xnuk

Go for it!