David
David

Reputation: 957

SvelteKit build throws ESM module error in production with adapter-node

exported via npm run build

Tested with the standard SvelteKit demo app and addition of adapter-node in the config as per usual:

import adapter from '@sveltejs/adapter-node'

const config = {
    kit: {
        // hydrate the <div id="svelte"> element in src/app.html
        target: '#svelte',
        adapter: adapter()
    }
};

export default config;

Using the latest versions as of writing this:

node index.js works locally but is failing in production:

(node:16609) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/[...]/build/index.js:40
import { createRequire } from "module";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1025:15)
    at Module._compile (node:internal/modules/cjs/loader:1059:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47```

Server is running latest NodeJS (v16.4.2).

What I've tried

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

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

Error is persisting. Ideas?

Upvotes: 3

Views: 3364

Answers (1)

David
David

Reputation: 957

Solution node_modules were not installed correctly previously. Had to make sure that npm install was completing correctly on the server. In case this helps someone…

Upvotes: 1

Related Questions