Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm: import require of tslib fails, import_map does not help #18468

Closed
tv42 opened this issue Mar 27, 2023 · 6 comments
Closed

npm: import require of tslib fails, import_map does not help #18468

tv42 opened this issue Mar 27, 2023 · 6 comments
Labels
invalid what appeared to be an issue with Deno wasn't

Comments

@tv42
Copy link

tv42 commented Mar 27, 2023

npm package using require to get hold of tslib fails, making using that package impossible (without CDN hacks).

@kitsonk at #5427 (comment) thinks that using an import_map would help, but it doesn't:

podman run -it --rm --user deno docker.io/denoland/deno:alpine /bin/sh
/ $ deno --version
deno 1.32.1 (release, x86_64-unknown-linux-gnu)
v8 11.2.214.9
typescript 5.0.2
/ $ cat >/tmp/import_map.json <<EOF
{
  "imports": {
    "tslib": "npm:tslib@2.5.0",
    "ts-results": "npm:ts-results@3.3.0"
  }
}
EOF
/ $ deno eval --import-map /tmp/import_map.json 'import { Err, Ok, Result } from "ts-results";'
error: Uncaught Error: Cannot find module 'tslib'
Require stack:
- /deno-dir/npm/registry.npmjs.org/ts-results/3.3.0/index.js
- /deno-dir/npm/registry.npmjs.org/ts-results/3.3.0/index.js
    at Function.Module._resolveFilename (ext:deno_node/01_require.js:825:15)
    at Function.Module._load (ext:deno_node/01_require.js:657:27)
    at Module.require (ext:deno_node/01_require.js:871:19)
    at require (ext:deno_node/01_require.js:1011:16)
    at file:///deno-dir/npm/registry.npmjs.org/ts-results/3.3.0/index.js:12:19
    at file:///deno-dir/npm/registry.npmjs.org/ts-results/3.3.0/index.js:3:17
    at Object.<anonymous> (file:///deno-dir/npm/registry.npmjs.org/ts-results/3.3.0/index.js:9:3)
    at Object.<anonymous> (file:///deno-dir/npm/registry.npmjs.org/ts-results/3.3.0/index.js:17:4)
    at Module._compile (ext:deno_node/01_require.js:938:34)
    at Object.Module._extensions..js (ext:deno_node/01_require.js:971:10)
/ $

Related issues: #5427 #17403 #17841 denoland/manual#147

@marvinhagemeister
Copy link
Contributor

Had a quick peek at this and the issue is that the module isn't fetched at all. It's nowhere to be found on disk. The resolution logic itself seems to be correct and searches in all the right places.

@bartlomieju
Copy link
Member

@marvinhagemeister that's a good pointer - my hunch is that ts-results specifies tslib dependency in some strange way that we haven't considered and our logic doesn't pick it up as an npm package that should be fetched. I would suggest to look into ts-results and see how tslib is loaded in there, since it's a CommonJS file our static analysis might not handle that case correctly.

@bartlomieju
Copy link
Member

The CJS analysis can be found in https://github.com/denoland/deno_ast/blob/main/src/cjs_parse.rs

@tv42
Copy link
Author

tv42 commented Mar 28, 2023

Maybe this can help explain what ts-results is doing in a non-typical way: vultix/ts-results#37

@dsherret
Copy link
Member

dsherret commented Mar 28, 2023

The issue is that ts-results does not specify tslib is a peer dependency and yet it attempts to import it in its index.js file.

You can work around this by telling deno to load the tslib package from the import map as well:

 deno eval --import-map /tmp/import_map.json 'import "tslib"; import { Err, Ok, Result } from "ts-results";'

(Note that an import map is not a dependency manifest file, but rather a way to map specifiers, so you need an explicit side import to get it to be resolved and load)

@dsherret dsherret added the invalid what appeared to be an issue with Deno wasn't label Mar 28, 2023
@lucacasonato
Copy link
Member

I will close this because this is very obviously a ts-results bug, see vultix/ts-results#38.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid what appeared to be an issue with Deno wasn't
Projects
None yet
Development

No branches or pull requests

5 participants