2

我正在尝试通过 @now-node. 任务基本上是部署一个具有依赖关系的 Polka 服务器,并静态地提供 static/ 和 client/ 文件。我已设法通过 includeFiles 包含 Lambda 所需的文件,但现在我在日志中看到构建器忽略了 package.json 中描述的依赖项。确切的信息是

Starting server on port 3000
Cannot find module 'sirv'
Did you forget to add it to "dependencies" in `package.json`?

但是我在构建日志中看到没有收集依赖项。两者package.jsonpackage-lock.json都存在于源文件中。

我将不胜感激有关如何解决此问题的任何建议。

我到达的 now.json 配置如下所示:

{
    "version": 2,
    "name": "experimental-sapper",
    "builds": [
        {
            "src": "__sapper__/build/index.js",
            "use": "@now/node",
            "config": {
                "includeFiles": [
                    "../build/**",
                    "../../static/**"
                    ]
            }
        },
        {
            "src": "static/**",
            "use": "@now/static"
        },
        {
            "src": "__sapper__/build/client/**",
            "use": "@now/static"
        }
    ],
    "routes": [
        { "src": "/(.*(\\.css)|(\\.json)|(\\.png))", "dest": "/static/$1" },
        { "src": "/client/(.*)", "dest": "/__sapper__/build/client/$1" },
        { "src": "/(.*)", "dest": "/__sapper__/build/index.js" }
    ],
    "alias": "..."
}

src/server.js 看起来像这样(在汇总捆绑之前):

import sirv from 'sirv';
import polka from 'polka';
import compression from 'compression';
import * as sapper from '@sapper/server';
const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';
const app = polka() // You can also use Express
    .use(compression({ threshold: 0 }));
if (dev) {
    app.use(sirv('static', { dev }));
}
app.use(sapper.middleware())
    .listen(PORT, err => {
        if (err) console.log('error', err);
    });
export default app.handler;

然后 package.json 也很标准:

  "description": "TODO",
  "version": "0.0.1",
  "scripts": {
    "dev": "sapper dev",
    "build": "sapper build --legacy",
    "export": "sapper export --legacy",
    "start": "node __sapper__/build",
    "cy:run": "cypress run",
    "cy:open": "cypress open",
    "test": "run-p --race dev cy:run"
  },
  "dependencies": {
    "compression": "^1.7.1",
    "express": "^4.17.1",
    "polka": "^0.5.0",
    "postcss-define-property": "^0.5.0",
    "sirv": "^0.4.0"
  },
  "devDependencies": {
    ...
  },
  "browserslist": "last 2 versions"
}

提前致谢!

4

1 回答 1

0

对于遇到此问题的任何人,解决方案是使用https://www.npmjs.com/package/now-sappernow-sapper上的构建器

网站上有说明,但基本上您需要如上所述导出处理程序,然后让构建器完成其余的工作。现在有一些错综复杂的情况,这意味着单独的节点构建器将无法工作。

您现在的配置应如下所示:

{
  "version": 2,
  "builds": [
    {
      "src": "package.json",
      "use": "now-sapper"
    }
  ]
}

有一个从 README 链接的演示项目,它是一个基本的 Sapper 模板,具有现在所需的配置。

导出应用程序的注释@now/static就足够了。

于 2020-02-18T15:59:49.110 回答