serverless-heaven / serverless-webpack

Serverless plugin to bundle your lambdas with Webpack
MIT License
1.72k stars 417 forks source link

AWS error: Unable to import module 'src/service/serviceName/handler': Error #383

Closed jvgeee closed 6 years ago

jvgeee commented 6 years ago

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.

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...).

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

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

HyperBrain commented 6 years ago

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?

HyperBrain commented 6 years ago

There's also an old issue with a discussion thread #230 .

I will check if I can get some better error analysis into the plugin, that might be able to find out missing modules while packaging. This would help do fix the configuration for these cases much easier.

HyperBrain commented 6 years ago

Can you try with "serverless-webpack": "github:serverless-heaven/serverless-webpack#improvement/better-missing-dep-errors" in your package.json? The version from that branch should error if any needed runtime dependency is found in devDependencies, so that configuration issues can be found easily.

jvgeee commented 6 years ago

Ah! That'll almost definitely be the issue, I moved a bunch of things from dependancies to devDependancies recently when doing a bunch of dependancy ipgrades and might have been a little brazen with it.

It's midnight here but I'll try again in the morning, cheers for the tip about the better branch! Really appreciate your thorough answers, thanks for that. Will report back once I've had a look.

jvgeee commented 6 years ago

@HyperBrain The better-missing-dep-errors branch helped a lot! You were right about this being the issue, cheers for your help.

RALifeCoach commented 6 years ago

The 'better-missing-dep-errors' brnach no longer exists. Is there another option for me? I am using "serverless-webpack": "^5.1.5".

 "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "serverless": "^1.27.3",
    "serverless-offline": "^3.25.4",
    "serverless-webpack": "^5.1.5",
    "webpack": "^3.8.1",
    "webpack-node-externals": "^1.6.0"
  },
  "dependencies": {
    "aws-sdk": "^2.264.1",
    "babel-polyfill": "^6.26.0",
    "babel-runtime": "^6.26.0",
    "sequelize": "^4.38.0",
    "sqlite3": "^4.0.1"
  }
HyperBrain commented 6 years ago

Use master or the latest version

Christopher Oliphant notifications@github.com schrieb am Mi., 27. Juni 2018, 3:44 nachm.:

The 'better-missing-dep-errors' brnach no longer exists. Is there another option for me?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/serverless-heaven/serverless-webpack/issues/383#issuecomment-400676432, or mute the thread https://github.com/notifications/unsubscribe-auth/AFRM3oaDiQWebJ4aPyXRVdWnK-_w2WXsks5uA4wwgaJpZM4TvDdU .

HyperBrain commented 6 years ago

Just checked. The fix is available in the current 5.1.5

jamesdixon commented 6 years ago

@HyperBrain I can open a new issue, but figured I'd piggy back on this one as I'm running into the same thing. I've checked that everything is good in devDependencies/dependencies and tried sls package and haven't seen any errors, yet I get the following error:

Unable to import module 'lib/createCampaign/index': Error at Object.<anonymous> (/var/task/lib/createCampaign/index.js:80264:19)

Here's my webpack config (note that I have zero experience with webpack until this, so maybe it's a config error):

const slsw = require('serverless-webpack')
const Webpack = require('webpack')

module.exports = {
  entry: slsw.lib.entries,
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  target: 'node',
  devtool: 'source-map',
  optimization: {
    minimize: false
  },
  plugins: [
    // https://github.com/tj/consolidate.js/issues/295
    new Webpack.IgnorePlugin(/(atpl|bracket|dot|doT.js|dust|dustjs-linkedin|eco|ect|ejs|haml|haml-coffee|hamlet|hiredis|handlebars|hogan|htmling|jazz|jqtpl|just|liquor|lodash|marko|mote|mustache|nunjucks|plates|QEJS|ractive|react|slm|swig|swig|teacup|templayed|twig|liquid|toffee|underscore|vash|walrus|whiskers)/),
    // https://github.com/tgriesser/knex/issues/1446
    new Webpack.NormalModuleReplacementPlugin(/\.\.\/migrate/, '../util/noop.js'),
    new Webpack.NormalModuleReplacementPlugin(/\.\.\/seed/, '../util/noop.js')
  ]
}

Here's my package.json:

{
  "scripts": {
    "test": "ava -v -s",
    "coverage": "nyc npm test"
  },
  "devDependencies": {
    "ava": "^0.25.0",
    "aws-sdk": "^2.269.1",
    "aws-sdk-mock": "^4.0.0",
    "dotenv": "^6.0.0",
    "faker": "^4.1.0",
    "knex-cleaner": "^1.1.4",
    "lambda-tester": "^3.4.1",
    "lodash.times": "^4.3.2",
    "proxyquire": "^2.0.1",
    "serverless-offline": "^3.25.5",
    "serverless-plugin-deploy-environment": "^1.1.0",
    "serverless-webpack": "^5.2.0",
    "sinon": "^6.1.0",
    "standard": "^11.0.1",
    "webpack": "^4.15.1"
  },
  "dependencies": {
    "@sendgrid/client": "^6.3.0",
    "@sendgrid/mail": "^6.3.1",
    "cache-pug-templates": "^0.0.7",
    "email-templates": "^4.0.1",
    "joi": "^13.4.0",
    "knex": "^0.15.0",
    "lodash.camelcase": "^4.3.0",
    "lodash.chunk": "^4.2.0",
    "lodash.get": "^4.4.2",
    "lodash.groupby": "^4.6.0",
    "lodash.orderby": "^4.6.0",
    "lodash.pickby": "^4.6.0",
    "lodash.startcase": "^4.4.0",
    "lodash.uniqby": "^4.7.0",
    "moment": "^2.22.2",
    "pg": "^7.4.3",
    "pug": "^2.0.3",
    "redis": "^2.8.0",
    "rollbar": "^2.4.2",
    "rrule": "^2.2.9"
  },
  "ava": {
    "require": [
      "./dotenv",
      "dotenv/config"
    ]
  }
}

and finally my serverless.yml:

service: email-service

provider:
  name: aws
  runtime: nodejs8.10
  region: us-east-2

package:
  individually: true

functions:
  sendEmail:
    handler: lib/sendEmail/index.handler
    events:
      - sqs: 
          arn: ${self:deployVariables.SEND_EMAIL_SQS_ARN}
          enabled: true
          batchSize: 1
  createEmail:
    handler: lib/createEmail/index.handler
    events:
      - sqs: 
          arn: ${self:deployVariables.CREATE_EMAIL_SQS_ARN}
          enabled: true
          batchSize: 1
  createCampaign:
    handler: lib/createCampaign/index.handler
    events:
      - sqs: 
          arn: ${self:deployVariables.CREATE_CAMPAIGN_SQS_ARN}
          enabled: true
          batchSize: 1
  sendGridEvent:
    handler: lib/sendGridEvent/index.handler
    events:
      - http:
          path: /sendGridEvent
          method: post

custom:
  deploy:
    environments: ${file(config/env.yml)}
    variables: ${file(config/variables.yml)}
  webpack:
    includeModules: false
    packager: 'yarn'

plugins:
  - serverless-offline
  - serverless-plugin-deploy-environment
  - serverless-webpack

I really appreciate any insights you can provide!

jamesdixon commented 6 years ago

I think I was able to get this working using nodeExternals()

alatras commented 5 years ago

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?

jorenvh1 commented 5 years ago

Having the same issue...

seunlanlege commented 5 years ago

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

lukebyrne commented 5 years ago

Duplicate of https://github.com/serverless-heaven/serverless-webpack/issues/43#issuecomment-486041997

ThomasEg commented 5 years ago

To the future me in another dimension, don't also waste a day trawling Google like i did. Instead follow these very simple steps to track down the missing dependency. The situation is that your function works just fine locally, but published to AWS you are getting an error similar to this in CloudWatch:

Unable to import module 'whatshamicallit': 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/node_modules/sequelize-typescript/lib/utils/versioning.js:3:17)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)

...the clue is to look at the point(s) in the callstack with something with "node_modules", in this case have a look at that file in your project, node_modules/sequelize-typescript/lib/utils/versioning.js, on line 3:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var sequelize = require("sequelize");

[...cut...]

... the culprit is the module "sequelize" in this case... then add this to your serverless.yml:

  webpack:
    webpackConfig: ./webpack.config.js
    includeModules:
      forceInclude:
        - sequelize

...republish and try again....profit!

(Some of you guys properly think this i very basic, and you are properly right... but i wasted a lot of time tracking this down and i was hoping to spare someone else the hassle...)

HyperBrain commented 5 years ago

Hi @ThomasEg , nice catch 👍 . I remember, that especially sequelize already occurred multiple times. If you don't mind, you could add this to the README (plz submit a PR), so that other ppl can solve this quicker ;-)