Serverless-webpack: Lambda now supports Node 8.10… how do we enable?

Created on 3 Apr 2018  Â·  11Comments  Â·  Source: serverless-heaven/serverless-webpack

Hi

Lambda now supports Node 8.10 (finally)!

https://docs.aws.amazon.com/lambda/latest/dg/programming-model.html

Does serverless-webpack just work if you change the serverless.yml file to use Node 8.10 or are there .babelrc and/or webpack.config.js changes required to make this work?

question

Most helpful comment

@ChristopheBougere I was having the same issue as you were with object spreads resulting in an unexpected token error. Turns out, you need the babel-plugin-syntax-object-rest-spread package for babel to parse - but not transform - the spread syntax so it'll work natively in Node 8.

npm i --save-dev babel-plugin-syntax-object-rest-spread

then in your .babelrc

{
  "plugins": ["syntax-object-rest-spread"]
}

Hope that helps.

All 11 comments

Hi @waynerobinson , yeah - finally they support it.

Imo, it is enough to change the function runtimes in your serverless.yml to nodejs8.10 and add the proper babel env preset target to your .babelrc (and make sure _that_ you use the babel-preset-env preset):

// .babelrc
{
  "presets": [
    ["env", {
      "targets": {
        "node": "8.10"
      }
    }]
  ],
  // further babelrc settings
}

Seems to work pretty well… thanks. :)

That's great news! As I think node 8.10 provides all the features I'm using, I tried removing babel:

  • .babelrc
  • babel-loader in webpack.config.js
  • uninstall babel packages

But I'm having an issue with spread operator, that should be available in 8.10:


ERROR in ./index.js
Module parse failed: Unexpected token (68:6)
You may need an appropriate loader to handle this file type.
|     //
|     const obj = {
|       ...obj1,
|       ...obj2,
|     };

This is coming from serverless-webpack/lib/compile.js:34:19
Am I missing something? It seems that babel is still trying to compile it... 🤔

You should not remove babel, but configure the babel preset to version 8.10. It will make sure that only functions are transpiled that are known to be missing. I'm not sure if you can drop the babel-loader - webpack has to know how to compile the file.
However, the spread operator should be available according to https://kangax.github.io/compat-table/es6/#node8_7

Thanks @HyperBrain !
Just to understand, if my code is able to run in 8.10, why should I keep babel?
The goal was mainly to remove dependencies, avoid compilation, ...
I still need webpack for other stuff though.

BTW 2 of the examples doesn't seem to use babel:

  • include-external-npm-packages
  • multiple-statically-entries

Did you try to remove the babel loader completely from module: rules and use the very latest webpack version? According to webpack's documentation, loaders should be optional (just read through it) and webpack itself should understand plain JS. (However, I remember that the import statement is not yet supported in Node 8).

yep I removed the rule from webpack.config.js, but using webpack 3.11 (updating to 4 would be more complicated).
I actually didn't noticed that babel had such an importance in webpack but that make sense. I'll keep you updated if I find a solution.

@ChristopheBougere

updating to 4 would be more complicated

Please have a look at the babel-example-webpack4 I created in the examples folder. Switching to webpack 4 is quite easy - you just have to do the right changes to the webpack config (using the latest sls-webpack version). I already did that for multiple of our big projects.

Thanks, I'll have a look and I may take advantage of removing babel to upgrade webpack at the same time.

@ChristopheBougere I was having the same issue as you were with object spreads resulting in an unexpected token error. Turns out, you need the babel-plugin-syntax-object-rest-spread package for babel to parse - but not transform - the spread syntax so it'll work natively in Node 8.

npm i --save-dev babel-plugin-syntax-object-rest-spread

then in your .babelrc

{
  "plugins": ["syntax-object-rest-spread"]
}

Hope that helps.

As pointed out in #363, the most convenient way to enable Node 8 is to configure the env preset and additionally use the stage-2 preset to add support for the spread operator and import.

Closing this issue now - a viable solution is available...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

deftomat picture deftomat  Â·  5Comments

jaydp17 picture jaydp17  Â·  4Comments

bericp1 picture bericp1  Â·  5Comments

hassankhan picture hassankhan  Â·  3Comments

vladtamas picture vladtamas  Â·  5Comments