gpt4 book ai didi

amazon-web-services - 部署到 AWS lambda 时找不到模块 ./dist/server

转载 作者:行者123 更新时间:2023-12-03 22:30:23 27 4
gpt4 key购买 nike

我有一个使用节点 serverless deploy 在 AWS lambda 中运行的简单无服务器网站,然后我向站点添加了更多内容并更新到最新版本的无服务器,现在当我部署站点并访问我得到的 URL 时:

{"message": "Internal server error"}

当我访问 lambda 控制台时,我得到:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module './dist/server'",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module './dist/server'",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:45:30)",
" at Module._compile (internal/modules/cjs/loader.js:778:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
" at Module.load (internal/modules/cjs/loader.js:653:32)",
" at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
" at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
" at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)",
" at startup (internal/bootstrap/node.js:283:19)"
]
}

知道如何解决这个问题吗?

更多信息

基于@Amit Baranes 评论下面引用 this SO post这是一个 Angular 网站。我不太明白需要更改处理程序名称以匹配我的站点结构。我自己并没有将文件打包成 .zip,而是在运行 serverless deploy通过节点命令:

这是网站 serverless.yml
# generated by @ng-toolkit/serverless
service: serverless-site

plugins:
- serverless-apigw-binary

provider:
name: aws
runtime: nodejs10.x
memorySize: 192
timeout: 10
stage: production
region: us-east-1

package:
exclude:
- src/**
- node_modules/**
- firebug-lite/**
- e2e/**
- coverage/**
- '!node_modules/aws-serverless-express/**'
- '!node_modules/binary-case/**'
- '!node_modules/type-is/**'
- '!node_modules/media-typer/**'
- '!node_modules/mime-types/**'
- '!node_modules/mime-db/**'

custom:
apigwBinary:
types:
- '*/*'

functions:
api:
handler: lambda.universal
events:
- http: ANY {proxy+}
- http: ANY /

这是我的网站项目文件结构(从 src 目录开始)。

enter image description here
enter image description here

这是部署时的网站 lambda 函数:

enter image description here

由于该函数没有 server.js。我试图通过修改 server.ts 并将其放入文件夹中来创建,但没有成功:
// generated by Paul
//import 'zone.js/dist/zone-node';
//import 'reflect-metadata';

var express =require('express');
var cors= require('cors');
var compression =require('compression');

var join=require('path').join;

export const app = express();

app.use(compression());
app.use(cors());

const DIST_FOLDER = join(process.cwd(), 'dist/serverless-site');

app.get('*.*', express.static(join(DIST_FOLDER), {
maxAge: '1y'
}));

app.get('/*', (req, res) => {
res.sendFile(join(DIST_FOLDER + '/index.html'));
});

最佳答案

所以看来 server.js 不是在 中创建的距离 文件夹。
我通过运行 npm run build:server:prod 让它工作了之前 serverless deploy我的 package.json 中的相关行如下所示:"build:server:serverless": "webpack --config webpack.server.config.js"这可能不是正确的方法,但它解决了找不到 ./dist/server 的问题并让我遇到下一个错误:

{
"errorType": "TypeError",
"errorMessage": "express is not a function",
"stack": [
"TypeError: express is not a function",
" at Object.<anonymous> (/var/task/dist/server.js:9231:15)",
" at __webpack_require__ (/var/task/dist/server.js:21:30)",
" at /var/task/dist/server.js:85:18",
" at Object.<anonymous> (/var/task/dist/server.js:88:10)",
" at Module._compile (internal/modules/cjs/loader.js:778:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
" at Module.load (internal/modules/cjs/loader.js:653:32)",
" at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
" at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
" at Module.require (internal/modules/cjs/loader.js:692:17)"
]
}
我通过设置 esModuleInterop = false 修复了 express 错误在 tsconfig 文件中,感谢 AWS Serverless Lambda + Angular - TypeError: express is not a function

关于amazon-web-services - 部署到 AWS lambda 时找不到模块 ./dist/server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58478403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com