Fix hard dependance on ES modules
Previously, __dirname was redefined to a value derived from import.meta.url. This meant when the adapter was transpiled to commonJS, __dirname would be undefined, crashing the server. Now it only redefined if it does not have a truthy value.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [ ] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it solves.
- [ ] Ideally, include a test that fails without this PR but passes with it.
- (I'm not sure adding esbuild for just one test is a great idea)
Tests
- [ ] Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- [ ] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. All changesets should bepatchuntil SvelteKit 1.0
⚠️ No Changeset found
Latest commit: 0d05ab64e478725ab1b7f12e7333ca652e76172a
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Thanks, though as noted above this definitely isn't the right fix as it's invalid JS.
Can you open an issue with a reproduction please?
Sorry about leaving this for so long - I had a bit of a busy couple of weeks.
So - the story for this. I've been building a SvelteKit app that needs to run on a server with a bit of a 'pet' set up that runs a bunch of other webapps on it. I wanted to run it in a way that I could upload a single JS file to the server for easier updates. Unfortunately, the /client/ and /static/ directories can't be all bundled into one file. I compromised a bit to wanting a small, quick upload to a single folder. Directly copying the build folder did not work, for two reasons:
- NodeJS does not allow using ES modules by default without
"type": "module"in a package.json - The dependencies are not bundled
- node_modules are huge, and include all dev dependences, with no way to separate them out without deleting and reinstalling production only.
I discovered the second one only after automating copying a package json into the build :(.
Of course, the obvious solution - bundle, transpile and treeshake it all!I used ESBuild, and it worked great first try, except for the one line of code here - it couldn't deal with import.meta.url, and when running the code with node, it errored out.
After much fiddling and trial and error, I came up with this: @[email protected]
diff --git a/files/handler-393b2283.js b/files/handler-393b2283.js
index c5588bfa8e4eb54fed732724e95aa6dab677a944..c6a50d6de94641f8e7f71eddaf704b48017d190c 100644
--- a/files/handler-393b2283.js
+++ b/files/handler-393b2283.js
@@ -1052,7 +1052,14 @@ const address_header = env('ADDRESS_HEADER', '').toLowerCase();
const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase();
const host_header = env('HOST_HEADER', 'host').toLowerCase();
-const __dirname = path.dirname(fileURLToPath(import.meta.url));
+let __dirname;
+try {
+ __dirname = path.dirname(fileURLToPath(import.meta.url))
+} catch (e) {
+ try {
+ __dirname = path.dirname(resolve(".", process.argv[1] ))
+ } catch (e) { }
+}
/**
* @param {string} path
With the caveat that node must be run with the filename to be executed rather than the folder, defaulting to index,js or the settings in package.json ("main" I think?). There's probably a way to account for this, or a better way of doing it in general, but this worked for me.
Again, apologies for leaving this so long, and for my writing skills. I also did this all via the web interface, so sorry for not running all the scripts as well.
Your latest proposed patch is even less likely to be merged, so I'm going to go ahead and close this.
If you're just trying to bundle dependencies, you can do something like this: https://github.com/sveltejs/kit/issues/3176#issuecomment-1176372356
Otherwise, you should use the custom server option: https://github.com/sveltejs/kit/tree/master/packages/adapter-node#custom-server