CanadaHonk
CanadaHonk7mo ago

Dynamically importing NPM modules errors

The following works fine in Node:
const parser = 'acorn';
const { parse } = await import(parser);
const parser = 'acorn';
const { parse } = await import(parser);
But with Deno gives the error:
Uncaught (in promise) TypeError: Loading unprepared module: npm:acorn@^8.9.0
Uncaught (in promise) TypeError: Loading unprepared module: npm:acorn@^8.9.0
Not sure what I should do to "prepare" it since I can't find any information on it?
1 Reply
Deno AI Helper
Deno AI Helper7mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet and error message you provided, it seems like you're trying to dynamically import an NPM module in Deno. Deno does not natively support NPM modules as it uses a different module system. In Deno, modules are imported using URLs, and there is no built-in npm command or node_modules directory. To use NPM packages in Deno, you would typically need to find an equivalent module hosted as an ES module that can be imported via URL, or use a service like esm.sh or skypack.dev to convert NPM packages to ES modules. Here's an example of how you might import an NPM package using esm.sh:
const parser = 'https://esm.sh/acorn';
const { parse } = await import(parser);
const parser = 'https://esm.sh/acorn';
const { parse } = await import(parser);
Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of this advice. If you continue to have issues, you might want to explore the Deno community resources or forums for more specific guidance on importing NPM modules in Deno.