serverless-webpack
serverless-webpack copied to clipboard
Unable to import "function handler" in logs
This is a Bug Report
Description
Im using serverless-webpack
with serverless-http
with express.
I have my serverless.yaml:
custom:
webpack:
packager: 'yarn'
webpackConfig: './webpack.config.js'
functions:
app:
handler: main.handler
....
this is my main.js
file:
const serverless = require('serverless-http');
const app = require('./app');
module.exports.handler = serverless(app);
and the app
file is the express app.
When Im using serverless offline start
it works well with webpack build.
But when deploying I see the cant find "main" in the trace...
Any help is appreciated!
For bug reports:
-
What went wrong? webpack builds properly with serverless local, but when deploying I get "main" not found. (this is my function handler)
-
What did you expect should have happened? deploy will go well and app will be useable
-
What was the config you used?
-
What stacktrace or error message from your provider did you see?
Unable to import module 'main': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/main.js:1:3496)
at n (/var/task/main.js:1:220)
at Object.<anonymous> (/var/task/main.js:1:3559)
at n (/var/task/main.js:1:220)
at /var/task/main.js:1:1019
at Object.<anonymous> (/var/task/main.js:1:1029)
Similar or dependent issue(s): *
Additional Data
- Serverless-Webpack Version you're using: 5.1.5
- Webpack version you're using: 4.12.0
- Serverless Framework Version you're using: 1.32.0
- Operating System: mac
- Stack Trace (if available):
Unable to import module 'main': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/main.js:1:3496)
at n (/var/task/main.js:1:220)
at Object.<anonymous> (/var/task/main.js:1:3559)
at n (/var/task/main.js:1:220)
at /var/task/main.js:1:1019
at Object.<anonymous> (/var/task/main.js:1:1029)
Not sure if it's same problem but I encountered a similar issue that I spent countless hours debugging. What version of node are you using locally? I updated from v8.10 to v8.12.0 and then the issues went away. I know you are suppose to use the same version with aws lambda but I couldn't get past this issue any other way. Hopefully it works for you.
I have the exact same issue Deps
{
"devDependencies": {
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.2.3",
"@babel/register": "^7.0.0",
"@babel/runtime": "^7.2.0",
"axios": "^0.18.0",
"babel-cli": "^6.26.0",
"babel-loader": "^8.0.4",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-2": "^6.24.1",
"chai": "^4.1.2",
"graphql-playground-middleware-express": "^1.7.8",
"mocha": "^5.2.0",
"morgan": "^1.9.1",
"serverless-offline": "^3.31.3",
"webpack": "^4.28.2",
"webpack-node-externals": "^1.7.2"
},
"dependencies": {
"apollo-server": "^2.3.1",
"apollo-server-express": "^2.3.1",
"apollo-server-lambda": "^2.3.1",
"bcryptjs": "^2.4.3",
"cors": "^2.8.4",
"dataloader": "^1.4.0",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"graphql": "^14.0.2",
"graphql-resolvers": "^0.2.2",
"jsonwebtoken": "^8.3.0",
"mongoose": "^5.4.0",
"serverless": "^1.35.1",
"serverless-http": "^1.8.0",
"serverless-webpack": "^5.2.0"
}
Serverless.yml
service: abc
tenant: adamzaczek
app: abc
plugins:
- serverless-webpack
- serverless-offline
custom:
webpack:
includeModules:
forceExclude:
- graphql-playground-middleware-express
- morgan
- "@babel/runtime"
webpackConfig: ./webpack.config.js
provider:
name: aws
runtime: nodejs8.12
stage: dev
credentials:
accessKeyId: totallyMyRealAccessKey
secretAccessKey: notASecretAnymore
region: us-east-1
functions:
graphql:
handler: index.handler
events:
- http:
path: graphql
method: post
cors: true
playground:
handler: index.handler
events:
- http:
path: playground
method: get
webpack.config.js
const path = require("path");
const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");
module.exports = {
entry: slsw.lib.entries,
target: "node",
mode: slsw.lib.webpack.isLocal ? "development" : "production",
optimization: {
minimize: false
},
performance: {
hints: false
},
devtool: "nosources-source-map",
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader"
}
]
}
]
},
output: {
libraryTarget: "commonjs2",
path: path.join(__dirname, ".webpack"),
filename: "[name].js",
sourceMapFilename: "[file].map"
}
};
OS: darwin Node Version: 8.12.0 Serverless Version: 1.35.1
Logs from cloudwatch
Unable to import module 'index': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/index.js:122:18)
at __webpack_require__ (/var/task/index.js:21:30)
at Module.<anonymous> (/var/task/index.js:220:21)
at __webpack_require__ (/var/task/index.js:21:30)
at /var/task/index.js:85:18
at Object.<anonymous> (/var/task/index.js:88:10)
Zip file looks ok, it has index.js, index.js.map, package.json and node_modules. I've been developing the app using sls offline and I never had any issues. Do you guys have any ideas what might be wrong?
I managed to fix the issue. For me it was .babelrc config file. I was missing node 8.10 target. This is my working .babelrc:
{
"presets": [["@babel/preset-env", { "targets": { "node": "8.10" } }]],
"plugins": ["@babel/plugin-transform-runtime"]
}