Sachin Khokhar
Sachin Khokhar

Reputation: 100

Webpack throwing error "You may need an appropriate loader" in react with typescript

I tried to setup a new project with webpack, react, typescript. But I am unable to get it working.

I have tried html loader and tried various tutorials to solve the issue.

Webpack config:

const path = require("path");

module.exports = {
  entry: {
    app: ["./src/app"]
  },
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        use: [
          {
            loader: "ts-loader",
            options: {
              transpileOnly: true,
              compilerOptions: {
                module: "es2015",
                allowJs: true
              }
            }
          }
        ],
        include: [path.resolve(path.resolve(__dirname, "../"), "./src")]
      }
    ]
  },
  resolve: {
    extensions: [".js", ".jsx", ".ts", ".tsx"]
  }
};

tsconfig.json

{
    "compilerOptions": {
        "baseUrl": ".",
        "moduleResolution": "node",
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false,
        "jsx": "react",
        "allowJs": true,
        "noImplicitThis": true,
        "removeComments": false,
        "skipDefaultLibCheck": true,
        "skipLibCheck": true,
        "lib": ["dom", "es5", "scripthost", "es2015", "es2017"],
        "downlevelIteration": true,
        "allowSyntheticDefaultImports": true,
        "experimentalDecorators": true,
        "esModuleInterop": true,
        "outDir": "./dist/"
    }
}

app.tsx

import * as React from "react";
import * as ReactDOM from "react-dom";
ReactDOM.render(<></>, document.getElementById("root"));

But while compiling with : webpack --mode production :- throws error :

ERROR in ./src/app.tsx 4:16 Module parse failed: Unexpected token (4:16) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders | import * as ReactDOM from "react-dom"; | //import { Home } from "./pages/home";

ReactDOM.render(<>, document.getElementById("root")); | @ multi ./src/app app[0]

Upvotes: 4

Views: 1764

Answers (1)

Sachin Khokhar
Sachin Khokhar

Reputation: 100

i tried it and this include is the problem. include: [path.resolve(path.resolve(__dirname), "./src")]... no need to go back from dirname and it will work .

Credits: @JurajKocan

Upvotes: 0

Related Questions