正如标题所述,我无法在生产模式下启动我的sapper项目。当我运行npm run start
时,我将在控制台上获得以下输出:
niklas@Niklass-iMac project-name % npm run start
> apple-on-svelte@0.0.1 start /Users/niklas/path/to/project
> node __sapper__/build
Starting server on port 3000
niklas@Niklass-iMac project-name %
如您所见,命令立即中止并且服务器未启动。可能是什么原因?
我的package.json
看起来像这样
{
"name": "project name",
"description": "description",
"version": "0.0.1",
"scripts": {
"build": "sapper build --legacy",
"deploy": "firebase deploy --only functions",
"dev": "sapper dev",
"export": "sapper export --legacy",
"fb_start": "npm run shell",
"logs": "firebase functions:log",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "node __sapper__/build"
},
"browserslist": [
"last 3 version"
],
"engines": {
"node": "10"
},
"dependencies": {
"compression": "^1.7.4",
"express": "^4.17.1",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.0",
"sirv": "^0.4.2"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.9.0",
"@babel/preset-env": "^7.9.5",
"@babel/runtime": "^7.9.2",
"@fullhuman/postcss-purgecss": "^2.1.2",
"@rollup/plugin-commonjs": "11.0.2",
"@rollup/plugin-node-resolve": "^7.1.3",
"@rollup/plugin-replace": "^2.3.1",
"autoprefixer": "^9.7.6",
"cssnano": "^4.1.10",
"firebase-functions-test": "^0.2.0",
"npm-run-all": "^4.1.5",
"postcss": "^7.0.27",
"postcss-import": "^12.0.1",
"postcss-nested": "^4.2.1",
"postcss-url": "^8.0.0",
"rollup": "^2.6.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-postcss": "^2.6.1",
"rollup-plugin-svelte": "^5.2.1",
"rollup-plugin-terser": "^5.3.0",
"sapper": "^0.27.12",
"svelte": "^3.20.1",
"svelte-preprocess": "^3.7.1",
"svelte-preprocess-postcss": "^1.1.1",
"tailwindcss": "^1.2.0"
}
}
我的src/server.js
看起来像这样:
import sirv from "sirv";
import express from "express";
import compression from "compression";
import * as sapper from "@sapper/server";
import "./assets/global.css";
const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === "development";
const expressServer = express().use(
compression({ threshold: 0 }),
sirv("static", { dev }),
sapper.middleware()
);
if (dev) {
expressServer.listen(PORT, (err) => {
if (err) console.log("error", err);
});
}
export { expressServer };
我的index.js
看起来像这样:
const functions = require("firebase-functions");
const { expressServer } = require("./__sapper__/build/server/server");
exports.ssr = functions.https.onRequest(expressServer);
我在macOS 10.15.5上使用汇总和最新的节点lts版本
没问题,我可以运行npm run dev
或npm run build
,但不能运行npm run start
我该如何解决?有什么想法吗?
答案 0 :(得分:0)
据我判断,您的快递服务似乎在开发模式下not
时您没有收听传入的连接。对此负责的确切代码是:
// only in dev mode you listen to inc connections
if (dev) {
expressServer.listen(PORT, (err) => {
if (err) console.log("error", err);
});
}
每当您不处于开发模式时,它将跳过清单部分。 我会考虑将if语句移到您的监听回调函数中,例如:
expressServer.listen(PORT, (err) => {
if (err && dev) console.log("error", err);
});
这应该使服务器保持运行并列出将来的连接。如果这对您有帮助,请让我保持大拇指。有关更多信息,我还建议您使用expressJS
的快速入门指南