Steps for running a TypeScript project as a child process within a JavaScript project

I am facing an issue with integrating my Electron app, written mainly in JavaScript, with an Express server project built in TypeScript. When I attempt to create a child process of the TypeScript project within my electron.js file, I encounter TypeScript errors stating that my server.js does not recognize the .ts file extension it is trying to import. The actual files exist, but they are not being handled correctly.

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

==== Electron App ===

Here is the snippet from my package.json in the Electron app:

"scripts": {
    "build": "react-app-rewired build",
    "start": "react-app-rewired start",
    "electron": "electron ."
}

tsconfig.json

{
  "compilerOptions": {
    // Compiler options here
  },
  "include": ["src", "expressproject/src"]
}

This is how I'm attempting to run the Express project in my electron.js file:

// electron.js

const { spawn } = require("child_process");

// Code to start express server
const expressServerProcess = spawn("node", [pathToExpressServerJS]); // D:/expressproject/src/server.js

function createWindow() {
...
}

==== Express Project ====

Snippet from the package.json in the TypeScript Express project:

"scripts": {
    "build": "tsc -p tsconfig.json",
    "start": "npm run build && node --loader ts-node/esm --experimental-specifier-resolution=node server.js",
}

tsconfig.json

{
  "extends": "ts-node/node16/tsconfig.json",
  // Compiler and transpile options here
}

It seems like the TypeScript code is not being properly transpiled to JavaScript. How can I ensure that the transpilation process is executed correctly?

Answer №1

Keith's Helpful Answer:

To integrate the values used in the npm start script of a TypeScript project into the child_process commands within an Electron app, pass all the values except the build script.

For the typescript express server package.json:

node --loader ts-node/esm --experimental-specifier-resolution=node server.js

In electron.js:

function startExpressServer() {
  const command = "node";
  const args = [
    "--loader",
    "ts-node/esm",
    "--experimental-specifier-resolution=node",
    pathToExpressServerJS, // Path to expressproject/src/server.js
  ];

  const expressProcess = spawn(command, args, {
    shell: true,
    stdio: "ignore", 
  });

  expressProcess.on("close", (code) => {
    console.log(`Express server process exited with code ${code}`);
  });
}

startExpressServer()

function createWindow() {
...
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

graphic map and paintbrush

I've implemented an image map using shape circles, but now I'm looking for a way to draw visible circles or icons on the map. Is there a solution for this issue? Currently, I have used a canvas overlay on the image to draw on it This method wo ...

Deleting an uploaded image on Cloudinary with Node.js - step by step guide

Having trouble removing uploaded images from my cloudinary account. When I delete a post, only the post is removed from the database but the image remains in cloudinary. How can I delete both the post and images simultaneously from my cloudinary account? ...

Adjust the language of the submit and reset button text based on a variable

I'm facing a simple question but I'm stuck right now... and I was hoping someone could assist me. My issue is as follows: I need to switch the values of two buttons, reset and submit, from Greek to English and vice versa based on a variable retri ...

Having trouble uploading an image using the Cloudinary API in a Next.js project

I am currently working on a page meant for creating posts. Initially, the page was designed to handle multiple image uploads. However, I made some adjustments so that it can now accept only a single image upload. Unfortunately, after this modification, the ...

NodeJs simple mock: Capturing query string parameters with ease

I'm currently developing a basic mock server using only JavaScript and Yarn. Simply put, I have this functional code snippet: function server() { (...) return { users: generate(userGenerator, 150) } This piece of code successfully generates 15 ...

Using the ControllerAs syntax in conjunction with $scope methods

Currently working on incorporating the controllerAs syntax into AngularJS 1.3 Here is how I'm starting my function declarations: function() { var myCtrl = this; myCtrl.foo = foo; // Successfully implemented myCtrl.$on("foo", bar); // Enc ...

The duration required to render DOM elements

Trying to determine the best method for measuring the rendering time of DOM elements in a web browser. Any tips? I'm currently developing an application that involves significant DOM manipulation as part of comparing the performance between Angular 1 ...

Execute asynchronous calls within each iteration and proceed to the next iteration in Node.js

I am currently faced with a dilemma at work and I need advice on the most effective approach to handle it. Below is an example of my code: for(var i =0 ;i < collection.length; i++){ asynCall( collection[i],function(){....})//doing a asynchronous cal ...

How to modify the color of the placeholder text in a select dropdown using Material-ui

I have a material-ui select component that displays a dropdown menu of options for the user to choose from. The placeholder text, which currently reads "select option," appears in light grey to prompt the user to make a selection. However, I am concerned t ...

Can you iterate through each element that matches those in a different array?

As a beginner in Javascript, I may stumble upon some obvious errors, so please bear with me. My goal is to iterate through two arrays and perform a loop for each element that exists in both arrays. Currently, this is my code: if(obtainedCards.some( sp =& ...

What is the process for creating a parent container in which clicking anywhere inside will cause a child container (built with jQuery UI draggable) to immediately move to that location?

This is a rundown of tasks that I am struggling to code more effectively: When the bar is clicked anywhere, I want the black dot button to instantly move there and automatically update the displayed percentage below it. Additionally, when I drag the butt ...

What methods can be used to customize the font and background color of a website for different user groups?

Trying to incorporate a template into my project. My client has requested the following: The regular user area should feature a blue background. The professional user area should have an orange background. Is there a way to set up a condition to change ...

Leveraging composer to enhance the organization of my dependencies

As I organize my files for my PHP-based website, I've turned to composer for assistance. One issue I've encountered is loading front-end dependencies like jquery into my pages, specifically the index.php. In the past, I manually specified each js ...

Updating the customer's billing address in Stripe without modifying their payment card details

Can I update a customer's stored address on Stripe without updating their card information as well? Currently, when customers update their info, they are required to enter their card details even for minor changes like the city. Is there a way to only ...

Is there a way to send a variable from a client-side script to a server-side script?

I am facing a challenge in Google App Maker where I am attempting to execute a Client Script to retrieve a variable and then pass that variable to a Server Script for further use. However, I am struggling to figure out the correct implementation. Within m ...

What is the process for designating the TypeScript server side entry point in a "Nuxt TypeScript" project?

In my experience with a JavaScript-based Nuxt project, the server entry is located in server/index.js. Here is the default code for Express.js: const express = require('express') const consola = require('consola') const { Nuxt, Builder ...

Determine the type of input and output based on another argument

When working with a function that takes an object of either TypeA or TypeB, the first parameter is used to specify the type of the object and the returned type depends on this first parameter. The issue arises in TypeScript where the type of the object is ...

How can the value of a number in Angular be changed without altering its original value?

Imagine having the initial number 100. If I enter 50 in another input, it should add 50 to 100. However, if I then change the value from 50 to 80, the total should be 180 and not 230. The goal is always to add numbers to the original sum, not the new valu ...

"Alert in Javascript executing prematurely prior to initiating the function for sending a get request

private validateURL(url: string) { let isValid = false; this.$http.get(url).then( (data) => { console.log('success'); isValid = true; } ).catch( (reason) => { console. ...

The folder creation in the 'outDir' directory by TSC continues to grow

Hello! Currently, I am engaged in a small TypeScript project where I need to utilize two separate tsconfig.json files, both of which inherit from my main tsconfig.base.json file. Unfortunately, I encountered an issue with the compiler creating unnecessar ...