kit: adapter-static fails with ESM config

npm init svelte@next test-esm-static
cd test-esm-static
mv svelte.config.cjs svelte.config.js
yarn add -D @sveltejs/kit@next @sveltejs/adapter-static@next

and changing the contents of svelte.config.js to

import adapter from "@sveltejs/adapter-static";

export default {
  adapter: adapter(),
  kit: {
    target: "#svelte",
  },
};

followed by svelte-kit (dev|build) throws

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in node_modules/@sveltejs/adapter-static/package.json imported from svelte.config.js
    at new NodeError (node:internal/errors:363:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:321:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:510:7)
    at packageResolve (node:internal/modules/esm/resolve:687:14)
    at moduleResolve (node:internal/modules/esm/resolve:739:18)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:853:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:73:40)
    at link (node:internal/modules/esm/module_job:72:36)

I was expecting this to work now that #936 is merged but looks like the adapters still need updating?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 24 (15 by maintainers)

Most upvoted comments

I did try to re-write it with ESM, but it just spat out:

Malformed svelte.config.js
SyntaxError: Unexpected strict mode reserved word
    at Loader.moduleStrategy (internal/modules/esm/translators.js:145:18)
    at async link (internal/modules/esm/module_job.js:47:21)

Also no my global.css isn’t relative, I have it set to /global.css

Can you please share what your attempted ESM conversion looks like? It’s almost impossible for anybody to know what the issue is if we can’t see that file.

(By the way, we shouldn’t really even be having a support session in the GitHub issues – probably the Discord server instead).

Just bit by this myself. I had const static in the file, and static is a reserved word. Replace all reserved words in the file and hopefully everything will work.

Is there a reason for adapter-static to exist as a separate package, vs encouraging users to roll their own? For example:

/** @type {import('@sveltejs/kit').Adapter} */
const staticAdapter = {
    name: "local/adapter-static",

    async adapt(utils) {
        utils.copy_static_files("build")
        utils.copy_client_files("build")

        await utils.prerender({
            all: true,
            dest: "build",
            fallback: null,
        })
    },
}

That’s likely due to a bug in Svelte 3.38.0 and 3.38.1 — should be fixed in 3.38.2

#936 was a pre-requisite for the upcoming work to publish the adapters as ESM. Until that’s done it’s probably better to continue using .cjs, though if you really want to use ESM config you should be able to do this:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

const adapter = require('@sveltejs/adapter-static');