簡體   English   中英

[ERR_MODULE_NOT_FOUND]:找不到導入的 package

[英][ERR_MODULE_NOT_FOUND]: Cannot find package imported

我想將導入從../../../db/index.js更改為db/index.js

在此處輸入圖像描述

我已經在我的 jsconfig.json 中添加了這個設置,但我仍然遇到這個錯誤。

{
    "compilerOptions": {
        "module": "commonjs",
        "baseUrl": "src"
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules"
    ]
}

在嘗試了不同的方法后終於找到了正確的答案。 Eslint 導入解析器和 babel 導入解析器似乎不起作用。

添加ff:

package.json

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

如果您想通過 ctr+click/left click create jsconfig.json訪問該導入目錄

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

在你的index.js中的用法:

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

代替:

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

https://marian-caikovski.medium.com/4-ways-to-avoid-double-dots-in-module-specifiers-f5b6086cd9d1

這是兩件截然不同的事情:

import { something } from '../../something' - 本地導入(你創建的文件)

import { something } from 'something' - 從 package 導入(例如用yarn add something

如果您想清理導入並能夠執行以下操作:

import { something } from '@components/something'然后你需要額外的插件/設置。 例如,值得研究一下babel-plugin-root-import

可能還有其他插件/工具可以做到這一點,但我從來沒有需要這樣做,所以我不知道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM