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

createRequire(import.meta.url) doesn't work from jsr registry #25071

Closed
rlidwka opened this issue Aug 16, 2024 · 8 comments
Closed

createRequire(import.meta.url) doesn't work from jsr registry #25071

rlidwka opened this issue Aug 16, 2024 · 8 comments
Labels
jsr Issues or feature requests relating to JSR.io working as designed this is working as intended

Comments

@rlidwka
Copy link

rlidwka commented Aug 16, 2024

deno 1.45.5

I'm running this code:

$ deno eval 'import UnpluginTypia from "jsr:@ryoppippi/[email protected]/vite";'
error: Uncaught (in promise) Error: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received https://jsr.io/@ryoppippi/unplugin-typia/0.6.18/src/core/cache.ts
    at createRequire (node:module:819:13)
    at https://jsr.io/@ryoppippi/unplugin-typia/0.6.18/src/core/cache.ts:11:35

Which internally does this:

import { createRequire } from 'node:module';
const { version: typiaVersion } = createRequire(import.meta.url)('typia/package.json')

createRequire fails because import.meta.url is an http url, and it expects a file path

Please advice whether that's intended behavior.

If it is, perhaps it needs more clear error message (i.e. "you can't require things in this context, here are workarounds").

@dsherret
Copy link
Member

dsherret commented Aug 16, 2024

It's expected behaviour. require doesn't work with remote modules like https://jsr.io/@ryoppippi/unplugin-typia/0.6.18/src/core/cache.ts (which as you pointed out is what import.meta.url is in this case). Since there is no node_modules folder in Deno with JSR, doing node resolution to resolve the "typia" specifier doesn't make sense to resolve from the JSR package, which has no context to be able to resolve with node resolution.

Ideally this package should instead probably do:

import packageJson from "npm:typia@^6.8.0/package.json" with { type: "json" };

@dsherret dsherret added the jsr Issues or feature requests relating to JSR.io label Aug 16, 2024
@ryoppippi
Copy link

Hi! |'m the author of this library.
I got some issue for json importing from some environment(mainly cjs+webpack environment)
And also, top level await cannot be used for cjs environment, right?
So is there any way to get version information from package.json by not using import and not using top-level await

@rlidwka
Copy link
Author

rlidwka commented Aug 18, 2024

It's expected behaviour. require doesn't work with remote modules

okay, thanks for clarification, I'll note createRequire(import.meta.url) to be an anti-pattern for compatibility reasons

@rlidwka rlidwka closed this as completed Aug 18, 2024
@ryoppippi
Copy link

ryoppippi commented Aug 18, 2024

  • we need to support cjs because of webpack, we can't use top-level await
  • We cannot use JSON import because some node env doesn't support it

We tried those two methods above, but some users reported issues related to these implementations.

so I use the require statement.

If you have some idea for this issue, send us a patch
@rlidwka

It's easy to say "anti-pattern". We are willing to support much environments as possible. DO NOT MAKE US SAD.

@rlidwka
Copy link
Author

rlidwka commented Aug 19, 2024

It's easy to say "anti-pattern". We are willing to support much environments as possible. DO NOT MAKE US SAD.

My intention with this issue was to clarify whether createRequire is intended to work with remote URLs. And to create a point of reference for anyone who comes up with the similar error in the future. Mention of it as "anti-pattern" was a short summary of discussion above, so other people can quickly scan this thread and consider using other options if available. I have no intentions and claim no responsibility for any change of feelings of anyone involved, my sole concern is for code correctness.

If you have some idea for this issue, send us a patch

I will send a patch when/if I find free time to find any solution for this issue.

@rlidwka
Copy link
Author

rlidwka commented Aug 19, 2024

@dsherret, that being said, I find it very alarming that I can have a package that works locally with createRequire, but stops working when I publish it to jsr registry. There's no good way to debug this (we can't publish every attempt at a fix to registry, right?). And other developers might not even be aware that published package is broken.

@dsherret
Copy link
Member

When developing the package is in the current directory with file:/// specifiers. Once published it's no longer in your local directory and so anything that depends on that fact (like createRequire) won't work.

We're going to add a jsr lint rule for createRequire to help prevent this issue denoland/deno_lint#1315

@ryoppippi
Copy link

ryoppippi commented Aug 19, 2024

@rlidwka Sorry to be emotional

@dsherret Is there any way to use deno lint in node project?
I'm using eslint for my project.
Is there any way to enable rules only related to JSR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
jsr Issues or feature requests relating to JSR.io working as designed this is working as intended
Projects
None yet
Development

No branches or pull requests

3 participants