I have a problem on set webpack alias path using create-react-app and craco, already googling it but can't solve the problem.
I got an error Module not found: Can't resolve '@app/App' in 'C:\ReactSandbox\my-project\src
everytime i run application using command yarn start
create-react-app my-project
cd my-project
yarn add @craco/craco
cat > craco.config.js
(see configuration below)
react-scripts
to craco
on 'script' section on package.json (craco start, craco build, etc)
src/index.js
(replace line 4, see code below)
yarn start
const path = require("path");
module.exports = {
webpack: {
resolve: {
alias: {
"@app": path.resolve(__dirname, "src/"),
}
}
}
};
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from '@app/App'; //replace './App' into '@app/App'
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
serviceWorker.unregister();
Module not found: Can't resolve '@app/App' in 'C:\ReactSandbox\my-project\src
I'm avoiding call relative path hell, instead of import module like ../../../../FilterComment.js
, it would be clean to write @app/FilterComment.js
GitHub - risenforces/craco-alias: A craco plugin for automatic aliases generation for Webpack and Jest. Product. Actions.
Now, anywhere on your system, you can run the create-react-app command to initiate the setup for a new Webpack-powered React app. The completed version of the code for this next section is available in heart-webpack-complete . This will create the boilerplate for the new app and install the app's dependencies.
Create React App (CRA) ships with webpack already under the hood, but usually, we would need to add more configurations as our app grows. Luckily for us, we can create a webpack. config.
resolve for carco:
u need install craco-alias, then will write in craco.config.js
const CracoAlias = require('craco-alias')
module.exports = {
plugins: [
{
plugin: CracoAlias,
options: {
source: 'tsconfig',
baseUrl: '.',
tsConfigPath: './tsconfig.path.json',
},
},
],
}
tsconfig.path.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"],
"@svg/*": ["src/assets/svg/*"],
"@img/*": ["src/assets/images/*"],
"@icons/*": ["src/assets/icons/*"],
"@shared/*": ["src/shared/*"],
"@components/*": ["src/components/*"],
"@hooks/*": ["src/hooks/*"],
"@constants/*": ["src/constants/*"],
"@layout/*": ["src/layout/*"],
"@services/*": ["src/services/*"]
}
}
}
tsconfig.json
{
"extends": "./tsconfig.path.json",
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"checkJs": false,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true,
"removeComments": true
},
"include": [
"src",
"src/configs",
"react-app-env.d.ts"
]
}
create separate file tsconfig.paths.json
and add aliases:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@utils/*": ["utils/*"]
}
}
}
Add created tsconfig.paths.json
to main tsconfig.json
{
"extends": "./tsconfig.paths.json",
... other staff ...
}
Add aliases to config-overrides.js
const {
override,
addWebpackAlias
} = require('customize-cra');
const path = require("path");
module.exports = override(
addWebpackAlias({
"@utils": path.resolve(__dirname, "./src/utils"),
}),
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With