Any idea why the following code doesn’t work if I do not write the full dependency path?
Also, why the named import doesn’t work?
source file
// import faunadb, { query as q } from "faunadb"
// console.log(q)
// > JSBUILD: failed to transform "scripts/fauna.js" (application/javascript): Could not resolve "util"
// import faunadb, { query as q } from "../../node_modules/faunadb/dist/faunadb-min.js"
// console.log(q)
// > Uncaught ReferenceError: q is not defined
import faunadb, { query } from "../../node_modules/faunadb/dist/faunadb-min.js"
q = query
console.log(q)
// > OK
This is an educated guess, at best…
-
See https://github.com/evanw/esbuild/issues/193
-
Change assets/scripts/fauna.js to:
import faunadb, { query as q } from "faunadb"
-
Change layouts/partials/scripts.html from:
{{ $fauna := resources.Get "scripts/fauna.js" | js.Build }}
to:
{{ $opts := dict "externals" (slice "util" "http" "https") }}
{{ $fauna := resources.Get "scripts/fauna.js" | js.Build $opts }}
-
Test everything thoroughly to make sure there are no unintended consequences.
I believe the effect of #3 is the same as placing the following in the project’s package.json:
"browser": {
"util": false,
"http": false,
"https": false
}
Thanks for helping.
Then the error disappears but faunadb/q are still not defined
.
With the package.json alternative, I’ve got this error:
Uncaught TypeError: util.inherits is not a function
Like I said, “an educated guess, at best.” You might consider raising the issue in the esbuild issue queue. I suspect the problem is related to how faunadb is packaged, but I don’t have enough experience in this context to be sure.