Webpack 4 + Jest + Babel 7 (+AWS Lambda): tests pass locally, error when deployed to AWS Lambda












0















Upgrading to Webpack 4 + Jest 23 + Babel 7 and have run into some issues.



Error only when deployed to AWS Lambda.



{
"errorMessage": "Handler 'handler' missing on module 'index'"
}


Jest tests pass locally. Assume Jest is building the code differently from what npm run build is doing which is why. Switching from import statements to require() fixes the error message. So presumably this is a build config problem, need another pair of eyes.



If you have SAM Local installed, can reproduce this locally with:



git clone https://git@github.com/buildbreakdo/lambda-starter
cd lambda-starter
npm install
npm start
/usr/bin/open -a "/Applications/Google Chrome.app" 'http://127.0.0.1:5000/'


Thanks for the assist.



Handler is set to:



enter image description here



package.json



{
"name": "aws-api-lambda",
"version": "1.0.0",
"description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
"main": "build/index.js",
"scripts": {
"build": "NODE_ENV=production webpack --display-error-details --display-modules",
"watch": "webpack --watch",
"test": "jest --config ./jest.config.js",
"test:watch": "jest --watch --config ./jest.config.js",
"start": "sam local start-api --port 5000",
"dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
"update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
"update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
"deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
"deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
},
"repository": {
"type": "git",
"url": "git+https://github.com/buildbreakdo/aws-api-lambda.git"
},
"keywords": [
"starter",
"starter-kit",
"aws-api-gateway",
"aws-lambda"
],
"author": "Your Name Here",
"bugs": {
"url": "https://github.com/buildbreakdo/aws-api-lambda/issues"
},
"homepage": "https://github.com/buildbreakdo/aws-api-lambda#readme",
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^8.0.4",
"jest": "^23.6.0",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2"
},
"dependencies": {
"cross-fetch": "^2.2.3"
}
}


.babelrc



{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "8.10"
}
}
]
]
}


webpack.config.js



const path = require('path');
const webpack = require('webpack');

module.exports = {
optimization: {
minimize: false
},
target: 'node',
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
entry: [
path.join(__dirname, 'src/index.js')
],
output: {
path: path.resolve(__dirname, 'build'),
filename: 'index.js',
libraryTarget: 'commonjs2'
},
plugins: [
new webpack.IgnorePlugin(/^pg-native$/),
new webpack.DefinePlugin({
'process.env.BROWSER': false,
__DEV__: process.env.NODE_ENV !== 'production',
}),
],
module: {
rules: [
{
test: /.(mjs|js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
}
};


src/index.js



import fetch from 'cross-fetch';

exports.handler = async (event, context, callback) => {
const request = fetch('https://google.com', {
method: 'HEAD'
});

let data;
try {
const response = await request;

data = {
url: response.url,
status: response.status,
statusText: response.statusText
};
} catch (e) {
console.log(e);
}

return callback(null, {
statusCode: 200,
header: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}


build/index.js



https://github.com/buildbreakdo/lambda-starter/blob/master/build/index.js



Repository:
https://github.com/buildbreakdo/lambda-starter










share|improve this question





























    0















    Upgrading to Webpack 4 + Jest 23 + Babel 7 and have run into some issues.



    Error only when deployed to AWS Lambda.



    {
    "errorMessage": "Handler 'handler' missing on module 'index'"
    }


    Jest tests pass locally. Assume Jest is building the code differently from what npm run build is doing which is why. Switching from import statements to require() fixes the error message. So presumably this is a build config problem, need another pair of eyes.



    If you have SAM Local installed, can reproduce this locally with:



    git clone https://git@github.com/buildbreakdo/lambda-starter
    cd lambda-starter
    npm install
    npm start
    /usr/bin/open -a "/Applications/Google Chrome.app" 'http://127.0.0.1:5000/'


    Thanks for the assist.



    Handler is set to:



    enter image description here



    package.json



    {
    "name": "aws-api-lambda",
    "version": "1.0.0",
    "description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
    "main": "build/index.js",
    "scripts": {
    "build": "NODE_ENV=production webpack --display-error-details --display-modules",
    "watch": "webpack --watch",
    "test": "jest --config ./jest.config.js",
    "test:watch": "jest --watch --config ./jest.config.js",
    "start": "sam local start-api --port 5000",
    "dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
    "update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
    "update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
    "deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
    "deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
    },
    "repository": {
    "type": "git",
    "url": "git+https://github.com/buildbreakdo/aws-api-lambda.git"
    },
    "keywords": [
    "starter",
    "starter-kit",
    "aws-api-gateway",
    "aws-lambda"
    ],
    "author": "Your Name Here",
    "bugs": {
    "url": "https://github.com/buildbreakdo/aws-api-lambda/issues"
    },
    "homepage": "https://github.com/buildbreakdo/aws-api-lambda#readme",
    "devDependencies": {
    "@babel/core": "^7.1.6",
    "@babel/preset-env": "^7.1.6",
    "babel-core": "^7.0.0-bridge.0",
    "babel-loader": "^8.0.4",
    "jest": "^23.6.0",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.2"
    },
    "dependencies": {
    "cross-fetch": "^2.2.3"
    }
    }


    .babelrc



    {
    "presets": [
    [
    "@babel/preset-env",
    {
    "targets": {
    "node": "8.10"
    }
    }
    ]
    ]
    }


    webpack.config.js



    const path = require('path');
    const webpack = require('webpack');

    module.exports = {
    optimization: {
    minimize: false
    },
    target: 'node',
    mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
    entry: [
    path.join(__dirname, 'src/index.js')
    ],
    output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'index.js',
    libraryTarget: 'commonjs2'
    },
    plugins: [
    new webpack.IgnorePlugin(/^pg-native$/),
    new webpack.DefinePlugin({
    'process.env.BROWSER': false,
    __DEV__: process.env.NODE_ENV !== 'production',
    }),
    ],
    module: {
    rules: [
    {
    test: /.(mjs|js|jsx)$/,
    exclude: /node_modules/,
    use: ['babel-loader']
    }
    ]
    }
    };


    src/index.js



    import fetch from 'cross-fetch';

    exports.handler = async (event, context, callback) => {
    const request = fetch('https://google.com', {
    method: 'HEAD'
    });

    let data;
    try {
    const response = await request;

    data = {
    url: response.url,
    status: response.status,
    statusText: response.statusText
    };
    } catch (e) {
    console.log(e);
    }

    return callback(null, {
    statusCode: 200,
    header: {
    'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
    });
    }


    build/index.js



    https://github.com/buildbreakdo/lambda-starter/blob/master/build/index.js



    Repository:
    https://github.com/buildbreakdo/lambda-starter










    share|improve this question



























      0












      0








      0








      Upgrading to Webpack 4 + Jest 23 + Babel 7 and have run into some issues.



      Error only when deployed to AWS Lambda.



      {
      "errorMessage": "Handler 'handler' missing on module 'index'"
      }


      Jest tests pass locally. Assume Jest is building the code differently from what npm run build is doing which is why. Switching from import statements to require() fixes the error message. So presumably this is a build config problem, need another pair of eyes.



      If you have SAM Local installed, can reproduce this locally with:



      git clone https://git@github.com/buildbreakdo/lambda-starter
      cd lambda-starter
      npm install
      npm start
      /usr/bin/open -a "/Applications/Google Chrome.app" 'http://127.0.0.1:5000/'


      Thanks for the assist.



      Handler is set to:



      enter image description here



      package.json



      {
      "name": "aws-api-lambda",
      "version": "1.0.0",
      "description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
      "main": "build/index.js",
      "scripts": {
      "build": "NODE_ENV=production webpack --display-error-details --display-modules",
      "watch": "webpack --watch",
      "test": "jest --config ./jest.config.js",
      "test:watch": "jest --watch --config ./jest.config.js",
      "start": "sam local start-api --port 5000",
      "dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
      "update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
      "update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
      "deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
      "deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
      },
      "repository": {
      "type": "git",
      "url": "git+https://github.com/buildbreakdo/aws-api-lambda.git"
      },
      "keywords": [
      "starter",
      "starter-kit",
      "aws-api-gateway",
      "aws-lambda"
      ],
      "author": "Your Name Here",
      "bugs": {
      "url": "https://github.com/buildbreakdo/aws-api-lambda/issues"
      },
      "homepage": "https://github.com/buildbreakdo/aws-api-lambda#readme",
      "devDependencies": {
      "@babel/core": "^7.1.6",
      "@babel/preset-env": "^7.1.6",
      "babel-core": "^7.0.0-bridge.0",
      "babel-loader": "^8.0.4",
      "jest": "^23.6.0",
      "webpack": "^4.20.2",
      "webpack-cli": "^3.1.2"
      },
      "dependencies": {
      "cross-fetch": "^2.2.3"
      }
      }


      .babelrc



      {
      "presets": [
      [
      "@babel/preset-env",
      {
      "targets": {
      "node": "8.10"
      }
      }
      ]
      ]
      }


      webpack.config.js



      const path = require('path');
      const webpack = require('webpack');

      module.exports = {
      optimization: {
      minimize: false
      },
      target: 'node',
      mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
      entry: [
      path.join(__dirname, 'src/index.js')
      ],
      output: {
      path: path.resolve(__dirname, 'build'),
      filename: 'index.js',
      libraryTarget: 'commonjs2'
      },
      plugins: [
      new webpack.IgnorePlugin(/^pg-native$/),
      new webpack.DefinePlugin({
      'process.env.BROWSER': false,
      __DEV__: process.env.NODE_ENV !== 'production',
      }),
      ],
      module: {
      rules: [
      {
      test: /.(mjs|js|jsx)$/,
      exclude: /node_modules/,
      use: ['babel-loader']
      }
      ]
      }
      };


      src/index.js



      import fetch from 'cross-fetch';

      exports.handler = async (event, context, callback) => {
      const request = fetch('https://google.com', {
      method: 'HEAD'
      });

      let data;
      try {
      const response = await request;

      data = {
      url: response.url,
      status: response.status,
      statusText: response.statusText
      };
      } catch (e) {
      console.log(e);
      }

      return callback(null, {
      statusCode: 200,
      header: {
      'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
      });
      }


      build/index.js



      https://github.com/buildbreakdo/lambda-starter/blob/master/build/index.js



      Repository:
      https://github.com/buildbreakdo/lambda-starter










      share|improve this question
















      Upgrading to Webpack 4 + Jest 23 + Babel 7 and have run into some issues.



      Error only when deployed to AWS Lambda.



      {
      "errorMessage": "Handler 'handler' missing on module 'index'"
      }


      Jest tests pass locally. Assume Jest is building the code differently from what npm run build is doing which is why. Switching from import statements to require() fixes the error message. So presumably this is a build config problem, need another pair of eyes.



      If you have SAM Local installed, can reproduce this locally with:



      git clone https://git@github.com/buildbreakdo/lambda-starter
      cd lambda-starter
      npm install
      npm start
      /usr/bin/open -a "/Applications/Google Chrome.app" 'http://127.0.0.1:5000/'


      Thanks for the assist.



      Handler is set to:



      enter image description here



      package.json



      {
      "name": "aws-api-lambda",
      "version": "1.0.0",
      "description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
      "main": "build/index.js",
      "scripts": {
      "build": "NODE_ENV=production webpack --display-error-details --display-modules",
      "watch": "webpack --watch",
      "test": "jest --config ./jest.config.js",
      "test:watch": "jest --watch --config ./jest.config.js",
      "start": "sam local start-api --port 5000",
      "dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
      "update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
      "update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
      "deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
      "deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
      },
      "repository": {
      "type": "git",
      "url": "git+https://github.com/buildbreakdo/aws-api-lambda.git"
      },
      "keywords": [
      "starter",
      "starter-kit",
      "aws-api-gateway",
      "aws-lambda"
      ],
      "author": "Your Name Here",
      "bugs": {
      "url": "https://github.com/buildbreakdo/aws-api-lambda/issues"
      },
      "homepage": "https://github.com/buildbreakdo/aws-api-lambda#readme",
      "devDependencies": {
      "@babel/core": "^7.1.6",
      "@babel/preset-env": "^7.1.6",
      "babel-core": "^7.0.0-bridge.0",
      "babel-loader": "^8.0.4",
      "jest": "^23.6.0",
      "webpack": "^4.20.2",
      "webpack-cli": "^3.1.2"
      },
      "dependencies": {
      "cross-fetch": "^2.2.3"
      }
      }


      .babelrc



      {
      "presets": [
      [
      "@babel/preset-env",
      {
      "targets": {
      "node": "8.10"
      }
      }
      ]
      ]
      }


      webpack.config.js



      const path = require('path');
      const webpack = require('webpack');

      module.exports = {
      optimization: {
      minimize: false
      },
      target: 'node',
      mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
      entry: [
      path.join(__dirname, 'src/index.js')
      ],
      output: {
      path: path.resolve(__dirname, 'build'),
      filename: 'index.js',
      libraryTarget: 'commonjs2'
      },
      plugins: [
      new webpack.IgnorePlugin(/^pg-native$/),
      new webpack.DefinePlugin({
      'process.env.BROWSER': false,
      __DEV__: process.env.NODE_ENV !== 'production',
      }),
      ],
      module: {
      rules: [
      {
      test: /.(mjs|js|jsx)$/,
      exclude: /node_modules/,
      use: ['babel-loader']
      }
      ]
      }
      };


      src/index.js



      import fetch from 'cross-fetch';

      exports.handler = async (event, context, callback) => {
      const request = fetch('https://google.com', {
      method: 'HEAD'
      });

      let data;
      try {
      const response = await request;

      data = {
      url: response.url,
      status: response.status,
      statusText: response.statusText
      };
      } catch (e) {
      console.log(e);
      }

      return callback(null, {
      statusCode: 200,
      header: {
      'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
      });
      }


      build/index.js



      https://github.com/buildbreakdo/lambda-starter/blob/master/build/index.js



      Repository:
      https://github.com/buildbreakdo/lambda-starter







      amazon-web-services webpack jestjs babel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 10:24









      skyboyer

      3,53111128




      3,53111128










      asked Nov 22 '18 at 2:19









      Joshua RobinsonJoshua Robinson

      1,4141623




      1,4141623
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Finally have this working. Babel 7 + Jest 23.6.0 + Webpack 4 + AWS Lambda. There was a AWS Lambda outage today too, so I am not even sure there is a difference between what is above (too tired to check!) but this works. Repo here: https://github.com/buildbreakdo/lambda-starter



          .babelrc



          {
          "presets": [
          [
          "@babel/preset-env",
          {
          "targets": {
          "node": "8.10"
          }
          }
          ]
          ]
          }


          package.json



          {
          "name": "lambda-starter",
          "version": "1.0.0",
          "description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
          "main": "build/index.js",
          "scripts": {
          "build": "NODE_ENV=production webpack --display-error-details --display-modules",
          "watch": "webpack --watch",
          "test": "jest --config ./jest.config.js",
          "test:watch": "jest --watch --config ./jest.config.js",
          "start": "sam local start-api --port 5000",
          "dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
          "update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
          "update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
          "deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
          "deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
          },
          "repository": {
          "type": "git",
          "url": "git+https://github.com/buildbreakdo/lambda-starter.git"
          },
          "keywords": [
          "starter",
          "starter-kit",
          "aws-api-gateway",
          "aws-lambda"
          ],
          "author": "Your Name Here",
          "bugs": {
          "url": "https://github.com/buildbreakdo/lambda-starter/issues"
          },
          "homepage": "https://github.com/buildbreakdo/lambda-starter#readme",
          "devDependencies": {
          "@babel/core": "^7.1.6",
          "@babel/preset-env": "^7.1.6",
          "babel-core": "^7.0.0-bridge.0",
          "babel-jest": "^23.6.0",
          "babel-loader": "^7.1.4",
          "jest": "^23.6.0",
          "jest-cli": "^23.6.0",
          "webpack": "^4.8.1",
          "webpack-cli": "^2.0.11"
          },
          "dependencies": {
          "node-fetch": "^2.3.0"
          }
          }


          webpack.config.js



          const path = require('path');
          const webpack = require('webpack');

          module.exports = {
          target: 'node',
          mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
          entry: [ './src/index.js' ],
          output: {
          path: path.resolve(__dirname, 'build'),
          filename: 'index.js',
          library: 'index',
          libraryTarget: 'commonjs2'
          },
          plugins: [
          new webpack.IgnorePlugin(/^pg-native$/),
          new webpack.DefinePlugin({
          'process.env.BROWSER': false,
          __DEV__: process.env.NODE_ENV !== 'production',
          }),
          ],
          module: {
          rules: [
          {
          test: /.(mjs|js|jsx)$/,
          exclude: /node_modules/,
          loader: 'babel-loader'
          }
          ],
          }
          };





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423011%2fwebpack-4-jest-babel-7-aws-lambda-tests-pass-locally-error-when-deploye%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Finally have this working. Babel 7 + Jest 23.6.0 + Webpack 4 + AWS Lambda. There was a AWS Lambda outage today too, so I am not even sure there is a difference between what is above (too tired to check!) but this works. Repo here: https://github.com/buildbreakdo/lambda-starter



            .babelrc



            {
            "presets": [
            [
            "@babel/preset-env",
            {
            "targets": {
            "node": "8.10"
            }
            }
            ]
            ]
            }


            package.json



            {
            "name": "lambda-starter",
            "version": "1.0.0",
            "description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
            "main": "build/index.js",
            "scripts": {
            "build": "NODE_ENV=production webpack --display-error-details --display-modules",
            "watch": "webpack --watch",
            "test": "jest --config ./jest.config.js",
            "test:watch": "jest --watch --config ./jest.config.js",
            "start": "sam local start-api --port 5000",
            "dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
            "update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
            "update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
            "deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
            "deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
            },
            "repository": {
            "type": "git",
            "url": "git+https://github.com/buildbreakdo/lambda-starter.git"
            },
            "keywords": [
            "starter",
            "starter-kit",
            "aws-api-gateway",
            "aws-lambda"
            ],
            "author": "Your Name Here",
            "bugs": {
            "url": "https://github.com/buildbreakdo/lambda-starter/issues"
            },
            "homepage": "https://github.com/buildbreakdo/lambda-starter#readme",
            "devDependencies": {
            "@babel/core": "^7.1.6",
            "@babel/preset-env": "^7.1.6",
            "babel-core": "^7.0.0-bridge.0",
            "babel-jest": "^23.6.0",
            "babel-loader": "^7.1.4",
            "jest": "^23.6.0",
            "jest-cli": "^23.6.0",
            "webpack": "^4.8.1",
            "webpack-cli": "^2.0.11"
            },
            "dependencies": {
            "node-fetch": "^2.3.0"
            }
            }


            webpack.config.js



            const path = require('path');
            const webpack = require('webpack');

            module.exports = {
            target: 'node',
            mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
            entry: [ './src/index.js' ],
            output: {
            path: path.resolve(__dirname, 'build'),
            filename: 'index.js',
            library: 'index',
            libraryTarget: 'commonjs2'
            },
            plugins: [
            new webpack.IgnorePlugin(/^pg-native$/),
            new webpack.DefinePlugin({
            'process.env.BROWSER': false,
            __DEV__: process.env.NODE_ENV !== 'production',
            }),
            ],
            module: {
            rules: [
            {
            test: /.(mjs|js|jsx)$/,
            exclude: /node_modules/,
            loader: 'babel-loader'
            }
            ],
            }
            };





            share|improve this answer




























              0














              Finally have this working. Babel 7 + Jest 23.6.0 + Webpack 4 + AWS Lambda. There was a AWS Lambda outage today too, so I am not even sure there is a difference between what is above (too tired to check!) but this works. Repo here: https://github.com/buildbreakdo/lambda-starter



              .babelrc



              {
              "presets": [
              [
              "@babel/preset-env",
              {
              "targets": {
              "node": "8.10"
              }
              }
              ]
              ]
              }


              package.json



              {
              "name": "lambda-starter",
              "version": "1.0.0",
              "description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
              "main": "build/index.js",
              "scripts": {
              "build": "NODE_ENV=production webpack --display-error-details --display-modules",
              "watch": "webpack --watch",
              "test": "jest --config ./jest.config.js",
              "test:watch": "jest --watch --config ./jest.config.js",
              "start": "sam local start-api --port 5000",
              "dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
              "update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
              "update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
              "deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
              "deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
              },
              "repository": {
              "type": "git",
              "url": "git+https://github.com/buildbreakdo/lambda-starter.git"
              },
              "keywords": [
              "starter",
              "starter-kit",
              "aws-api-gateway",
              "aws-lambda"
              ],
              "author": "Your Name Here",
              "bugs": {
              "url": "https://github.com/buildbreakdo/lambda-starter/issues"
              },
              "homepage": "https://github.com/buildbreakdo/lambda-starter#readme",
              "devDependencies": {
              "@babel/core": "^7.1.6",
              "@babel/preset-env": "^7.1.6",
              "babel-core": "^7.0.0-bridge.0",
              "babel-jest": "^23.6.0",
              "babel-loader": "^7.1.4",
              "jest": "^23.6.0",
              "jest-cli": "^23.6.0",
              "webpack": "^4.8.1",
              "webpack-cli": "^2.0.11"
              },
              "dependencies": {
              "node-fetch": "^2.3.0"
              }
              }


              webpack.config.js



              const path = require('path');
              const webpack = require('webpack');

              module.exports = {
              target: 'node',
              mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
              entry: [ './src/index.js' ],
              output: {
              path: path.resolve(__dirname, 'build'),
              filename: 'index.js',
              library: 'index',
              libraryTarget: 'commonjs2'
              },
              plugins: [
              new webpack.IgnorePlugin(/^pg-native$/),
              new webpack.DefinePlugin({
              'process.env.BROWSER': false,
              __DEV__: process.env.NODE_ENV !== 'production',
              }),
              ],
              module: {
              rules: [
              {
              test: /.(mjs|js|jsx)$/,
              exclude: /node_modules/,
              loader: 'babel-loader'
              }
              ],
              }
              };





              share|improve this answer


























                0












                0








                0







                Finally have this working. Babel 7 + Jest 23.6.0 + Webpack 4 + AWS Lambda. There was a AWS Lambda outage today too, so I am not even sure there is a difference between what is above (too tired to check!) but this works. Repo here: https://github.com/buildbreakdo/lambda-starter



                .babelrc



                {
                "presets": [
                [
                "@babel/preset-env",
                {
                "targets": {
                "node": "8.10"
                }
                }
                ]
                ]
                }


                package.json



                {
                "name": "lambda-starter",
                "version": "1.0.0",
                "description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
                "main": "build/index.js",
                "scripts": {
                "build": "NODE_ENV=production webpack --display-error-details --display-modules",
                "watch": "webpack --watch",
                "test": "jest --config ./jest.config.js",
                "test:watch": "jest --watch --config ./jest.config.js",
                "start": "sam local start-api --port 5000",
                "dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
                "update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
                "update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
                "deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
                "deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
                },
                "repository": {
                "type": "git",
                "url": "git+https://github.com/buildbreakdo/lambda-starter.git"
                },
                "keywords": [
                "starter",
                "starter-kit",
                "aws-api-gateway",
                "aws-lambda"
                ],
                "author": "Your Name Here",
                "bugs": {
                "url": "https://github.com/buildbreakdo/lambda-starter/issues"
                },
                "homepage": "https://github.com/buildbreakdo/lambda-starter#readme",
                "devDependencies": {
                "@babel/core": "^7.1.6",
                "@babel/preset-env": "^7.1.6",
                "babel-core": "^7.0.0-bridge.0",
                "babel-jest": "^23.6.0",
                "babel-loader": "^7.1.4",
                "jest": "^23.6.0",
                "jest-cli": "^23.6.0",
                "webpack": "^4.8.1",
                "webpack-cli": "^2.0.11"
                },
                "dependencies": {
                "node-fetch": "^2.3.0"
                }
                }


                webpack.config.js



                const path = require('path');
                const webpack = require('webpack');

                module.exports = {
                target: 'node',
                mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
                entry: [ './src/index.js' ],
                output: {
                path: path.resolve(__dirname, 'build'),
                filename: 'index.js',
                library: 'index',
                libraryTarget: 'commonjs2'
                },
                plugins: [
                new webpack.IgnorePlugin(/^pg-native$/),
                new webpack.DefinePlugin({
                'process.env.BROWSER': false,
                __DEV__: process.env.NODE_ENV !== 'production',
                }),
                ],
                module: {
                rules: [
                {
                test: /.(mjs|js|jsx)$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
                }
                ],
                }
                };





                share|improve this answer













                Finally have this working. Babel 7 + Jest 23.6.0 + Webpack 4 + AWS Lambda. There was a AWS Lambda outage today too, so I am not even sure there is a difference between what is above (too tired to check!) but this works. Repo here: https://github.com/buildbreakdo/lambda-starter



                .babelrc



                {
                "presets": [
                [
                "@babel/preset-env",
                {
                "targets": {
                "node": "8.10"
                }
                }
                ]
                ]
                }


                package.json



                {
                "name": "lambda-starter",
                "version": "1.0.0",
                "description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
                "main": "build/index.js",
                "scripts": {
                "build": "NODE_ENV=production webpack --display-error-details --display-modules",
                "watch": "webpack --watch",
                "test": "jest --config ./jest.config.js",
                "test:watch": "jest --watch --config ./jest.config.js",
                "start": "sam local start-api --port 5000",
                "dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
                "update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
                "update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
                "deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
                "deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
                },
                "repository": {
                "type": "git",
                "url": "git+https://github.com/buildbreakdo/lambda-starter.git"
                },
                "keywords": [
                "starter",
                "starter-kit",
                "aws-api-gateway",
                "aws-lambda"
                ],
                "author": "Your Name Here",
                "bugs": {
                "url": "https://github.com/buildbreakdo/lambda-starter/issues"
                },
                "homepage": "https://github.com/buildbreakdo/lambda-starter#readme",
                "devDependencies": {
                "@babel/core": "^7.1.6",
                "@babel/preset-env": "^7.1.6",
                "babel-core": "^7.0.0-bridge.0",
                "babel-jest": "^23.6.0",
                "babel-loader": "^7.1.4",
                "jest": "^23.6.0",
                "jest-cli": "^23.6.0",
                "webpack": "^4.8.1",
                "webpack-cli": "^2.0.11"
                },
                "dependencies": {
                "node-fetch": "^2.3.0"
                }
                }


                webpack.config.js



                const path = require('path');
                const webpack = require('webpack');

                module.exports = {
                target: 'node',
                mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
                entry: [ './src/index.js' ],
                output: {
                path: path.resolve(__dirname, 'build'),
                filename: 'index.js',
                library: 'index',
                libraryTarget: 'commonjs2'
                },
                plugins: [
                new webpack.IgnorePlugin(/^pg-native$/),
                new webpack.DefinePlugin({
                'process.env.BROWSER': false,
                __DEV__: process.env.NODE_ENV !== 'production',
                }),
                ],
                module: {
                rules: [
                {
                test: /.(mjs|js|jsx)$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
                }
                ],
                }
                };






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 6:01









                Joshua RobinsonJoshua Robinson

                1,4141623




                1,4141623






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423011%2fwebpack-4-jest-babel-7-aws-lambda-tests-pass-locally-error-when-deploye%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga