P
Prisma4mo ago
drew

Prisma breaks lambda

Hey there, I'm new to prisma but fell in love with the database seeding feature immediately. What a time saver and the format tool :chef-kiss: I'm having a problem as you can see from the screenshots that prisma orm as the only dependency makes my .zip deployable file to lambda 3 times the size of the postgres only version breaking my ability to upload the deployable (50MB limit on aws lambda). What is making it so large? How can I minimize this? What are my options to move forward?
No description
No description
No description
2 Replies
CodingWithDrew
CodingWithDrew4mo ago
Going to try npm prune —production and see if that helps
drew
drew4mo ago
nope, that didn't help
generator client {
provider = "prisma-client-js"
binaryTargets = ["rhel-openssl-3.0.x"]
}
generator client {
provider = "prisma-client-js"
binaryTargets = ["rhel-openssl-3.0.x"]
}
brought my package zip size down to 48.7MB. Just barely deployable. If I have any packages other than prisma I won't be able to upload. I used webpack with the following setup:
const path = require('path');
const fs = require('fs');
const nodeExternals = require('webpack-node-externals');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = (env) => {
return {
mode: 'production',
entry: path.resolve(__dirname, 'src', env.entry),
target: 'node',
externals: [nodeExternals()],
output: {
path: path.resolve(__dirname, 'deploy'),
filename: `${env.entry.replace('.js', '')}.bundle.js`,
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'prisma/schema.prisma', to: 'schema.prisma' }
]
})
],
optimization: {
minimize: true
},
node: {
__dirname: false,
__filename: false
}
};
};
const path = require('path');
const fs = require('fs');
const nodeExternals = require('webpack-node-externals');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = (env) => {
return {
mode: 'production',
entry: path.resolve(__dirname, 'src', env.entry),
target: 'node',
externals: [nodeExternals()],
output: {
path: path.resolve(__dirname, 'deploy'),
filename: `${env.entry.replace('.js', '')}.bundle.js`,
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'prisma/schema.prisma', to: 'schema.prisma' }
]
})
],
optimization: {
minimize: true
},
node: {
__dirname: false,
__filename: false
}
};
};
This appears to work and brought my total file size down to 7.3MB when zipped using this build script:
#!/bin/bash
set -vex
set -o pipefail

# Define the source and deploy directories
SRC_DIR="./src"
DEPLOY_DIR="./deploy"

# Clean and prepare the deploy directory
rm -rf $DEPLOY_DIR
mkdir -p $DEPLOY_DIR

# Loop through each .js file in the src directory
for entry in $SRC_DIR/*.js; do
# Get the base name of the file without the extension
base_name=$(basename $entry .js)

# Run Webpack to bundle this file
webpack --config webpack.config.js --env entry=$base_name.js

# Move to the deploy directory
cd $DEPLOY_DIR

# Create a directory for this function's deployment package
mkdir -p $base_name
mv $base_name.bundle.js $base_name/
cp ../prisma/schema.prisma $base_name/
cp ../node_modules/.prisma/client/libquery_engine-rhel-openssl-3.0.x.so.node $base_name/

# Zip this function's deployment package
zip -r $base_name.zip $base_name

# Clean up the function directory
rm -rf $base_name

# Move back to the project root directory
cd ..

done

# Clean up remaining .js and .txt files in the deploy directory
find $DEPLOY_DIR -type f \( -name "*.js" -o -name "*.txt" \) -delete

echo "Build and packaging completed."
#!/bin/bash
set -vex
set -o pipefail

# Define the source and deploy directories
SRC_DIR="./src"
DEPLOY_DIR="./deploy"

# Clean and prepare the deploy directory
rm -rf $DEPLOY_DIR
mkdir -p $DEPLOY_DIR

# Loop through each .js file in the src directory
for entry in $SRC_DIR/*.js; do
# Get the base name of the file without the extension
base_name=$(basename $entry .js)

# Run Webpack to bundle this file
webpack --config webpack.config.js --env entry=$base_name.js

# Move to the deploy directory
cd $DEPLOY_DIR

# Create a directory for this function's deployment package
mkdir -p $base_name
mv $base_name.bundle.js $base_name/
cp ../prisma/schema.prisma $base_name/
cp ../node_modules/.prisma/client/libquery_engine-rhel-openssl-3.0.x.so.node $base_name/

# Zip this function's deployment package
zip -r $base_name.zip $base_name

# Clean up the function directory
rm -rf $base_name

# Move back to the project root directory
cd ..

done

# Clean up remaining .js and .txt files in the deploy directory
find $DEPLOY_DIR -type f \( -name "*.js" -o -name "*.txt" \) -delete

echo "Build and packaging completed."
Ok I have had nothing but troubles treeshaking this and the code above didn't work as I had hoped so here is the updated versions:
bash
#!/bin/bash
set -vex
set -o pipefail

SRC_DIR="./src"
DEPLOY_DIR="./deploy"

rm -rf $DEPLOY_DIR
mkdir -p $DEPLOY_DIR
cp "prisma/schema.prisma" "$DEPLOY_DIR"
cp "node_modules/.prisma/client/libquery_engine-rhel-openssl-3.0.x.so.node" "$DEPLOY_DIR"

for entry in $(find "$SRC_DIR" -name "*.js"); do
base_name=$(basename "$entry" .js)
echo "Bundling $entry using Webpack..."
webpack --config webpack.config.js --env entry=$base_name.js
if [ ! -f "$DEPLOY_DIR/$base_name.js" ]; then
echo "Bundled file for $base_name not found, skipping."
continue
fi
mkdir -p "$DEPLOY_DIR/tmp"
mv "$DEPLOY_DIR/$base_name.js" "$DEPLOY_DIR/tmp"
cp "$DEPLOY_DIR/schema.prisma" "$DEPLOY_DIR/tmp"
cp "$DEPLOY_DIR/libquery_engine-rhel-openssl-3.0.x.so.node" "$DEPLOY_DIR/tmp"
mkdir -p "$DEPLOY_DIR/tmp/node_modules/@prisma"
cp -r node_modules/@prisma/client "$DEPLOY_DIR/tmp/node_modules/@prisma/client"
cp -r node_modules/.prisma "$DEPLOY_DIR/tmp/node_modules/.prisma"
echo "Zipping contents for $base_name..."
(cd "$DEPLOY_DIR/tmp" && zip -r "../$base_name.zip" ./*)
rm -rf "$DEPLOY_DIR/tmp"
echo "Contents of the zip file for $base_name:"
unzip -l "$DEPLOY_DIR/$base_name.zip"
done

rm -rf deploy/*.js
rm -rf deploy/*.txt

echo "Build and packaging completed."
bash
#!/bin/bash
set -vex
set -o pipefail

SRC_DIR="./src"
DEPLOY_DIR="./deploy"

rm -rf $DEPLOY_DIR
mkdir -p $DEPLOY_DIR
cp "prisma/schema.prisma" "$DEPLOY_DIR"
cp "node_modules/.prisma/client/libquery_engine-rhel-openssl-3.0.x.so.node" "$DEPLOY_DIR"

for entry in $(find "$SRC_DIR" -name "*.js"); do
base_name=$(basename "$entry" .js)
echo "Bundling $entry using Webpack..."
webpack --config webpack.config.js --env entry=$base_name.js
if [ ! -f "$DEPLOY_DIR/$base_name.js" ]; then
echo "Bundled file for $base_name not found, skipping."
continue
fi
mkdir -p "$DEPLOY_DIR/tmp"
mv "$DEPLOY_DIR/$base_name.js" "$DEPLOY_DIR/tmp"
cp "$DEPLOY_DIR/schema.prisma" "$DEPLOY_DIR/tmp"
cp "$DEPLOY_DIR/libquery_engine-rhel-openssl-3.0.x.so.node" "$DEPLOY_DIR/tmp"
mkdir -p "$DEPLOY_DIR/tmp/node_modules/@prisma"
cp -r node_modules/@prisma/client "$DEPLOY_DIR/tmp/node_modules/@prisma/client"
cp -r node_modules/.prisma "$DEPLOY_DIR/tmp/node_modules/.prisma"
echo "Zipping contents for $base_name..."
(cd "$DEPLOY_DIR/tmp" && zip -r "../$base_name.zip" ./*)
rm -rf "$DEPLOY_DIR/tmp"
echo "Contents of the zip file for $base_name:"
unzip -l "$DEPLOY_DIR/$base_name.zip"
done

rm -rf deploy/*.js
rm -rf deploy/*.txt

echo "Build and packaging completed."
webpack.config.js
const path = require('path');
const fs = require('fs');
const nodeExternals = require('webpack-node-externals');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = (env) => {
return {
mode: 'production',
entry: path.resolve(__dirname, 'src', env.entry),
target: 'node',
externals: [nodeExternals()],
output: {
path: path.resolve(__dirname, 'deploy'),
filename: `${path.basename(env.entry, '.js')}.js`, // Ensure filename matches the basename of the entry file
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'prisma/schema.prisma', to: 'schema.prisma' }, // Ensure schema.prisma is copied to deploy directory
// You can add more patterns here if there are other static files to be included
]
})
],
optimization: {
minimize: true
},
node: {
__dirname: false,
__filename: false
}
};
};
const path = require('path');
const fs = require('fs');
const nodeExternals = require('webpack-node-externals');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = (env) => {
return {
mode: 'production',
entry: path.resolve(__dirname, 'src', env.entry),
target: 'node',
externals: [nodeExternals()],
output: {
path: path.resolve(__dirname, 'deploy'),
filename: `${path.basename(env.entry, '.js')}.js`, // Ensure filename matches the basename of the entry file
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'prisma/schema.prisma', to: 'schema.prisma' }, // Ensure schema.prisma is copied to deploy directory
// You can add more patterns here if there are other static files to be included
]
})
],
optimization: {
minimize: true
},
node: {
__dirname: false,
__filename: false
}
};
};
Want results from more Discord servers?
Add your server