从adapter-node生成的构建在官方SvelteKit示例中不起作用。

huangapple go评论87阅读模式
英文:

Build generated from adapter-node is not working for official SvelteKit example

问题

我正在尝试官方的SvelteKit示例,网址是https://realworld.svelte.dev/。
它的代码托管在https://github.com/sveltejs/realworld

我尝试使用adapter-node生成此项目的SSR构建。

我的svelte.config.js如下:

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

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

我运行npm run build来生成构建。

我运行node /tmp/realworld/index.js但是遇到了错误:

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文件如下:

{
	"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"
	}
}

我该如何使这个构建工作?

英文:

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?

答案1

得分: 1

根据https://github.com/sveltejs/realworld README所述,你需要在使用npm run build构建项目之后,使用npm run preview启动项目。

经过测试,本地环境(MacOS,Node 18.12.1)上正常运行,使用默认端口4173。

svelte.config.js

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

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

注意:此默认设置将在项目根目录的build目录中定位你的构建文件。

编辑:

可以选择构建到诸如/tmp/realworld之类的位置,但请注意:

  • 如果构建目录是绝对路径,该路径相对于你的文件系统的根目录
  • 仍然需要使用npm run preview来启动项目
英文:

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

huangapple
  • 本文由 发表于 2023年1月7日 18:02:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75039452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定