serverless-webpack: AWS error: Unable to import module 'src/service/serviceName/handler': Error

This is a Bug Report

Description

This vague issue is mentioned in other bug reports, so apologies if it’s a duplicate, but while the error code is the same those issues/resolutions seem unrelated to what’s happening to me.

  • What went wrong?

When deploying to AWS and trying to access my lambdas, I’m getting:

Unable to import module 'src/service/authentication/handler': Error

This is the only error I see (no stacktrace…).

  • What was the config you used? serverless.yml:
service: ${opt:service, 'OurApplicationName'}

frameworkVersion: '>=1.0.0 <2.0.0'

provider:
  name: aws
  runtime: nodejs8.10
  stage: ${opt:stage, 'dev'}

  region: 'ap-southeast-2'
  role: '${self:custom.roles.default}'

  deploymentBucket:
    name: '${self:provider.stage}-${self:service}-deployment-bucket'
    serverSideEncryption: 'AES256'

  # VPC configuration per environment (dev, qa, prod)
  # This is required for deployment, but not during Lambda execution.
  vpc: '${file(./serverless-vpc-config.yml):${opt:stage, self:provider.stage}}'

  environment: '${file(.env.${opt:stage, self:provider.stage}.yml)}'

  iamRoleStatements:
    - Effect: 'Allow'
      Action:
        - 'lambda:InvokeFunction'
      Resource:
      - Fn::Join:
        - ':'
        - - arn:aws:lambda
          - Ref: AWS::Region
          - Ref: AWS::AccountId
          - function:${self:service}-${opt:stage, self:provider.stage}-*


functions:
  ${file(resource/serverlessHelper.js):aggregatedFunctions}

plugins:
  - serverless-swagger-documentation
  - serverless-webpack
  - serverless-dynamodb-local
  - serverless-s3-local
  - serverless-offline
  - serverless-offline-scheduler
  - serverless-domain-manager

resources:
  Resources:
    '${file(resource/serverlessHelper.js):cloudformationResources}'

custom:
   # A bunch of dynamoDB seeding stuff here


  # serverless-webpack configuration
  webpack:
    includeModules: true

My folder structure is like so:

/
├── resource
│   └── // database initialization, swagger documentation tooling, authentication...
├── src
│   ├── service
│   │   └── profile
│   │       ├── handler.js
│   │       ├── profile.fn.yml
       └── // All services have their own folders in here
serverless.yml
webpack.config.js

My webpack.config.js:

var yaml = require('js-yaml')
var fs = require('fs')
var path = require('path')
var nodeExternals = require('webpack-node-externals')
const slsw = require('serverless-webpack');


module.exports = {
  entry: slsw.lib.entries,
  devtool: 'source-map',
  target: 'node',
  externals: [nodeExternals()],
  module: {
    rules: [{
      test: /\.js$/,
      loader: 'babel-loader',
      include: __dirname,
      exclude: /node_modules/
    }]
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.resolve(__dirname, '.webpack'),
    filename: '[name].js'
  }
};

Additional Data

  • Serverless-Webpack Version you’re using: 5.1.3
  • Webpack version you’re using: 3.5.5
  • Serverless Framework Version you’re using: 1.26.1

I get the feeling these errors are because of my folder structure? We’ve been on an old version of webpack/serverless webpack for the past year and these errors are happening since upgrading from serverless-webpack v2

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Hi @jascination . The “Unable to import module” error means that one of your dependencies is missing in the generated package and AWS cannot load/require the handler.

Can you check that you have everything needed in dependencies but not devDependencies? You can generate the packages with serverless package. They will be created in .serverless. It is the easiest way to check the dependencies in there. Especially source-map-support must be in the dependencies section. Maybe you can post your package.json?

having this same issue, how was anyone able to get it fixed?

I am importing a module I wrote in the handler. But it’s returning the same error mentioned here. Why? And how can I handle it?