最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - [ERR_MODULE_NOT_FOUND]: Cannot find package imported - Stack Overflow

programmeradmin0浏览0评论

I would like to change import from ../../../db/index.js to db/index.js

I have already added this setting in my jsconfig.json but I still got this error.

{
    "pilerOptions": {
        "module": "monjs",
        "baseUrl": "src"
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules"
    ]
}

I would like to change import from ../../../db/index.js to db/index.js

I have already added this setting in my jsconfig.json but I still got this error.

{
    "pilerOptions": {
        "module": "monjs",
        "baseUrl": "src"
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules"
    ]
}
Share Improve this question asked Nov 24, 2022 at 9:02 PusoyPusoy 1,1182 gold badges11 silver badges17 bronze badges 1
  • It seems to be working in my client folder (react app). This is on my server folder. – Pusoy Commented Nov 24, 2022 at 9:05
Add a ment  | 

2 Answers 2

Reset to default 3

Finally found the right answer after trying different kinds of approaches. Eslint import resolver and babel import resolver seem to be not working.

Add the ff:

package.json

"imports": {
    "#root/*": {
        "default": "./src/*"
    }
},

If you want to access that import directy via ctr+click/left click create jsconfig.json

{
    "pilerOptions": {
        "target": "esnext",
        "module": "monjs",
        "baseUrl": "./src",
        "paths": {
            "#root/*": ["./*"]
        }
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules"
    ]
}

Usage in your index.js:

import level1 from '#root/level1/index.js';

instead of:

import level1 from './level1/index.js';

https://marian-caikovski.medium./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 from a package (e.g. installed with yarn add something

If you'd like to clean up your imports and be able to do something like:

import { something } from '@ponents/something' then you need additional plugins/setup. It's worth looking into babel-plugin-root-import for example.

There might be other plugins/tools to do that but I've never had the need do do that so I don't know.

发布评论

评论列表(0)

  1. 暂无评论