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
Steps to reproduce:
create-react-app my-project
cd my-project
yarn add @craco/craco
cat > craco.config.js
(see configuration below)- replace
react-scripts
tocraco
on 'script' section on package.json (craco start, craco build, etc) - edit file
src/index.js
(replace line 4, see code below) yarn start
craco.config.js
const path = require("path");
module.exports = {
webpack: {
resolve: {
alias: {
"@app": path.resolve(__dirname, "src/"),
}
}
}
};
src/index.js
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();
Current result
Module not found: Can't resolve '@app/App' in 'C:\ReactSandbox\my-project\src
Expected
I'm avoiding call relative path hell, instead of import module like ../../../../FilterComment.js
, it would be clean to write @app/FilterComment.js