我正在尝试官方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
来生成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
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"
}
}
我怎样才能使这个构建工作?
如https://github.com/sveltejs/realworld README所述,在使用npm run build
构建项目后,需要使用npm run preview
启动项目。
已测试,在我的机器(MacOS w/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