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

deno bundle and createRequire use absolute paths #12495

Closed
Tracked by #12577
juliangruber opened this issue Oct 19, 2021 · 3 comments · Fixed by #15678
Closed
Tracked by #12577

deno bundle and createRequire use absolute paths #12495

juliangruber opened this issue Oct 19, 2021 · 3 comments · Fixed by #15678
Labels
cli related to cli/ dir node compat suggestion suggestions for new features (yet to be agreed)

Comments

@juliangruber
Copy link

Say you want to bundle a script that uses node_modules, like this:

import { createRequire } from "https://deno.land/[email protected]/node/module.ts"
const require = createRequire(import.meta.url)
const isarray = require("isarray")

If you run this it works fine, if you bundle and then run this it also works fine, but if you bundle it, then rename the folder containing the script, then run, it won't be able to find the dependency any more:

// use [email protected]
$ deno bundle --unstable source.ts > out.js
$ cd .. && mv deno deno2 && cd deno2
$ deno run --allow-read --unstable out.js
error: Uncaught Error: Cannot find module 'isarray'
Require stack:
- /Users/julian/Desktop/deno/source.ts
            const err = new Error(message10);
                        ^
    at Function._resolveFilename (file:///Users/julian/Desktop/deno2/out.js:16369:25)
    at Function._load (file:///Users/julian/Desktop/deno2/out.js:16438:33)
    at Module.require (file:///Users/julian/Desktop/deno2/out.js:16282:27)
    at require1 (file:///Users/julian/Desktop/deno2/out.js:16916:21)
    at file:///Users/julian/Desktop/deno2/out.js:16942:17

Also the error report will have both locations deno and deno2 mixed.

Would it be possible to either make this use relative paths, or even better, include node_modules in the bundle too?

@juliangruber
Copy link
Author

juliangruber commented Oct 19, 2021

So far the best fix that I've found would be (for this case) to use

const require = createRequire(Deno.mainModule)

however could that create other issues in the long run?

@nayeemrmn
Copy link
Collaborator

however could that create other issues in the long run?

No, it's most likely fine for your use case. If your Deno modules are being bundled together then you want them to act as the same referrer when requiring node modules. It won't make a difference if you're only using require() on node packages and not on relative project paths.

@juliangruber
Copy link
Author

Ok, thank you for the confirmation!

Do you think this is something that could be documented? It's now the second time that I had to replace import.meta.url with Deno.mainModule when using deno bundle.

@lucacasonato lucacasonato added node compat cli related to cli/ dir suggestion suggestions for new features (yet to be agreed) labels Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli related to cli/ dir node compat suggestion suggestions for new features (yet to be agreed)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants