Hello,
I've got some code with async / await and it works well outside Serverless framework, I test it with jest tests.
I use babel-jest with following .babelrc config:
{
"presets": [
"env"
]
}
My package.json includes:
"devDependencies": {
"babel-core": "^6.25.0",
"babel-jest": "^22.4.3",
"babel-loader": "^7.1.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.0",
Now when I want to invoke serverless function sls invoke local -f first -l that uses code with async/await I get following error:
ReferenceError: regeneratorRuntime is not defined
I tried to add env preset to webpack config but I'm afraid I don't know how.
Following file causes error (options element specifically)
const path = require('path');
// eslint-disable-next-line import/no-unresolved
const slsw = require('serverless-webpack');
module.exports = {
entry: slsw.lib.entries,
target: 'node',
module: {
loaders: [{
test: /\.js$/,
loaders: ['babel-loader'],
include: __dirname,
exclude: /node_modules/,
options: {
presets: ['env']
}
}],
},
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
};
options/query cannot be used with loaders (use options for each array item)
Does anyone know how to solve that problem?
Hi @panzupa , I think there are some configuration issues. When using the env preset, you have to configure the target, so that the needed plugins are activated:
// .babelrc
"presets": [
[ "env", {
"targets": {
"node": "8.10"
}
}]
],
In your webpack config you then have to omit the options property in the loader rule. Webpack will use the .babelrc then.
Then you have to upgrade the babel-loader to at least 7.1.3. Then the build part should be ok.
However, if you want to implement a Lambda handler in AWS via async/await (i.e. you return the await itself) there is a PR open right now in Serverless to add that functionality. Right now the very top lambda handler only can use the callback with invoke local. Internally you should be able to use async/await at will.
Hi @HyperBrain ,
Configuration of .babelrc you've provided solved my problem.
Thank you very much.
And for Node that is not the 8? like the one that runs on aws lambda?
@LucasBadico Can you explain, what exactly you mean - which "8" do you reference?
In aws lambda, the node version is 6.10
hi @HyperBrain
Should be:
// .babelrc
{
"presets": [
["env", {
"targets": {
"node": "8.10"
}
}]
]
}
//this is error output:
A common cause of this error is the presence of a configuration options object without the corresponding preset name. Example:
Invalid:
`{ presets: [{option: value}] }`
Valid:
`{ presets: [['presetName', {option: value}]] }`
When I put "env" and i got this Module build failed: Error: Couldn't find preset "env" relative to directory
OK I just install babel-preset-env and have no this error again.
Most helpful comment
Hi @panzupa , I think there are some configuration issues. When using the env preset, you have to configure the target, so that the needed plugins are activated:
In your webpack config you then have to omit the options property in the loader rule. Webpack will use the
.babelrcthen.Then you have to upgrade the
babel-loaderto at least 7.1.3. Then the build part should be ok.However, if you want to implement a Lambda handler in AWS via async/await (i.e. you return the await itself) there is a PR open right now in Serverless to add that functionality. Right now the very top lambda handler only can use the callback with
invoke local. Internally you should be able to use async/await at will.