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

Guide to implementing reverse routing in an expressjs application

Take a look at this sample from the express documentation: app.get('/users/:userId/books/:bookId', function (req, res) { res.send(req.params) }) The main question arises here, how can I create this URL for a specific user/book combination? For ...

Steps to establish a connection with a remote MYSQL database using the actual IP address in Node.js

In my Nodejs file, I have set up the connection as follows. The issue arises when attempting to connect to the remote database on a server. module.exports = { host: '234.32432.32432',//this is an example IP address and not a real one. use ...

The value stored in $_POST['valuename'] is not being retrieved

Having recently delved into ajax, I am encountering some difficulties in making it function properly. The objective of the code is to send two variables from JavaScript to PHP and then simply echo them back as a string. However, instead of receiving the e ...

What is the process of accessing JSON data transmitted from a Express server in JavaScript?

Working with a node server, I've set up a client-server model, meaning the client connects to "localhost" and express generates a response based on certain logic. While I'm able to send a JSON object through the response, I'm unsure of how t ...

Check for support of Symbol.toStringTag in JavaScript

Can this function reliably detect the presence of @@toStringTag in all environments? function hasToStringTagSymbol() { if (Symbol && (typeof Symbol() == "symbol") && !!Symbol.toStringTag) { var xTest = function () { }; xTest.prototyp ...

sending parameters to a callback function that is listening for arguments

function setmclisten(message, sender, sendResponse) { console.log(data); if(message['type'] === 'startUp') { console.log(data); sendResponse(data) } } function QuarryToServer(){ chrome.runtime.onMessage.removeListener( ...

Troubleshooting: ng-disabled feature is not properly functioning with Bootstrap buttons

I am currently using a combination of bootstrap.js and angular js in my project. The code snippet I have is as follows: //snippet from the controller $scope.isWaiting = true; $scope.promise = $http.get("voluumHandler.php?q=campaigns&filter=traffic-sou ...

Creating dynamic charts in Javascript using Firebase

My dad recently asked me to enhance our motor home by integrating an Arduino with Firebase to monitor the water and propane tanks. He's hoping to be able to check tank levels from his phone so he can see when they need refilling. I've successful ...

store the id of each li element dynamically into an array

In my code, a list is generated dynamically and each list item has a special id. I am trying to store each li "id" in one array. This is JavaScript code: var i = 0; $("ul#portfolio li").each(function(eval) { var idd = new Array(); idd[i] = $(this ...

Retrieving user input from one component to be used in another component in Angular

I'm currently working on a structure that involves a navbar component and a form component https://i.stack.imgur.com/nPRLO.png Initially, I have a navbar component where I load user data using an ID stored in the session. In the right side component ...

The functionality of AC_FL_RunContent is failing after an UpdatePanel postback

In the code for the repeater item, I have a JavaScript function that calls AC_FL_RunContent to display a flash file when a link within the repeater item is clicked. The datasource I am using displays the first page of video links with five items per page, ...

The select box in Material UI is not displaying the data as expected

I'm currently tackling an issue where, upon every click of an icon, a select box (from material ui) needs to be displayed with a few options inside it. The functionality should show the select box each time the icon is clicked. Here's a brief sum ...

Exploring the world of ng2-translate for translating texts

For the translation of headings and texts in my Angular2 web application, I utilized ng2-translate. However, I am facing a dilemma when it comes to translating texts that are passed from a .ts file. For example, I can easily translate texts in an HTML fi ...

The dropdown menu vanishes from sight as soon as the cursor moves away from a link

Recently, I encountered an issue while trying to create a dropdown menu using Jquery. The problem arose when attempting to select the second link, as the entire menu would disappear. Additionally, is there a way to ensure that only one dropdown menu is vis ...

Issues with jQuery autocomplete functionality on certain elements are not uncommon

I've been experimenting with creating a user script for Opera using Greasemonkey to implement autocomplete functionality on input elements within web pages. However, I've encountered some issues with the script not working as expected. Initially ...

Error in defining Mongoose schema field (topics: Express, request body, inheritance, attribute, data type)

After spending half an hour troubleshooting, I have realized the issue and decided to share it here to save your time. I defined a schema as shown below: var mongoose = require('mongoose'); var ItemSchema = new mongoose.Schema({ title: Str ...

Is AJAX.call functioning properly in every browser except for Firefox?

I encountered an issue with an ajax load in Firefox. Every time I try to load, I keep receiving a message that says 'unreachable code after return statement'. Despite my efforts to find a solution, I have not been successful in resolving it. Inte ...

How can I retrieve the index of a v-for loop within a function in Vue.js HTML?

How can I splice the array from the fields in HTML Vue JS if the status is true? I also need to pass the index value to a function. Is this possible? The error I am encountering is: Uncaught ReferenceError: index is not defined at Object.success (80 ...

Enter text into a field on a different webpage and verify if the output matches the expected result

Possible Duplicate: Exploring ways to bypass the same-origin policy I'm facing a scenario where I have a form on my website that requires validation of a number. The validation process involves checking this number against another website where e ...

Unexpectedly, the npm Restful API has stopped functioning

I recently completed a tutorial on creating a RESTful API using npm and PostgreSQL. The tutorial was really helpful and I had everything working perfectly. However, when I returned to my project after some time, I encountered a sudden issue with routing - ...