105

I've got this configuration from https://www.npmjs.com/package/ts-loader:

webpack.config.js:

var path = require('path');
var webpack = require('webpack');

module.exports = {
    mode: "development",
    devtool: "inline-source-map",
    entry: "./src/Api.ts",
    output: {
        filename: "bundle.js"
    },
    resolve: {
        // Add `.ts` and `.tsx` as a resolvable extension.
        extensions: [".ts", ".tsx", ".js"]
    },
    module: {
        rules: [
            // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
            { test: /\.tsx?$/, loader: "ts-loader" }
        ]
    }
};

./src/Api.ts:

export class Api {
...
}

But when I run webpack I get:

Error: TypeScript emitted no output for Api.ts
3
  • (Just a shot in the dark) Do you by chance have any outdated compiled javascript in the same directory as your ts? I had an issue similar to this which ended up being cause by some out of date compiled files. Commented Mar 22, 2019 at 17:06
  • @Code2Code no, cleaned everything up. Is there enough to have export class Api { ... } or do I need to instantiate it in that file too? Commented Mar 22, 2019 at 17:11
  • It is enough to export, since TS is a static type checker. Commented Mar 22, 2019 at 17:12

2 Answers 2

318

Check that you don't have noEmit set to true In your tsconfig.json file.

Sign up to request clarification or add additional context in comments.

5 Comments

yes, it works like that, but no js files are created. I was expecting that webpack will create a bundle.js file
"noEmit": false is necessary, but also make sure the files ou want to bundle (and their refs) are in "include" in tsconfig.json. Was not expecting the noEmit issue with Webpack.
Also check for noEmitOnError not being set to true!
This worked for me as well when coverting a simple react app to typescript. Thank you!
Also emitDeclarationOnly should not be true !!!
11

First change webpack config entry like index.js to index.tsx. Second make sure rule added for tsx file like:

{
  test: /\.(ts|js)x?$/,
  exclude: /node_modules/,
  use: {
    loader: "babel-loader",
    options: {
      presets: [
        "@babel/preset-env",
        "@babel/preset-react",
        "@babel/preset-typescript",
      ],
    },
  },
},

2 Comments

why not use ts-loader with the ts file? @Soshiv Upreti

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.