首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack 4+ Jest + Babel 7 (+AWS Lambda):测试在本地通过,部署到AWS Lambda时出错

Webpack 4+ Jest + Babel 7 (+AWS Lambda):测试在本地通过,部署到AWS Lambda时出错
EN

Stack Overflow用户
提问于 2018-11-22 02:19:43
回答 1查看 1.3K关注 0票数 2

升级到Webpack 4+ Jest 23 + Babel 7,遇到了一些问题。

仅当部署到AWS Lambda时发生错误。

代码语言:javascript
复制
{
  "errorMessage": "Handler 'handler' missing on module 'index'"
}

开玩笑的考试在当地通过。假设Jest构建代码的方式与npm run build所做的不同,这就是原因所在。从import语句切换到require()修复错误消息。所以这大概是一个构建配置问题,需要另一双眼睛。

如果安装了SAM,可以通过以下方式在本地复制此文件:

代码语言:javascript
复制
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/'

谢谢你的协助。

处理程序设置为:

package.json

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "8.10"
        }
      }
    ]
  ]
}

webpack.config.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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

存储库:https://github.com/buildbreakdo/lambda-starter

EN

回答 1

Stack Overflow用户

发布于 2018-11-22 06:01:03

终于起作用了。巴贝尔7+ Jest 23.6.0 + Webpack 4+ AWS Lambda。今天也发生了AWS Lambda故障,所以我甚至不确定上面的内容有什么不同(太累了,无法检查!)但这个很管用。这里的回购:https://github.com/buildbreakdo/lambda-starter

.babelrc

代码语言:javascript
复制
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "8.10"
        }
      }
    ]
  ]
}

package.json

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
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'
      }
    ],
  }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53423011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档