Home > Software design >  Build generated from adapter-node is not working for official SvelteKit example
Build generated from adapter-node is not working for official SvelteKit example

Time:01-08

I am trying offical SvelteKit example https://realworld.svelte.dev/.
Its code is hosted at https://github.com/sveltejs/realworld

I tried to generate SSR build out of this project using adapter-node

My svelte.config.js is

import adapter from '@sveltejs/adapter-node';

export default {
    kit: {
        adapter: adapter({ out: '/tmp/realworld' })
    }
};

I am running npm run build to generate build.

I am running node /tmp/realworld/index.js but getting errors

alok@alok-HP-Laptop-14s-cf3xxx:~/tmp/test-svelte/realworld$ node /tmp/realworld/index.js 
(node:256781) 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)
/tmp/realworld/index.js:1
import { handler } from './handler.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.12.1

package.json file is

{
    "name": "realworld.svelte.dev",
    "version": "1.0.1-next.0",
    "private": true,
    "license": "MIT",
    "type": "module",
    "scripts": {
        "dev": "vite dev",
        "build": "vite build",
        "preview": "vite preview",
        "lint": "prettier --check --plugin-search-dir=. .",
        "format": "prettier --write --plugin-search-dir=. ."
    },
    "devDependencies": {
        "@sveltejs/adapter-node": "^1.0.0",
        "@sveltejs/adapter-vercel": "^1.0.0",
        "@sveltejs/kit": "^1.0.0",
        "marked": "^4.2.1",
        "prettier": "^2.7.1",
        "prettier-plugin-svelte": "^2.8.0",
        "svelte": "^3.54.0",
        "typescript": "^4.9.4",
        "vite": "^4.0.0"
    }
}

How can I make this build working?

CodePudding user response:

As stated by the https://github.com/sveltejs/realworld README, you need to start the project with npm run preview after building it with npm run build.

Tested, works locally on my machine (MacOS w/ Node 18.12.1), running on (default) port 4173.

svelte.config.js:

import adapter from '@sveltejs/adapter-node';

export default {
    kit: {
        adapter: adapter()
    }
};

Note: this default setup will locate your build in the build directory at the root of your project.

Edit:

You can choose to build into a location such as /tmp/realworld, but bear in mind that:

  • if the build directory is an absolute path, this path is relative to the root of your filesystem
  • you still need to launch with npm run preview
  • Related