gpt4 book ai didi

Webpack 不包括部署无服务器应用程序的所有依赖项

转载 作者:行者123 更新时间:2023-12-05 05:56:53 24 4
gpt4 key购买 nike

在 AWS 上部署无服务器 lambda 函数时出现错误

"errorMessage": "Please install pg package manually"

我观察到无服务器不包括来自 pakage.json 的所有依赖项。 pg 和 pg-hoster 包丢失。

Serverless: Packing external modules: source-map-support@^0.5.19, dotenv@^10.0.0, ajv@^8.6.1, 
ajv-errors@^3.0.0, @middy/core@^1.5.2, @middy/http-json-body-parser@^1.5.2, sequelize@6.6.5,
bcryptjs@^2.4.3, jsonwebtoken@^8.5.1

我的 webpack.config 文件是

const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
/*
This line is only required if you are specifying `TS_NODE_PROJECT` for whatever reason.
*/
// delete process.env.TS_NODE_PROJECT;

module.exports = {
context: __dirname,
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
devtool: slsw.lib.webpack.isLocal ? 'eval-cheap-module-source-map' : 'source-map',
resolve: {
extensions: ['.mjs', '.json', '.ts', 'js'],
symlinks: false,
cacheWithContext: false,
plugins: [
new TsconfigPathsPlugin({
configFile: './tsconfig.paths.json',
}),
],
},
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
optimization: {
concatenateModules: false,
},
target: 'node',
externals: [nodeExternals()],
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{
test: /\.(tsx?)$/,
loader: 'ts-loader',
exclude: [
[
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, '.serverless'),
path.resolve(__dirname, '.webpack'),
],
],
options: {
transpileOnly: true,
experimentalWatchApi: true,
},
},
],
},
plugins: [
new ForkTsCheckerWebpackPlugin({
eslint: {
files: './src/**/*.{ts,tsx,js,jsx}', // required - same as command `eslint ./src/**/*.
{ts,tsx,js,jsx} --ext .ts,.tsx,.js,.jsx`
},
}),
],
};

我的 serverless.ts 文件看起来像

import type { AWS } from '@serverless/typescript';
import lambdas from './src/handlers/index';

const serverlessConfiguration: AWS = {
service: 'tapit-backend',
frameworkVersion: '2',
custom: {
webpack: {
webpackConfig: './webpack.config.js',
includeModules: true,
},
},
plugins: ['serverless-webpack', 'serverless-offline', 'serverless-prune-plugin'],
provider: {
name: 'aws',
runtime: 'nodejs14.x',
apiGateway: {
minimumCompressionSize: 1024,
shouldStartNameWithService: true,
},
environment: {
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
NODE_ENV: 'dev',
},
lambdaHashingVersion: '20201221',
},
// import the function via paths
functions: lambdas,
};

module.exports = serverlessConfiguration;

有人可以帮我解决我做错的地方吗?请

最佳答案

Full credit to this blog post

您正在开发一个 NodeJS + Webpack + Sequelize + pg + pg-hstore 应用程序。你编译所有东西,当你执行你的 webpack 包时,你有以下错误

throw new Error(`Please install ${moduleName} package manually`);
Error: Please install pg package manually

您需要使用 NPM 包“webpack-node-externals”从最终 bundle 中删除所有 nodes_modules 库。

为此,只需安装它

npm install --save-dev webpack-node-externals

在你的 webpack.config.js 文件中,你需要导入插件

const nodeExternals = require('webpack-node-externals');

在这个文件的“externals”部分,你需要声明

externals: [
nodeExternals(),
],

你的最终包会更小,你不会有 Error: Please install pg package manually 错误信息了。

关于Webpack 不包括部署无服务器应用程序的所有依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68964188/

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