Webpack 不包括部署无服务器应用程序的所有依赖项
Webpack not including all dependencies on deploying serveless application
我在 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;
有人可以帮我解决我做错的地方吗?请
您正在开发一个 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”的最终捆绑包中删除所有 nodes_modules 库。
要做到这一点,只需安装
npm install --save-dev webpack-node-externals
在您的 webpack.config.js 文件中,您需要使用
导入插件
const nodeExternals = require('webpack-node-externals');
并且在此文件的“外部”部分中,您需要声明
externals: [
nodeExternals(),
],
你的最终包会更小,你不会再有错误信息:请手动安装 pg 包。
我在 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;
有人可以帮我解决我做错的地方吗?请
您正在开发一个 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”的最终捆绑包中删除所有 nodes_modules 库。
要做到这一点,只需安装
npm install --save-dev webpack-node-externals
在您的 webpack.config.js 文件中,您需要使用
导入插件const nodeExternals = require('webpack-node-externals');
并且在此文件的“外部”部分中,您需要声明
externals: [
nodeExternals(),
],
你的最终包会更小,你不会再有错误信息:请手动安装 pg 包。