如何避免在 create-response-app 中使用相对路径导入(/. ./. ./. ./redux/action/action1)

我一直在使用创建反应应用程序软件包来创建一个反应网站。我在整个应用程序中使用相对路径来导入组件、资源、还原等,例如 import action from '../../../redux/action

我试过使用 模块-阿里斯 npm 包,但没有成功。有没有什么插件,我可以用来导入基于文件夹名或别名,即一个绝对路径?

例如,import action from '@redux/action'import action from '@resource/css/style.css'

63636 次浏览

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';

Webpack 2 Resolve Reference

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!

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.

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

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'

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",
// ...
}

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.

Feb 2010
Wasted about an hour on this. An example is below:

Goal: Import ABC0 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";

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"
]
}