You can import from npm via await import("npm:package-name")
.
https://www.val.town/embed/stevekrouse.npmExample
default
The reason we used npm:lodash-es
instead of the classic npm:lodash
above is to avoid having to destructure the import off default
. However this is a very common thing you’ll have to do when using npm modules, and it looks like this:
https://www.val.town/embed/stevekrouse.npmDefaultExample
Sometimes, especially when making vals that interface with other libraries, you might want to only import a type from an npm package. You can do that using the familiar import
syntax (no need to await the promise here!)
https://www.val.town/embed/neverstew.dynamicImportTypeExample
Whenever you go to use a new npm library, we recommend first searching to see if anyone has used that library before. You can find the most common packages here.
Or you could look for examples related to cheerio
by searching for it or even more pointedly by searching for import("npm:cheerio")
.
It’s often not clear how a library will expose its methods. It can be helpful to log the keys of whatever object they return to you. That will tell you if you need to import it off of default or not, and what is available to you.
https://www.val.town/embed/stevekrouse.debugCheerioEx
It’s common to get Deno-related errors when importing npm packages. If you see a mysterious error, we reccomend looking for a Deno port of that npm package (search “name-of-package deno”) and trying that instead. As always, ask in our Discord if you want help.
npm:
or https://