Which is the best way to transpile Node 14 to older version of Node code? - node.js

I love using optional and optional chaining feature in Node 14 - however, most of the serverless platforms (I want to avoid running an instance) support 12.x and below - what is the best and the cleanest way to go about doing this.
I believe Babel is more directed towards using for the frontend js - do let me know if there is a better way of doing it in the backend as well.

Babel is the best way. I use it for the same purpose, AWS Lambda has support for Node.js 12.x runtime only.
So transpile your code using babel, and then use serverless stuff.

Related

Do I need Babel to transpile ES6 to ES5 nowadays?

I'm trying to upgrade an angularJS project and I'm stucked on one question, but let me summerize the project:
We use AngularJS, gulp and node 8.
we currently cannot use the ES6 features, like arrow functions, const/let and so on.
I achieved that by installing babels plugin. But babel transpiles the code, and the company staff doesn't want that, cause it can make it difficult to debug on production(I know we can use sourcemaps, it will be suggested).
We just want to write ES6 code and read(on the browser) the same code as we do now (but with es5)
I upgraded node to 10 and I thought that by upgrading node, the app would accept the new ES6 features. But still crashes when using es6 syntax.
Can someone tell me why? If the browsers accepts, why gulp doesnt?

Debugging AWS Lambda node.js ES6

I am trying to debug an AWS Lambda using lambda-local but the problem is that it doesn't recognize imports and exports in node.js.
Is there a workaround other than back-refactoring my entire API to requires?
Imports and exports are not yet supported in Node.
You have two options:
1. Change your imports to use require. Or,
2. Use babel to transpile your code before deploying to Lambda.
If you use serverless-framework, the serverless-webpack plugin will be of big help to you.

Typescript + Node + Express?

After stumbling upon the typscript-node starter released by microsoft, i started to get lost.
Can we really replace node with typescript on the server? There are several serverside things that TS does well:
- Creating a web API service with express
- Managing the CRUD queries with mongoDB
And much more...
I am used to generate an api with node and connect angular to that api. Am i wrong?
Should we switch to TS on the backend and forget about writing node code on the server?
Typescript is a (or rather, a superset of) language - not a runtime. It is the equivalent of Javascript except it needs to be compiled to run on the Node.js runtime.
You can write the backend with Typescript if you want, and then run it through ts-node, or just compile down to ES6 via tsc and then run it with standard Node (v8+ is recommended). This is what I do with many projects. It is still "node code", it just has all the benefits (and gimmicks) or Typescript.
I recommend the library meseret to manage your typescript node.js backend code. It has support for Koa, Mongoose and Socket.io, with many builtin configurations. It is a great way to manage things in one place, using TypeScript throughout your project.

Why transpile node.js backend code?

I am new to es6 and have two questions
Is transpiling really required for backend code (node.js) written in es6 ?
If transpiling is not necessary - is there a disadvantage running plain es6 on server side - can it affect debugging ?
Answer can be debatable. I strongly feel there is no need of transpiling your backend code (NodeJS). Transpiling converts your import/export statements to require, module.exports. Which you can directly use in NodeJs.
Also, on client side, it makes sense to have transpilation for different browser support and also it reduces the bundle size. But for NodeJS side, there is also a disadvantage for debugging.

Run jest with electron instead of node

To make a long story short, I'd like to run my jest tests (using CLI) with electron instead of node.
It's relevant when using native module, because you need to build them using electron header while jest run them using plain node.
So I must either build my native modules for my app (at least in dev mode) or my tests, I can't have both to work.
In this thread they propose to use mocha, but I want to use jest, which is far more advanced and interact well with React.
Note that I don't want to mock the native module, since I write integration tests.
I opened an issue about the zmq github repo. One proposed solution is "to target your tests using ELECTRON_RUN_AS_NODE=true electron as your node runtime".
This is a very good solution, since using electron will both make the test environment closer to the execution environment and solve my specific issue with native modules.
I'd like to apply that, but I do no seem to be able to setup the jest CLI to use electron instead of node, and I have no idea where to start. Maybe I should run jest programmatically without the CLI ? But I might lose the nice test filtering features of the CLI.
Has anyone solved this already?
"ELECTRON_RUN_AS_NODE=true ./node_modules/.bin/electron ./node_modules/.bin/jest works fine
If you're on Windows, then Eric Burel's excellent discovery might need a bit of a tweak to use the environment variable, and call the right version of Jest:
cross-env ELECTRON_RUN_AS_NODE=true ./node_modules/.bin/electron ./node_modules/jest-cli/bin/jest.js
Sadly, the colouring of the text in the results is lost.

Resources