TypeScript and Node - node.js

Are there any limitations using TypeScript in a Node project compared to using Javascript at the server side for a web application?
Are all existing Node modules out there totally reusable without modification?
(PS: I am ok to use Visual Studio as the IDE.)

Are there any limitations using TypeScript in a Node project compared to using Javascript
No. You just have the overhead of setting up TypeScript, for the sake of its advantages : https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html
Are all existing Node modules out there totally reusable without modification?
Yes. Just set allowJs:true in your tsconfig and you don't even need a declaration file.

Related

Why webpack and babel are dependent on Node.js to run?

I was learning babel and webpack and then it turns out I need to install node.js to run them both and I asked myself WHY? Then according to my research, we need node.js for webpack and babel since both of them were written in JS and to run that JS code which transpiles( for babel) and bundles up the code(for webpack). Also, another reason is that since both babel and webpack handles our JS code outside of the browser, this is the reason to use Node.js. Are these reasons true?
According to the Node.js website -
Node.jsĀ® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
Webpack and babel (along with many other tools you might use for frontend development) are written in javascript and since they are command line tools, they need a way to run outside of the browser (directly on your machine).
They could have used some other language to write the tools but since they chose to write them in javascript, Node.js is the only feasible options right now.
In case you are interested, the original creator of nodejs
Ryan Dahl has built another secure runtime environment for Javascript/Typescript called Deno
Yes, at the moment the node.js project is non-portable by full open source disclosure nor extension to port node.js commonjs without a just in time transpiler with a service worker on a server.
Definitions: FHC = "from half court"
Babel and webpack
(1) transpile/move (write&read, not ln -s sym...bolic link) & (2) compost/pile onto a JIT target,
like v8 or other browser-javascript-interpreters. v8 on a service worker can compile on the edge just in time for interpretation in a browser v8 environment but still on the cloudflare edge server.
Declare (FHC) Allegedly, rust provides webassembly modules with the lipid [llvm-]wrappings
that nucleic acid exocytosis needs to be a full-blown virus, err I mean
Initiation Routine needs to be a targetable JIT extension!
A compiler/transpiler still requires it's target-scope receivable. Declare (FHC) Emit is to execute as compile is to interpret, even AST.

Angular + Electron app: query web API or DataBase according to deskop or web mode

I would like to build an app using Angular + Electron. My app should be able to run both on desktop and browser platforms. I'm considering to use angular-electron starter kit (but I'm open to other possibilities).
What concerns my is the way I read and write data. The data must be stored in a MySQL database. Ideally I would like to:
make the app call an api when NOT running on Electron (browser mode)
make the app query directly the mysql database when running on Electron (desktop mode)
I know I could check for window && window.process && window.process.type to let the app understand wheter running on Electron or not, however I'm a bit concerned about how to handle this. Also because I probably need to import node packages like mysqljs but ONLY in the desktop mode.
You can simply pass in different environment files in Angular during build this allow you to control the environment variables so that you can tell that it is a web app build or an electron app build.
https://angular.io/guide/build
EXTRA
But if you are importing binary packages this is where it gets tricky. I dont think there is a clean way for you to do conditional imports. I did not manage to find a way to do it cleanly and sort of maintained another repository for all my services that needs to import binary files.
To import the binary files you will also need to edit some webpack settings to tell angular to not compile/include the binary files during the build process so that you can use you librarys like mysqljs that require binary files. There is also some settings on the electron end to make binary files compatible for different platforms ie Windows, Mac , Linux. Basically it is really a pain to do it.
Link to how to edit webpack settings for angular 7+
https://github.com/manfredsteyer/ngx-build-plus
I will totally suggest you not to do it unless you really have a very good reason that you need to use these libraries.
EDIT 10/1/19
Okay I was referring to the MySqlJs but it seems like it does not have native modules modules. Native/binary modules basically means javascript code that relies on c++ compiled binaries(Or any native language like rust...).
For my case I was using the grpc modules which has a native dependency. Had to switch to grpc-web in the end.
I will add some footnotes here if you ever need them
https://electronjs.org/docs/tutorial/using-native-node-modules
Node.js / npm - anyway to tell if a package is pure JS or not?

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.

Sharing code between node js project and a web site project

So I started coding a chess engine in typescript as a side project. I initially intended it to be a CLI like most other chess engines that interact through stdin and stdout. This so I could plug it to a GUI like Arena and test it against other engines. For that I decided to do it as a node project.
I set it across several files with 1 being the UCI (universal chess interface) implementation and the remaining ones chess logic and AI logic. After banging my head a few times trying to understand modules in typescript I finally got it to work.
Now I decided it would be interesting to write a simple GUI myself as a webpage with html+typescript+jquery. Now, I would like to use all the logic modules I wrote but I'm finding it impossible. From what I understand you can't use CommonJS in browsers so the only way I could get it to work was using instead internal modules for which I need to modify the .ts files to wrap the code in module X{ } blocks and recompile them just for the GUI every time I change something. This situation seems far from ideal and I was wondering if there is a way around it...
Use http://browserify.org/ to add CommonJS-like support on the front-end.
But also read this (slightly outdated) question and answer: How should I go about writing a node.js web application with both server and client side code?
Use external modules for both the server and the browser.
When compiling for the browser, use the switch to specify the AMD module pattern:
tsc --module amd app.ts
And use RequireJS to load modules for you.
<script src="require.js" data-main="app.js"></script>
You will need to compile for the different targets, but the source code in TypeScript can be identical.
I created a demo project to share code between the client and the server : https://github.com/basarat/demo-fullstack/blob/master/src/Gruntfile.js
It compiles the common files for both amd/commonjs and server files only as commonjs with the client only files as only amd. It uses grunt-ts to manage this : https://github.com/grunt-ts/grunt-ts

Open existing Node.js Project in a TypeScript project

I currently have an existing project in Node.js and I would like to migrate it to a TypeScript project.
How can I do this?
Thanks for the help.
The easiest way to do this is to look at the Node sample provided with the TypeScript. It already includes declarations for the built-in NodeJS modules.
After importing the node.js you can start by converting your modules one by one. Since the Typescript will convert into JS, you can convert one file at a time and test it right away!
With ambients it should be easy to do! Good luck with your conversion.

Resources