Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
Add 'Module resolution' section
Browse files Browse the repository at this point in the history
  • Loading branch information
not-my-profile committed Jul 4, 2023
1 parent 23788bb commit 4eebe52
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions node/npm_specifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ package:
import express from "npm:express@^4.17";
```

### Module resolution

The official TypeScript compiler `tsc` supports different
[moduleResolution](https://www.typescriptlang.org/tsconfig#moduleResolution)
settings. Deno only supports the modern `node16` resolution. Unfortunately many
NPM packages fail to correctly provide types under node16 module resolution,
which can result in `deno check` reporting type errors, that `tsc` does not
report.

If a default export from an `npm:` import appears to have a wrong type (with the
right type seemingly being available under the `.default` property), it's most
likely that 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?](https://arethetypeswrong.github.io/) website
(particularly the "node16 from ESM" row).

If you want to use a package that doesn't support TypeScript's node16 module
resolution, you can:

1. Open an issue at the issue tracker of the package about the problem. (And
perhaps contribute a fix :) (Although there unfortunately currently is a lack
of tooling for packages to support both ESM and CJS, since default exports
require different syntaxes, see also
[microsoft/TypeScript#54593](https://github.com/microsoft/TypeScript/issues/54593))
2. Use a [CDN](./cdns.md), that rebuilds the packages for Deno support, instead
of an `npm:` identifier.
3. Ignore the type errors you get in your code base with `// @ts-expect-error`
or `// @ts-ignore`.

### Including Node types

Node ships with many built-in types like `Buffer` that might be referenced in an
Expand Down

0 comments on commit 4eebe52

Please sign in to comment.