[英][ERR_MODULE_NOT_FOUND]: Cannot find package imported
I would like to change import from ../../../db/index.js
to db/index.js
我想将导入从
../../../db/index.js
更改为db/index.js
I have already added this setting in my jsconfig.json but I still got this error.我已经在我的 jsconfig.json 中添加了这个设置,但我仍然遇到这个错误。
{
"compilerOptions": {
"module": "commonjs",
"baseUrl": "src"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}
Finally found the right answer after trying different kinds of approaches.在尝试了不同的方法后终于找到了正确的答案。 Eslint import resolver and babel import resolver seem to be not working.
Eslint 导入解析器和 babel 导入解析器似乎不起作用。
Add the ff:添加ff:
package.json package.json
"imports": {
"#root/*": {
"default": "./src/*"
}
},
If you want to access that import directy via ctr+click/left click create jsconfig.json如果您想通过 ctr+click/left click create jsconfig.json访问该导入目录
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"baseUrl": "./src",
"paths": {
"#root/*": ["./*"]
}
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}
Usage in your index.js :在你的index.js中的用法:
import level1 from '#root/level1/index.js';
instead of:代替:
import level1 from './level1/index.js';
https://marian-caikovski.medium.com/4-ways-to-avoid-double-dots-in-module-specifiers-f5b6086cd9d1 https://marian-caikovski.medium.com/4-ways-to-avoid-double-dots-in-module-specifiers-f5b6086cd9d1
These are two very different things:这是两件截然不同的事情:
import { something } from '../../something'
- local import (file you've created) import { something } from '../../something'
- 本地导入(你创建的文件)
import { something } from 'something'
- import from a package (eg installed with yarn add something
import { something } from 'something'
- 从 package 导入(例如用yarn add something
If you'd like to clean up your imports and be able to do something like:如果您想清理导入并能够执行以下操作:
import { something } from '@components/something'
then you need additional plugins/setup. import { something } from '@components/something'
然后你需要额外的插件/设置。 It's worth looking into babel-plugin-root-import
for example.例如,值得研究一下
babel-plugin-root-import
。
There might be other plugins/tools to do that but I've never had the need do do that so I don't know.可能还有其他插件/工具可以做到这一点,但我从来没有需要这样做,所以我不知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.