Executing a TypeScript function through a package.json script: A guide

I'm currently using a javascript function from my package.json script, but I want to switch it to a typescript function in a typescript file. Can someone advise me on how to make this adjustment?

This is the javascript code that runs the desired function:

"scripts": {
    "command": "node -e 'require(\"./javaScriptFileName\").functionName()'"
}

Here is the typescript file exporting the function:

const fs = require("fs");

module.exports.functionName = function(path: string): void
{
   if(fs.exists(path))
   {
       console.log("Exists");
   }
}

I've tried using the ts-node package and replacing "node" with "ts-node", but it didn't work as expected.

Answer №1

If the file javaScriptFileName.ts is organized as:

export default {
  functionName: () => console.log('whatever')
}

You can execute it using ts-node just like any other typescript file: simply import the file, and then call the function.

ts-node -e "import myLib from './javaScriptFileName'; myLib.foo()"

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

Enhancing the level of abstraction in selectors within Redux using TypeScript

Here is a custom implementation of using Redux with TypeScript and the connect method. import { connect, ConnectedProps } from 'react-redux' interface RootState { isOn: boolean } const mapState = (state: RootState) =&g ...

Tips for clearing the sessionstorage upon browser refresh without removing data when clicking the back button in the browser

I am looking to clear the session storage only when the page is refreshed, without affecting the browser's back button functionality. I want to achieve this using AngularJS/JavaScript. Currently, I am storing data in sessionStorage on a back button c ...

The Amcharts 5 chart fails to load within the MUI Dialog due to the inability to locate the specified HTML element with the given ID

I'm utilizing AmChart 5's XY Chart to display data. Upon clicking a button, users can view this chart in a MUI Dialog for manipulation. However, when attempting to display the chart in a MUI Dialog with a unique ID, an error stating "Could not fi ...

Is it possible to use Immutable named parameters with defaults in Typescript during compilation?

Here is an example that highlights the question, but unfortunately it does not function as intended: function test({ name = 'Bob', age = 18 }: { readonly name?: string, readonly age?: number }) { // this should result in an error (but doesn&apo ...

AngularJS allowing multiple ngApps on a single page with their own ngRoute configurations, dynamically loaded and initialized

Seeking advice on lazy loading separate ngApps on one page and bootstrapping them using angular.bootstrap, each with its own unique ngRoute definitions to prevent overlap. I have a functioning plunkr example that appears to be working well, but I am unsur ...

Submit a document from a Vue.js application via an AJAX POST request

Seeking advice on how to upload a file in a VueJS application and send it to a PHP page using jQuery's ajax function. Any assistance would be greatly appreciated. My goal is to achieve this within a VueJS method (the script provided functions correctl ...

The presence of  is seen in textarea elements, but it is not found

Currently, I am in the process of developing an autocomplete feature for a textarea. While there are existing autocomplete solutions available, I decided to create my own from scratch. The autocomplete functionality works smoothly, except for one issue th ...

Graphing numerous boxplots simultaneously

Looking to generate multiple boxplots on the same graph? I have data for three different sports that I want to visualize in separate boxplots. Each boxplot should display a specific variable on the y-axis, and I need the flexibility to easily change this ...

One more time: Utilizing AJAX to save the outcome in a variable with the help of callback (undefined)

I am facing a common problem and despite my efforts, I cannot seem to resolve it. The code snippet I have provided below is causing me trouble: function get_network_from_database(callback) { $.ajax({ type: "POST", url: "load_network.ph ...

Is the UUID key displayed as an object within a Reactjs prop?

Hey there internet pals, I've stumbled upon a mysterious corridor, so dark that I can't see where I'm going.. could really use someone's flashlight to light the way. I have put together a basic, no-frills to-do list program. It consi ...

Challenges arise when integrating PHP variables into JSON

I am facing an issue with the following JSON code: $dataB = <<<DATA { "actualArrivalDateTime":"2021-09-07T10:00", "containerNumber:" "000", "shipmentNumber": aaaa1, "co ...

Why is webpack attempting to package up my testing files?

In my project, I have two main directories: "src" and "specs". The webpack configuration entrypoint is set to a file within the src directory. Additionally, the context of the webpack config is also set to the src directory. There is a postinstall hook in ...

showing the data in a textbox using AJAX retrieved information

I have a table where additional rows can be added by the user with the click of a JavaScript function. Each row contains a drop down list, and based on the selected value, an AJAX script fetches some values to display in corresponding textfields within th ...

Tips for utilizing Selenium to acquire links from a webpage

Is it possible to retrieve the link location of a web element in Selenium? While we can easily obtain the text from a web element and use getAttribute("href") method to get the location of a hyperlink, my need is to extract all the links present. For insta ...

Changing div height based on the height of another dynamic div

Looking to create a box that matches the height of the entire page, live height minus another dynamic div called #wrapping. I have code that offsets it instead of removing the height of the div... function adjustBoxHeight() { var viewPortHeight = $(wind ...

Utilizing JavaScript, HTML, and CSS to incorporate images within circular frames

After finding inspiration from this question about rotating objects around a circle using CSS, I decided to modify the code to include images of Earth orbiting the Sun. The challenge was to make one image orbit another like a planet circling its star. Ear ...

Create a cylindrical HTML5 canvas and place a camera view inside the cylinder

I have a unique project in mind that involves creating an HTML5 canvas cylinder with the camera view at its center. The walls of the cylinder will display images, and the entire structure should be able to rotate and zoom. If anyone has any advice on how ...

When incorporating conditional statements into your code, make sure to utilize the tags <script> along with

I have some script tags as shown below: <script src="cordova-2.5.0.js"></script> <script src="js/jquery-1.7.2.min.js"></script> <script src="js/jquery.mobile-1.1.1.min.js"></script> <script src="js/jquery.xdomainajax ...

Using Laravel: Validate username existence with jQuery AJAX before form submission

After creating a registration form with numerous fields, I encountered an issue when submitting data and receiving validation errors. The user would have to refill empty inputs, causing unnecessary delays. To streamline the process, I am looking to impleme ...

What is the purpose of adding node_modules to a .gitignore file when you can easily install it from npm anyway?

It appears that excluding the node_modules folder only complicates the process for anyone downloading a project as they will need to execute either npm install or yarn ...