Troubleshooting Problems with Tsconfig and Output Directory

I'm experiencing an issue with the outDir setting in my tsconfig.json file.

Here is what my tsconfig file looks like:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "outDir": "dist",
    "outFile": "App.js"
  },
  "exclude": [
    "node_modules",
    "local_typings"
  ]
}

The files are being compiled to App.js, but not in the specified directory (dist), instead they are being placed in the same directory as the tsconfig file. Has anyone found a solution to this problem?

Answer №1

After some investigating, I discovered the root of the problem. It turns out that all you need to do is specify the file path and name in the outFile without utilizing outDir. Therefore, in this scenario, it would look like this:

"outFile":"dist/App.js"

Hope this helps! Have a fantastic day!

Answer №2

Regrettably, as of version 3.3, TypeScript does not have the capability to support both outDir and outFile at the same time.

Like many others, I also wish to maintain a tidy source directory free of any build files while utilizing TypeScript to consolidate my code into a single file without relying on external tools like webpack, parcel, or rollup for this purpose. However, that functionality is currently unavailable.

(Nevertheless, if you simply want to generate the combined output in a separate directory, using outFile: "subdir/bundle.js" will suffice.)

Answer №3

For saving to a single file or multiple files, simply add the parameter "outDir":"./dist"

Answer №4

If you're searching for a way to import from a module rather than module/dist/Anything

I came across a relatively simple solution.

My folder structure looks something like this:

- project
|- package.json

- app
|- package.json
|- ...

- api
|- package.json
|- ...

- shared
|- package.json
|- tsconfig.json
|- index.ts
|- src
| MySharedResource.ts
| MySharedSomething.ts
...

(shared is an npm package installed locally in both api and app) I wanted to be able to import things from shared using a simple

import { MySharedResource } from "shared";

The solution was to export every necessary component from MySharedResource.ts and MySharedSomething.ts

export interface MySharedResource {
 ...
}

and then export it from index.ts like this:

export * from "./src/MySharedResource";
export * from "./src/MySharedSomething";

Important note

Your tsconfig.json in /shared should have the outDir option set to ./dist (for example), so that index.ts gets compiled to /shared/dist/index.js and all other components get compiled into /shared/dist/src. Also, make sure to set the following in your package.json:

"main":"dist/index.js"

Note 1

You can export multiple items per file, but if you want imports like this one

import { Thing } from 'shared/Thing';

Then you need another file (e.g., Thing.ts) alongside index.ts in /shared with the following content:

export * from "./src/Task";

Note 2

You can also do this

export * as Thing from "./src/Thing";

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

Utilizing d3 Charts in Angular 4 Framework

I need assistance with integrating a bar chart in an Angular 4 project, which includes HTML and TypeScript files as components. Can someone please provide guidance on this? The bar chart should show the increase in the number of employees each month, star ...

what is the best way to eliminate comments from nested arrays when using useReducer?

Can someone help me figure out how to use useReducer and useContext to manipulate global state? I specifically need to know how to delete comments using useReducer. Data Structures View the interface image here Sample Data Array export const listsData:IDa ...

Tips for implementing a decorator in a TypeScript-dependent Node module with Create-React-App

I am working on a project using TypeScript and React, which has a dependency on another local TypeScript based project. Here are the configurations: tsconfig.json of the React project: "compilerOptions": { "target": "esnext& ...

Guide on showing a component exclusively for iPads with React and TypeScript

I need help displaying an icon only in the component for iPad devices, and not on other devices. As a beginner in coding for iPads and mobile devices, I am unsure how to achieve this specific requirement for the iPad device. Below is the code snippet tha ...

Tips for Maintaining User Sessions in Ionic 4 Application

I am facing an issue with keeping users logged in to the app in Ionic 4. I have tried storing credentials like email and cross-checking the user using an API, then redirecting to the menu page. However, I am experiencing a brief moment on the login page be ...

I encountered an issue while attempting to retrieve data using route parameters. It seems that I am unable to access the 'imagepath' property as it is undefined

Is there a way to access data when the page loads, even if it is initially undefined? I keep getting this error message: "ERROR TypeError: Cannot read properties of undefined (reading 'imagepath')". How can I resolve this issue? import { Compone ...

Inject SCSS variables into Typescript in Vue 2 Using Vue-cli 5

When working on my Vue 2 project (created using the Vue-cli v4), I successfully imported variables from my SCSS file into my typescript (.vue files) without any issues. I had the :export { ... } in my SCSS file _variables.scss, along with shims.scss.d.ts ...

A unique column in the Foundry system that utilizes function-backed technology to calculate the monthly usage of engines over a

In my dataset of ‘Engine Hours’, I have the following information: Engine# Date Recorded Hours ABC123 Jan 21, 2024 9,171 ABC123 Dec 13, 2023 9,009 ABC123 Oct 6, 2023 8,936 XYZ456 Jan 8, 2024 5,543 XYZ456 Nov 1, 2023 4,998 XYZ456 Oct 1 ...

The Monorepo encountered an issue with parsing the module in NextJS 13 combined with Typescript, resulting in

Currently, I am in the process of transferring a functional nextjs 13 app to a single monorepo. I started by creating a new repository using npx create-turbo@latest and then relocating my existing repository (let's call it "frontend") to the apps/ dir ...

When ts-loader is used to import .json files, the declaration files are outputted into a separate

I've encountered a peculiar issue with my ts-loader. When I import a *.json file from node_modules, the declaration files are being generated in a subfolder within dist/ instead of directly in the dist/ folder as expected. Here is the structure of my ...

The Angular 6 View does not reflect changes made to a variable inside a subscribe block

Why isn't the view reflecting changes when a variable is updated within a subscribe function? Here's my code snippet: example.component.ts testVariable: string; ngOnInit() { this.testVariable = 'foo'; this.someService.someO ...

Using parametric types as type arguments for other types in TypeScript or Flow programming languages

Consider the various types showcased for demonstration: type TranslateToEnglish<A extends string> = A extends "1" ? "one" : A extends "2" ? "two" : A extends "3" ? "three" : "e ...

Tips for patiently anticipating the completed response within an interceptor

Using the interceptor, I want to display a loading spinner while waiting for subscriptions to complete. This approach works well in individual components by showing and hiding the spinner accordingly: ngOnInit() { this.spinnerService.show(); this. ...

Running the `npm start` command in Angular tends to be quite time-consuming

When I use Visual Studio Code to run Angular projects, my laptop seems to take a longer time when running the server through npm start compared to others. Could this delay be related to my PC specifications, or is there something I can do to improve it? ...

Creating an Object Factory that preserves the type: A step-by-step guide

I developed a unique object factory to create instances of all my interfaces: interface SomeInterface { get(): string; } class Implementation implements SomeInterface { constructor() {} get() { return "Hey :D"; } } type Injectable = ...

Error message: Unable to locate module when utilizing my alternative library packaged with Rollup

Currently, I am utilizing rollup to package a UI library for use across various primary applications. However, the bundled ESM file contains imports that are incompatible with webpack in the main applications: import { ArrowDropDownCircleOutlined } from &a ...

Why use rxjs observables if they don't respond to updates?

I have an array of items that I turn into an observable using the of function. I create the observable before populating the array. However, when the array is finally populated, the callback provided to subscribe does not execute. As far as I know, th ...

Converting a string into an array of objects using Angular TypeScript

Can anyone help me with converting the following string into an array of objects? {"Car": "[" {"Carid":234,"CompanyCode":null}"," {"Carid":134,"CompanyCode":"maruti"}"," {"Carid":145,"CompanyCode":"sedan"}"," "]" } I attempted using JSON.parse, ...

Injecting live data into an input field within a table using Angular 4

Trying to create a dynamic row table with input fields in all cells. The loaded data is static, and I'm facing issues adding more data in the view. However, the functionality to add and delete rows is working fine. I have experimented with ngModel and ...

Issue with IntelliJ: TypeScript Reference Paths Are Not Relative

I am currently using IntelliJ as my IDE, but I am facing an issue with configuring gulp-typescript to compile my typescript code. The problem arises from the fact that IntelliJ does not treat my reference paths relatively, instead it references them from m ...