我正在尝试将cognito用于我的项目,但是当我按照所有必要的设置解释时,我无法运行它并且在我的lamda测试控制台中我收到以下错误:
{
"errorMessage": "Cannot find module './lib/browser_loader'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:417:25)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)",
"Object.<anonymous> (/var/task/index.js:3651:1)",
"__webpack_require__ (/var/task/index.js:20:30)",
"webpackUniversalModuleDefinition (/var/task/index.js:149:28)",
"Object.<anonymous> (/var/task/index.js:156:3)",
"__webpack_require__ (/var/task/index.js:20:30)",
"Object.<anonymous> (/var/task/index.js:75:18)"
]
}
我按照以下链接中提到的说明进行操作:https://github.com/aws/amazon-cognito-identity-js
所以我使用webpack和json loader来首先转换代码。这是我的webpack代码:
{
"name": "bnitjouathprovider",
"version": "0.0.1",
"description": "provide the server for password management using cognito",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"author": "Hamed Minaee",
"license": "ISC",
"devDependencies": {
"amazon-cognito-identity-js": "^1.18.0",
"aws-sdk": "git://github.com/chrisradek/aws-sdk-js.git#webpack",
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"brfs": "^1.4.3",
"json-loader": "^0.5.4",
"transform-loader": "^0.2.4",
"webpack": "^3.0.0-rc.1"
},
"dependencies": {
"amazon-cognito-identity-js": "^1.18.0"
},
"babel": {
"presets": [
"es2015",
"stage-0"
]
}
}
这是我的webpack.config:
var path = require("path");
var DIST_DIR = path.resolve(__dirname, "dist");
module.exports = {
// Example setup for your project:
// The entry module that requires or imports the rest of your project.
// Must start with `./`!
entry: './',
// Place output files in `./dist/my-app.js`
output: {
path: DIST_DIR,
filename: 'index.js'
},
module: {
noParse: [
/aws\-sdk/,
],
loaders: [
{
test: /\.json$/,
loader: 'json'
}
]
}
};
所以现在我假设我的代码应该准备好转换了。这是我的代码:
var AWSCognito = require('amazon-cognito-identity-js/dist/amazon-cognito-
identity');
exports.handler = function (event, context, callback) {
registerUset();
callback(null, "some success message");
}
var registerUset = function () {
}
如您所见,它只是一个简单的Java脚本代码,其中没有任何内容。现在,当我这样做:npm run build我看到编译的js文件,我没有得到任何错误。 但是,当我压缩js和节点模块文件夹并在lambda控制台上传和测试时,我收到以下错误:
"errorMessage": "Cannot find module './lib/browser_loader'"
在帖子开头发布完整堆栈。对我来说没有意义。这是一个aws bug还是我错过了?
答案 0 :(得分:1)
如果有人遇到同样的问题,我找到了问题的解决方案:
感谢:
http://www.rricard.me/es6/aws/lambda/nodejs/2015/11/29/es6-on-aws-lambda.html
所以基本上我需要使用babel和json作为加载器,因此解决方案的关键部分是:
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015'],
plugins: ['syntax-flow', 'transform-flow-strip-types']
}
},
{
test: /\.json$/,
loader: 'json'
}
]
}