Taako
Taako8mo ago

Importing NPM Packages in Deno Throws 'Uncaught SyntaxError' for Specific Modules

Hi there, I'm relatively new to Deno. I'm running into an error with an NPM package import.
import { BskyAgent } from "npm:@atproto/api";

const agent = new BskyAgent({
/* Agent options */
});
import { BskyAgent } from "npm:@atproto/api";

const agent = new BskyAgent({
/* Agent options */
});
error: Uncaught SyntaxError: The requested module 'npm:@atproto/api' does not provide an export named 'BskyAgent'
import { BskyAgent } from "npm:@atproto/api";
^
at <anonymous> (file:///Users/<username>/Desktop/deno/main.ts:1:10)
Watcher Process failed. Restarting on file change...
error: Uncaught SyntaxError: The requested module 'npm:@atproto/api' does not provide an export named 'BskyAgent'
import { BskyAgent } from "npm:@atproto/api";
^
at <anonymous> (file:///Users/<username>/Desktop/deno/main.ts:1:10)
Watcher Process failed. Restarting on file change...
The code works fine with NodeJS. I've tried using Deno run with the --unstable flag but got the same error message. I've also tried other packages (express for example) which worked just fine. Am I doing something wrong here? Do I need to configure something to get the package to work with Deno?
2 Replies
Deno AI Helper
Deno AI Helper8mo ago
It seems like you're encountering an issue with importing a specific NPM package in Deno. According to the documentation, Deno has native support for importing npm packages using npm: specifiers since version 1.28. However, there are a few things to consider when importing npm packages in Deno: 1. Module Resolution: Deno only supports the modern node16 module resolution. If a default export from an npm: import appears to have a wrong type, it might be because the package provides wrong types under node16 module resolution for imports from ESM. You can verify this by checking if the error also occurs with tsc --module node16 and "type": "module" in package.json or by consulting the "Are the types wrong?" website, particularly the "node16 from ESM" row. 2. TypeScript Types: Some npm packages ship with types out of the box, and you can import those and use them with types easily. However, some packages do not, and you might need to specify their types with a @deno-types directive, especially if you're using a @types package. 3. Unstable Features: Some features related to Node.js compatibility and npm package imports might still be unstable. You mentioned that you've tried using the --unstable flag, which is correct for using unstable features, but the issue might be with the package itself or how it exports its modules. Given the error message you're seeing, it's possible that the @atproto/api package might not be exporting BskyAgent in a way that's compatible with Deno's import mechanism, or it might be an issue with the package's type definitions when resolved under Deno's node16 module resolution. Here are a few steps you can take to troubleshoot the issue: - Check the Package's Exports: Look into the @atproto/api
Deno AI Helper
Deno AI Helper8mo ago
package's source code or documentation to verify that BskyAgent is indeed an exported member of the package. - Try Importing the Default Export: If the package uses a default export, you might need to import it without destructuring:
import BskyAgentPackage from "npm:@atproto/api";
const { BskyAgent } = BskyAgentPackage;

import BskyAgentPackage from "npm:@atproto/api";
const { BskyAgent } = BskyAgentPackage;

- Check for Type Definitions: If the package has a @types package available, you might need to include that in your import statement using the @deno-types directive. If after these steps the issue persists, it might be a compatibility issue with the package itself. In such cases, you can report the problem to the Deno issue tracker, as the Deno team is actively working on improving compatibility with npm packages. 🦕