question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Aliased imports are not supported anymore when creating a new typescript react app

See original GitHub issue

I created a new React-typescript app via this command with react@17.0.2 and typescript@4.5.2:

npx create-react-app my-app --template typescript

Then I decided to define the alias path as I did many times before. I created the tsconfig.paths.json in the root of my app.

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

Then I added the extend property to the tsconfig.json file:

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

Also, I installed react-app-rewired@2.1.8 and created the config-override.js:

const path = require('path');

module.exports = function override(config) {
	config.resolve = {
		...config.resolve,
		alias: {
			...config.alias,
			'components': path.resolve(__dirname, 'src/components/*'),
			'routes': path.resolve(__dirname, 'src/routes/*'),
			'constants': path.resolve(__dirname, 'src/constants/*'),
		}
	}
	return config;
}

So I reopened my IDE (VSCode) and ran npm start. I must mention I changed scripts in the package.json before running the start command.

{
 .
 .
 .
  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-app-rewired eject"
  }
 .
 .
 .
}

After running the start command, this message was displayed in a terminal, and compiling is failed:

The following changes are being made to your tsconfig.json file: -compilerOptions.paths must not be set (aliased imports are not supported)

*Failed to compile.

./src/App.tsx Module not found: Can’t resolve ‘components’ in …*

So I started to search about this issue to resolve it. I found many approaches that I will mention some of them below:

1. Go to your jsconfig.json file add the base URL to be “.”

"compilerOptions": {
   "baseUrl": ".",
   ...

Then you can directly import stuff from the src directory

import Myfile from "src/myfile.js"

Result ==> DID NOT WORK!

2. This problem is solved by an alias for rewire. Install react-app-rewire-alias then create config-override.js file:

     const {alias, configPaths} = require('react-app-rewire-alias')

     module.exports = function override(config) {
      alias(configPaths())(config)
      return config
     }

Result ==> DID NOT WORK!

3. Using craco@6.4.1 package

  1. Install craco and craco-alias npm install @craco/craco --save and npm i -D craco-alias

  2. Create tsconfig.paths.json in root directory

    {
        "compilerOptions": {
            "baseUrl": "./src",
            "paths": {
               "@components/*" : ["./components/*"]
             }
        }
    }
    
  3. Extend tsconfig.paths.json in tsconfig.json

    { “extends”: “./tsconfig.paths.json”, //default configs… }

  4. Create craco.config.js in the root directory

    const CracoAlias = require("craco-alias");
    
    module.exports = {
       plugins: [
         {
            plugin: CracoAlias,
            options: {
               source: "tsconfig",
               // baseUrl SHOULD be specified
               // plugin does not take it from tsconfig
               baseUrl: "./src",
               /* tsConfigPath should point to the file where "baseUrl" and "paths" 
               are specified*/
               tsConfigPath: "./tsconfig.paths.json"
            }
         }
      ]
    };
    
  5. in package.json swap "start": "react-scripts start" with "start": "craco start"

Result ==> DID NOT WORK!

I’m confused because I used the alias path many times before, but it does not work now. I don’t want to eject my app but using the alias path is helpful.

This is my question in the Stackoverflow community.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:36
  • Comments:14

github_iconTop GitHub Comments

62reactions
Aleksey-Danchincommented, Apr 23, 2022

It’s 2022 year. React 18 released, CRA v5 released, nodejs imports option exists, but alias from box not supported =)

8reactions
jonathangrafcommented, Jun 4, 2022

Using Craco could be a short-term solution, but why do they not work out of the box anymore? Weird stuff

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Aliased imports are not supported anymore when ...
Aliased imports are not supported anymore when creating a new typescript react app ; 1. Go to your jsconfig.json file add the base...
Read more >
Aliased imports are not supported anymore when creating a ...
Coding example for the question Aliased imports are not supported anymore when creating a new typescript react app-Reactjs.
Read more >
How to Configure a Path Alias in a React Typescript App for ...
Path Alias in Typescript is a way to resolve imports like @/component/Button, and make the project imports cleaner and consistent!
Read more >
Create React App: import modules using aliases ... - Wavelop
Using Webpack and Typescript is possible to forget relative paths and to use aliases for better developer experience.
Read more >
Documentation - Module Resolution - TypeScript
js instead used a non-relative path and had the import var x = require("moduleB"); . Node would then try to resolve moduleB to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found