DEV Community

Electron with Typescript using ts-node

Michael De Abreu on January 27, 2018

In my last post, I talked about how to Integrate Angular-CLI with Electron. One of the things that was missing was the use of Typescript, oposite t...
Collapse
 
trusktr profile image
Joe Pea

I've been using ts-node while developing an app, and it works great! But when it comes time to bundle the application (f.e. using electron-forge, or similar), then something happens and it doesn't work anymore. Have you had luck bundling your ts-node-using application?

Collapse
 
michaeljota profile image
Michael De Abreu

For building you should consider use Webpack or similar to bundle your code. ts-node it's great for development, but it wasn't made for production environments.

Collapse
 
trusktr profile image
Joe Pea

Ts-node is working great in production. The application startup speed difference is totally imperceivable (and we have quite a bit of code!). The nice thing is that imports compile to native Node.js require() calls, which is much more flexible than with Webpack. The file path you specify is actually what you're working with, which is valuable. We can do nice runtime tricks with the imports (requires) that are otherwise difficult to do with Webpack. It's nice to be able to work with the actual underlying system that way.

Thread Thread
 
michaeljota profile image
Michael De Abreu

For node_modules requires, you can use npmjs.com/package/Webpack-node-ext.... I would really consider use Webpack, but it seems it's safe to use ts-node (github.com/TypeStrong/ts-node/issu...).

Thread Thread
 
trusktr profile image
Joe Pea

Ah, thanks for sharing webpack-node-externals. That could come handy when I find myself in an existing webpack/node project.

Thread Thread
 
marksyzm profile image
Mark Elphinstone-Hoadley

Did you manage to get debugging to work in vscode or similar? I've had problems getting breakpoints to work. It just ignores them when it is in a file that isn't native JS.

Collapse
 
xemanet profile image
xemanet

Hello Michael

I'm create a SSR with typescript and node-express-react-mongoDB-sequelize-mysql and I want integrate with electron to run on the win desktop. Thats all! ;)

All run correctly but there is no human way to make it work whith electron.

Imports errors, Document is not defined, electron window no open...etc

Can you tell me some resources where to find help on this?.

Thanks!

Collapse
 
michaeljota profile image
Michael De Abreu

Hi! You may face some troubles with native modules in the first place, as Electron bundle its own version of Node, that may or may not be different from yours.

Aside of this, what are your expectations of this applications? Does this should run on it's own? Does this should display data from an api REST? It really depends upon what you are expecting to archive.

If you want an application that runs on its own, and need a database, I would suggest you to use another kind of database. Maybe rxdb, that have a mongo compatible query system.

If you want an applications that display data from an api REST, then you just want to use the React part of the app. You may want to separate the React client entirely of the stack, to be able to use it in Electron and in Express.

You could setup an express server inside the Electron app, but this is kind of unlikely scenario to be needed.

Collapse
 
xemanet profile image
xemanet

Hi!

This app will have to work in a Pc windows that has installed a business management application(BMA) with mysql and apache(nginx). The user has a online shop in internet(mysql) and this app will manage actualized regs between BMA and Online Shop.(Clients, orders, products and products' images).
This app contains a poolling loop(4 timers) when opening it that run on , Socket.io to comunicate with user in react page, mongodb for store logs in pc and default values(mongodb) that user can modify with comunicate with databases with get and post of express.

I have separate server and client, and webpack compile in dist folder.

All run correctly in development and production.
The next step was integrate electron in app and this is the problem.

All app was write in nodejs javascript with babel and babel node for run it to use imports instead of require, and with errors of electron I opted to change the code to Typescript(much better of course) but the errrors continue.
If in main entry of pckage.jsfor I indicate dist/client.js, electron send error "document is not defined". If I indicate production.ts send error in imports Unexpected identifier.

I'm a bit lost and I do not find much on the internet.
Can you give me some information about it?

Thank you very much for answering!!

Thread Thread
 
michaeljota profile image
Michael De Abreu

You want to bundle the React app into Electron, so you would probably need to do just that. Have you try to create an Electron application before? The tutorial is very useful for a first app. :).

Thread Thread
 
xemanet profile image
xemanet

ok, thanks for your help.

Collapse
 
xemanet profile image
xemanet

Hi Michael!!

require('ts-node').register(); // This will register the TypeScript compiler
require('./src'); // This will load our Typescript application

wonderful!!!!!

I generate a package windows and run correctly!!

A note: with save-dev is ok in development but in package the app need as dependence

Collapse
 
michaeljota profile image
Michael De Abreu

Good to know. Glad it helps you. :).