Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found (Typescript and baseUrl option)

I am trying to use custom paths in my typescript project, the problem is that webpack doesn't recognize my modules giving me the "webpackMissingModule" error.

I have tried different solutions but nothing works for me. Suggestions? Ideas?

Some packages

"react": "^16.8.4",
"react-dom": "^16.8.4",
"typescript": "3.3.1",

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve"
  },
  "include": ["./src"],
  "exclude": ["node_modules", "build"],
  "extends": "./paths.json"
}

paths.json

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@src/*": ["*"],
      "@components/*": ["components/*"]
    }
  }
}

.env

NODE_PATH=./

Component

import React from "react";
import Hello from "@components/Hello";

const Home = () => (
  <div>
    <h1>Home</h1>
    <Hello />
  </div>
);

export default Home;
like image 273
Grigore Budac Avatar asked Apr 05 '26 18:04

Grigore Budac


1 Answers

Well, I found something that worked for me. It's called tsconfig-paths-webpack-plugin. All you need to do is to import it in your webpack config, connect with the tsconfig.json, and that's all.

The plugin is added in the resolve.plugins array of the exported config, not plugins in the root.

like image 126
Grigore Budac Avatar answered Apr 08 '26 08:04

Grigore Budac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!