Basic ExampleImporting off of defaultImporting typesLook for examplesDebugging importsDeno portsLimitations
Basic Example
You can import from npm via
await import("npm:package-name")
.Importing off of 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:Importing types
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!)Look for examples
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")
.Debugging imports
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.
Deno ports
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.
Limitations
- All Val Town imports must either start with
npm:
orhttps://
- Val Town only has dynamic imports. If you write static imports, they will be auto-rewritten.
- We donβt allow
require
style imports.