Missing dependencies after serverless webpack
This is a Question
Description
I have a node project using serverless + webpack. I am having an issue where my lambda cannot find a node dependency even though it is specified in my package.json and it is getting loaded locally.
serverless.yml
service: test-lambda
package:
individually: true
plugins:
- serverless-webpack
- serverless-offline
- serverless-offline-scheduler
provider:
name: aws
runtime: nodejs10.x
timeout: 300
region: us-east-2
custom:
webpack:
webpackConfig: ./webpack.config.js
includeModules: true
functions:
run:
handler: src/handler.runTest
events:
- http:
path: runTest
method: post
- http:
path: runTest/{project}/{name}
method: post
package.json dependencies
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/register": "^7.5.5",
"babel-loader": "^8.0.6",
"babel-plugin-source-map-support": "^2.1.1",
"chromedriver": "^75.1.0",
"geckodriver": "^1.16.2",
"serverless": "^1.48.4",
"serverless-offline": "^5.8.0",
"serverless-offline-scheduler": "^0.3.8",
"serverless-webpack": "^5.3.1",
"webpack": "^4.38.0",
"webpack-node-externals": "^1.7.2",
"copy-webpack-plugin": "^5.0.4"
},
"dependencies": {
"aws-sdk": "^2.503.0",
"chai": "^4.2.0",
"@wdio/logger": "^5.11.0",
"find-node-modules": "^2.0.0",
"shelljs": "^0.8.3",
"tmp": "^0.1.0",
"@wdio/cli": "^5.11.10",
"@wdio/sync": "^5.11.0",
"@wdio/dot-reporter": "^5.11.0",
"@wdio/local-runner": "^5.11.1",
"@wdio/mocha-framework": "^5.11.0",
"@wdio/reporter": "^5.11.7",
"@wdio/spec-reporter": "^5.11.0",
"wdio-chromedriver-service": "^5.0.2",
"wdio-mochawesome-reporter": "^3.1.0",
"wdio-json-reporter": "^1.3.1",
"wdio-reporter": "^5.0.0-alpha.7",
"wdio-timeline-reporter": "^5.0.10",
"source-map-support": "0.5.12",
"fs-extra": "^8.1.0",
"prom-client": "^11.5.3",
"dateformat": "^3.0.3"
}
In my local node_modules folder, I see these wdio dependency folders as expected:

However, when I run serverless package and inspect the zip file produced, I see the node_modules folder is missing those key dependencies:

Where is everything else?
Finally, here is my webpack.config.js file:
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
entry: slsw.lib.entries,
target: 'node',
// Generate sourcemaps for proper error messages
devtool: 'source-map',
// Since 'aws-sdk' is not compatible with webpack,
// we exclude all node dependencies
externals: [nodeExternals()],
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
optimization: {
// We no not want to minimize our code.
minimize: false,
},
performance: {
// Turn off size warnings for entry points
hints: false,
},
// Run babel on all .js files and skip those in node_modules
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: __dirname,
exclude: /node_modules/,
},
],
},
plugins: [
new CopyPlugin([
{
from: 'test/**/*',
to: '.'
},
{
from: 'reporters/**/*',
to: '.'
},
{
from: 'wdio.conf.js',
to: 'wdio.conf.js'
},
]),
],
};
What am I missing here?
Additional Data
- Serverless-Webpack Version you're using: 5.3.1
- Webpack version you're using: 4.38.0
- Serverless Framework Version you're using: 1.48.4
- Operating System: mac osx
- Stack Trace (if available): ...
hi @Drewster727 did you manage to solve the problem? am having very similar sounding issue (as same setup)
It's all a bit weird, in my case, 50% of time it works 50%, just doing another build nothing else.
What I managed to find is that, when it works the code generated has: `/***/ (function(module, exports, webpack_require) {
var WEBPACK_AMD_DEFINE_FACTORY, WEBPACK_AMD_DEFINE_ARRAY, WEBPACK_AMD_DEFINE_RESULT;var WEBPACK_AMD_DEFINE_FACTORY, WEBPACK_AMD_DEFINE_ARRAY, WEBPACK_AMD_DEFINE_RESULT; (function(root, factory) { if (true) { // AMD. Register as an anonymous module. if (typeof window === 'object' && window.DOMImplementation && window.XMLSerializer && window.DOMParser) { !(WEBPACK_AMD_DEFINE_ARRAY = [], WEBPACK_AMD_DEFINE_FACTORY = (factory(window)), WEBPACK_AMD_DEFINE_RESULT = (typeof WEBPACK_AMD_DEFINE_FACTORY === 'function' ? (WEBPACK_AMD_DEFINE_FACTORY.apply(exports, WEBPACK_AMD_DEFINE_ARRAY)) : WEBPACK_AMD_DEFINE_FACTORY), WEBPACK_AMD_DEFINE_RESULT !== undefined && (module.exports = WEBPACK_AMD_DEFINE_RESULT)); } else { !(WEBPACK_AMD_DEFINE_ARRAY = [], WEBPACK_AMD_DEFINE_FACTORY = (factory(webpack_require(489), true)), WEBPACK_AMD_DEFINE_RESULT = (typeof WEBPACK_AMD_DEFINE_FACTORY === 'function' ? (WEBPACK_AMD_DEFINE_FACTORY.apply(exports, WEBPACK_AMD_DEFINE_ARRAY)) : WEBPACK_AMD_DEFINE_FACTORY), WEBPACK_AMD_DEFINE_RESULT !== undefined && (module.exports = WEBPACK_AMD_DEFINE_RESULT)); } }
when it doesn't the check if the window is defined is missing, any ideas anyone?