score:120
create a file called .env
in the project root and write there:
node_path=src
then restart the development server. you should be able to import anything inside src
without relative paths.
note i would not recommend calling your folder src/redux
because now it is confusing whether redux
import refers to your app or the library. instead you can call your folder src/app
and import things from app/...
.
we intentionally don't support custom syntax like @redux
because it's not compatible with node resolution algorithm.
score:0
none of the answers worked for me. some didn't work at all and others worked but the import was already inside src
, for example:
import something from 'path/to/file'
.
whereas i wanted to be able to do:
import something from 'src/path/to/file'
here is how i solved it:
tsconfig.json
{
"compileroptions": {
// ...
"baseurl": ".",
"rootdirs": [
"src"
]
},
"include": [
"src"
]
}
score:1
i am using babel-plugin-module-resolver for my project to resolve that problem. babel-plugin-module-resolver also is the same as module-alis. so i think you should just resolve using module-alis problem.
because you didn't tell us why using module-alis was fail? so i cant show you how to fix it.
dont give up your solution while you dont know the reason!
score:1
in package.json file,
eject this code in the scripts object like this..
"scripts": {
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"test": "node scripts/test.js --env=jsdom",
"eject": "node_path=src/ react-scripts eject"
},
this will enable the absolute path imports in your app
score:1
the alias solution for craco or rewired create-react-app is react-app-alias for systems as: craco, react-app-rewired, customize-cra
according docs of mentioned systems replace react-scripts
in package.json
and configure next:
react-app-rewired
// config-overrides.js
const {aliaswebpack, aliasjest} = require('react-app-alias')
const options = {} // default is empty for most cases
module.exports = aliaswebpack(options)
module.exports.jest = aliasjest(options)
craco
// craco.config.js
const {cracoaliasplugin} = require('react-app-alias')
module.exports = {
plugins: [
{
plugin: cracoaliasplugin,
options: {}
}
]
}
all
configure aliases in json like this:
// tsconfig.paths.json
{
"compileroptions": {
"baseurl": ".",
"paths": {
"example/*": ["example/src/*"],
"@library/*": ["library/src/*"]
}
}
}
and add this file in extends
section of main typescript config file:
// tsconfig.json
{
"extends": "./tsconfig.paths.json",
// ...
}
score:2
feb 2010
wasted about an hour on this.
an example is below:
goal: import app.css
in homepage.js
myapp\src\app.css
myapp\src\pages\homepage.js
file: jsconfig.json
{
"compileroptions": {
"baseurl": "src"
}
}
file: src\pages\homepage.js
import "app.css";
score:6
we can use webpack 2 resolve property in the webpack config.
sample webpack config using resolve :
here component and utils are independent folder containing react components.
resolve: {
modules: ['src/scripts', 'node_modules'],
extensions: ['.jsx', '.js'],
unsafecache: true,
alias: {
components: path.resolve(__dirname, 'src', 'scripts', 'components'),
utils: path.resolve(__dirname, 'src', 'scripts', 'utils'),
}
}
after that we can import directly in files :
import uiutils from 'utils/uiutils';
import tabcontent from 'components/tabcontent';
score:6
after you try ben smith's solution above if you find eslint complains about importing absolute path add the following line to your eslint config:
settings: {
'import/resolver': {
node: {
paths: ['src'],
},
},
},
replace 'src' with your folder if you use your own boilerplate with your folder's name.
score:97
the approach in the accepted answer has now been superseded. create react app now has a different way to set absolute paths as documented here.
to summarise, you can configure your application to support importing modules using absolute paths by doing the following:
create/edit your jsconfig.json/tsconfig.json in the root of your project with the following:
{
"compileroptions": {
"baseurl": "src"
},
"include": ["src"]
}
once you have done this you can then import by specifying subdirectories of "src" (in the following example, components is a subdirectory of src) e.g.
import button from 'components/button'
Source: stackoverflow.com
Related Query
- How to avoid using relative path imports (/../../../redux/action/action1) in create-react-app
- React Redux - How to dispatch an action on componentDidMount when using mapDispatchToProps in a connected component
- How to indicate "loading" state for an async action during first render using redux
- How to add action to functional component using Redux and ReactJS
- How to pass Dispatch to action when using redux form
- How do I avoid using separate _PENDING _FULFILLED and _REJECTED actions with redux thunk?
- How to wait for action and API call to complete using redux saga?
- How to dispatch action when a state changes in redux toolkit without using useEffect?
- How to dispatch an action just if another action was correctly dispatched ? Using redux thunk (react)
- When using React / Redux, how is dispatching an action that merely changes the Redux state vs actually doing some task?
- how to match a returned Promise in using Jest with redux action
- How do I avoid infinite loop when using Redux state in useEffect dependency array?
- How to call action in button click of component, Even though i'm using connect function which is not calling in React Redux
- How do I avoid using the && logical operator in mapStateToProps in Redux
- how to avoid undefined data when getting data from redux using useSelector?
- How to redirect to a different page after a redux action using react
- How do I use a relative path to load my images for my website using webpack?
- How to manage submission errors on react final form when using a redux action on onSubmit?
- How to dispatch async action in Redux using Redux Thunk: TypeError: Cannot read property 'loading' of undefined
- How to stub async action in componentDidMount for react redux connected component test using Jest
- How do I avoid 'Function components cannot be given refs' when using react-router-dom?
- How to listen for specific property changes in Redux store after an action is dispatched
- How to dispatch Redux action from stateless component when route is loaded?
- How to divide the logic between Redux reducers and action creators?
- How to use absolute path to import custom scss, when using react + webpack?
- How to dispatch an action when clicking on Link when using React-Router & Redux?
- How to fire periodic actions using setTimeout and dispatcher in redux
- How to reset state of Redux Store when using configureStore from @reduxjs/toolkit?
- How do I resolve 'Property 'type' is missing in type 'AsyncThunkAction' using Redux Toolkit (with TypeScript)?
- how to import LESS files from specific path using webpack less-loader?
More Query from same tag
- webpack dev server CORS issue
- How to make a component show/hide on scroll in react.js
- npm install error: what does this mean exactly? How to fix it?
- How to cause a component to re render in react when using a hook which holds some state
- CSS Styling Pie Chart / Doughnut chart
- Profiling React app in Chrome DevTools - Profiling not supported
- How to fix a css navigation flashing issue
- How to get address based on lat lang lng in react google map
- How to change values of an array from true to false one by one in react?
- Unable to send formData to nodejs server
- CSS keyframe animation in iOS Safari/Chrome/Firefox flickers/out of sync
- I want to create a function that return a tree from a array of objects
- #Antd# How can I bind self-made component with Form.item
- Redirecting the url after login (react js)
- How do I Increment a React Prop
- How to show a loading bar when changing routes in NextJS?
- How do i display all the data in products while sorting like I've done?
- React Enzyme - Testing for a Value of a Specific Prop
- How can I dynamically loading external configuration settings in a React JS app?
- Using HTMLFormElement.reset() with Downshift and react-hook-form
- Using useState React hooks and calling other functions
- Show list of data in component material UI ReactJS
- How to use Observable.if correctly in redux-observable?
- How do I implement Firebase authentication with local state with hooks?
- Iterating through an array of objects in react to display a vertical scrolling item (Name & Logo)
- Category List in React
- My article state is not returning the proper data?
- How to change the value of a React text input using jQuery?
- Why does componentDidUpdate not see newly inserted dom nodes?
- adding onClick functionality to React to do list