What is the best way to add .xlsx and .docx files to the Typescript compilation output?

For my server, I have decided to use Typescript with NodeJS.

One of the challenges I encountered in my server logic is manipulating .xlsx and .docx files. Unfortunately, these file types are not included in the Typescript compilation output. These specific files serve as templates for generating other files.

This is an overview of my project structure:

package.json
package-lock.json
ormconfig.json
tsconfig.json
/src
  /reports
    --other-.ts-files
    Router.ts
    Report.xlsx //Desire this file to be incorporated in the Typescript compilation output
    Report.docx //Desire this file to be incorporated in the Typescript compilation output

I am seeking guidance on how to include .xlsx and .docx files in the Typescript compilation output. Specifically, what modifications should be made in the tsconfig.json file?

Answer №1

1) Deprecated method: It used to be possible to add additional extensions to nodejs, but this approach is no longer recommended.

var fs = require('fs');

require.extensions['.txt'] = function (module, filename) {
    module.exports = fs.readFileSync(filename, 'utf8');
};

var words = require("./words.txt");

2) Utilizing the file API: To read files in Node, you can always leverage the file API.

const fs = require('fs')
const path = require('path')
const css = fs.readFileSync(path.resolve(__dirname, 'email.css'), 'utf8')

3) Loading custom file types with TypeScript and webpack: With TypeScript and webpack, it's feasible to load custom file types such as SVG files. Additional typings are required for Typescript in this scenario, especially for frontend applications.

declare module "*.svg" {
  const content: any;
  export default content;
}

4) XLSX sheet reading library: When it comes to reading XLSX sheets, there is a reliable library named xlsx.

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

Creating number inputs in Ionic 2/3 using alerts

I am currently working with Ionic 3.x on my macOS system. The issue I am facing is as follows: I have an array that contains a number and another array consisting of names. table: { number: number, names: string[] } = { number: 0, names: ['& ...

An observable value is intertwined with another observable

indexData and indexEditData serve as the observables within my application. The purpose of indexData is to store a json object that is used to populate a table. Editing of table rows is facilitated by selecting individual rows, triggering the transfer of t ...

How can I make TypeScript mimic the ability of JavaScript object wrappers to determine whether a primitive value has a particular "property"?

When using XMLValidator, the return value of .validate function can be either true or ValidationError, but this may not be entirely accurate (please refer to my update). The ValidationError object includes an err property. validate( xmlData: string, opti ...

What is the specific type of event for a change handler in TypeScript?

As a newcomer to TypeScript, I recently crafted a change handling function that accepts the event as a parameter to assign the value, like event.target.value. Currently, I have designated this as any, but I suspect there is a more appropriate type for this ...

Tips for specifying a custom type as the return value of a function and denote it as a promise

I encountered this code snippet: import * as Promise from 'bluebird'; import {Response} from '../commonInterfaces/httpService'; getCall(url: string, accessToken: string, extraHeaders: object): Promise<Response> { let headers ...

How can we create dynamic keys for object properties in typescript?

Is there a way in TypeScript to convert an array of objects into an object with keys and arrays dynamically? For instance, given the following data: data1 = [ {a: 'st1', b: 1, c: 1, d: 1, e: 'e1' }, {a: 'st2', b: 2, c: 2, ...

Ionic2 - Ion-select malfunctioning on a particular page

I have encountered an issue with the ion-select component in my Ionic 2 app. Specifically, when navigating to a certain page, the ion-select does not display properly. Strangely enough, on other pages of the app, this component works perfectly fine. Below ...

The continuous rerendering of my component occurs when I use a path parameter

In my project, I am working on utilizing a path parameter as an ID to fetch data for a specific entity. To accomplish this, I have developed a custom data fetching hook that triggers whenever there is a change in the passed parameters. For obtaining the bo ...

Oops! It seems like there is an issue with reading the property 'filter' of an undefined object. Any ideas on how to resolve this error

Having an issue with a custom filter that is causing an error "ERROR TypeError: Cannot read property 'filter' of undefined". I need help fixing this as it's preventing anything from rendering on the page. Any suggestions on what changes I sh ...

Unable to determine all parameters for Modal: (?, ?, ?)

import { Component, Inject } from '@angular/core'; import { NavController, Modal } from 'ionic-angular'; import { PopupPage } from '../../components/modal/modal.page'; @Component({ templateUrl: 'build/pages/spot/spot. ...

Using @carbon/react in conjunction with Next.js version 13 leads to unconventional styling

Here's what I did to set up my Next.js application: npx create-next-app@latest I then installed the necessary package using: npm i -S @carbon/react The global styles in app/globals.scss were customized with this code snippet: @use '@carbon/reac ...

What steps should I take to resolve a 'Missing environment variable' issue in the Sanity platform?

No matter what I've tried, I can't seem to fix the error that keeps occurring. An uncaught error is popping up, saying that the environment variable NEXT_PUBLIC_SANITY_DATASET is missing. http://localhost:3333/static/sanity-5377bc10.js:4605 ...

Tips for utilizing CreateContext and AppContext with React and Typescript?

I am in the process of converting my current React app with JSX to TSX, but I am facing an issue with the AppContext not functioning as it did in my JSX version: App.TSX : export interface ContextType { cursor: {x: number, y: number}, } export const Ap ...

Uncovering TypeScript's Type Inference Power Through the keyof Keyword

Recently, I encountered a situation where I needed to utilize an abstract class. export abstract class ABaseModel { public static isKeyOf<T>(propName: (keyof T)): string { return propName; } } Following that, I also created another class wh ...

Discovering how to display the hidden messages section in the absence of any ongoing chats with the help of angular2

I currently have 2 tabs set up on my page. The active messages tab is functioning perfectly without any issues. However, I am encountering an error with the closed messages tab. If there are no messages in this tab, the system displays an error message w ...

"Utilize ngclass to set CSS classes based on enum string values

Is there a way to directly call an element in Angular when using an enum to organize component styles? Instead of writing multiple ng class expressions or dynamically passing them to the element call. button-types.ts export enum ButtonTypes { Primary ...

What is the reason for the failure of this typescript typeguard?

This code snippet defines a function for validating protocols and throwing an error if the input does not match the predefined protocols. const validProtocols = ["https", "http"] as const type AllProtocols = typeof validProtocols type Protocol = AllProtoco ...

Having trouble getting the onClick function to work in your Next.js/React component?

Recently, I delved into using next-auth for the first time and encountered an issue where my login and logout buttons' onClick functions stopped working when I resumed work on my project the next day. Strangely, nothing is being logged to the console. ...

Router-outlet @input decorator experiencing issues with multiple components

Within my router module, I have a route set up for /dashboard. This route includes multiple components: a parent component and several child components. The data I am trying to pass (an array of objects called tablesPanorama) is being sent from the parent ...

What is the proper way to add a string to a TypeScript array?

When attempting to add a string to a TypeScript array, I am encountering an error stating 'cannot push to undefined'. Is this the correct approach, or should I be using the spread operator instead? api.ts const api: IConfigName = {name: "getKey ...