admin管理员组文章数量:1025217
I would like to use absolute imports to import any file relative to the root src
directory. This is my folder structure:
example/
┣ public/
┃ ┣ index.html
┣ src/
┃ ┣ ponents/
┃ ┣ ┣ App.css
┃ ┣ ┣ App.js
┃ ┣ ┗ Todo.js
┃ ┣ api.js
┃ ┣ history.js
┃ ┗ index.js
┣ craco.config.js
┣ jsconfig.json
And here is my config
var path = require("path");
module.exports = {
resolve: {
alias: {
src: path.resolve(__dirname, "src"),
},
},
};
import Todo from "src/ponents/Todo"
But I get this error.
Module not found: Can't resolve 'src/ponents/Todo' in 'C:\_MyFiles\Programming\example\src\ponents'
However if I change the config and import to reference a subfolder under src like this it works...
var path = require("path");
module.exports = {
resolve: {
alias: {
ponents: path.resolve(__dirname, "src/ponents"),
},
},
};
import Todo from "ponents/Todo"
How can I get the 1st variation working, so the imports reference the src folder instead of its subfolders?
I would like to use absolute imports to import any file relative to the root src
directory. This is my folder structure:
example/
┣ public/
┃ ┣ index.html
┣ src/
┃ ┣ ponents/
┃ ┣ ┣ App.css
┃ ┣ ┣ App.js
┃ ┣ ┗ Todo.js
┃ ┣ api.js
┃ ┣ history.js
┃ ┗ index.js
┣ craco.config.js
┣ jsconfig.json
And here is my config
var path = require("path");
module.exports = {
resolve: {
alias: {
src: path.resolve(__dirname, "src"),
},
},
};
import Todo from "src/ponents/Todo"
But I get this error.
Module not found: Can't resolve 'src/ponents/Todo' in 'C:\_MyFiles\Programming\example\src\ponents'
However if I change the config and import to reference a subfolder under src like this it works...
var path = require("path");
module.exports = {
resolve: {
alias: {
ponents: path.resolve(__dirname, "src/ponents"),
},
},
};
import Todo from "ponents/Todo"
How can I get the 1st variation working, so the imports reference the src folder instead of its subfolders?
Share Improve this question asked May 3, 2020 at 20:28 pyjamaspyjamas 5,4377 gold badges50 silver badges94 bronze badges1 Answer
Reset to default 4You can use:
resolve: {
modules: [path.resolve('./src'), path.resolve('./node_modules')],
extensions: ['', '.js', '.jsx']
}
This will create an alias for all the subdirectories in your src
and node_modules
folders as well as your src
folder itself.
So you'll be able to use
Import App from 'ponents/App'
and
Import history from 'history'
I would like to use absolute imports to import any file relative to the root src
directory. This is my folder structure:
example/
┣ public/
┃ ┣ index.html
┣ src/
┃ ┣ ponents/
┃ ┣ ┣ App.css
┃ ┣ ┣ App.js
┃ ┣ ┗ Todo.js
┃ ┣ api.js
┃ ┣ history.js
┃ ┗ index.js
┣ craco.config.js
┣ jsconfig.json
And here is my config
var path = require("path");
module.exports = {
resolve: {
alias: {
src: path.resolve(__dirname, "src"),
},
},
};
import Todo from "src/ponents/Todo"
But I get this error.
Module not found: Can't resolve 'src/ponents/Todo' in 'C:\_MyFiles\Programming\example\src\ponents'
However if I change the config and import to reference a subfolder under src like this it works...
var path = require("path");
module.exports = {
resolve: {
alias: {
ponents: path.resolve(__dirname, "src/ponents"),
},
},
};
import Todo from "ponents/Todo"
How can I get the 1st variation working, so the imports reference the src folder instead of its subfolders?
I would like to use absolute imports to import any file relative to the root src
directory. This is my folder structure:
example/
┣ public/
┃ ┣ index.html
┣ src/
┃ ┣ ponents/
┃ ┣ ┣ App.css
┃ ┣ ┣ App.js
┃ ┣ ┗ Todo.js
┃ ┣ api.js
┃ ┣ history.js
┃ ┗ index.js
┣ craco.config.js
┣ jsconfig.json
And here is my config
var path = require("path");
module.exports = {
resolve: {
alias: {
src: path.resolve(__dirname, "src"),
},
},
};
import Todo from "src/ponents/Todo"
But I get this error.
Module not found: Can't resolve 'src/ponents/Todo' in 'C:\_MyFiles\Programming\example\src\ponents'
However if I change the config and import to reference a subfolder under src like this it works...
var path = require("path");
module.exports = {
resolve: {
alias: {
ponents: path.resolve(__dirname, "src/ponents"),
},
},
};
import Todo from "ponents/Todo"
How can I get the 1st variation working, so the imports reference the src folder instead of its subfolders?
Share Improve this question asked May 3, 2020 at 20:28 pyjamaspyjamas 5,4377 gold badges50 silver badges94 bronze badges1 Answer
Reset to default 4You can use:
resolve: {
modules: [path.resolve('./src'), path.resolve('./node_modules')],
extensions: ['', '.js', '.jsx']
}
This will create an alias for all the subdirectories in your src
and node_modules
folders as well as your src
folder itself.
So you'll be able to use
Import App from 'ponents/App'
and
Import history from 'history'
本文标签: javascriptWebpack alias for src folder in React appStack Overflow
版权声明:本文标题:javascript - Webpack alias for src folder in React app? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745509526a2153761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论