[英]JavaScript on Next.js and React import error "Unable to resolve path to module '../utils/makeId'.eslintimport/no-unresolved"
I'm following along a course and the instructor doesn't have this error while my code is identical to his.我正在学习一门课程,当我的代码与他的代码相同时,讲师没有出现此错误。 I've got a utility function in the /utils folder which is in the same folder as the index.js that is importing../utils/makeId.
我在 /utils 文件夹中有一个实用程序 function,它与正在导入的 index.js 位于同一文件夹中../utils/makeId。 I'm using eslint and everything both files are.js files but it's not importing.
我正在使用 eslint,这两个文件都是 .js 文件,但它没有导入。
import { useState, useEffect, useRef } from 'react';
import { Banner, CreatorCard } from '../components';
import images from '../assets';
import { makeId } from '../utils/makeId';
const Home = () => {
const parentRef = useRef(null);
const scrollRef = useRef(null);
return (
<div className="flex justify-center sm:px-4 p-12">
<div className="w-full minmd:w-4/5">
<Banner
name="Discover, collect, and sell extraordinary NFTs"
childStyles="md:text-4xl sm:text-2xl xs=text-xl text-left"
parentStyles="justify-start mb-6 h-72 sm:h-60 p-12 xs:p-4 xs:h-44 rounded-3xl"
/>
<div>
<h1 className="font-poppins dark:text-white text-nft-black-1 text-2xl minlg:text-4xl font-semibold ml-4 xs:ml-0">Best Creators</h1>
<div
className="relative flex-1 max-w-full flex mt-3"
ref={parentRef}
>
<div
className="flex flex-row w-max overflow-x-scroll no-scrollbar select-none"
ref={scrollRef}
>
{[6, 7, 8, 9, 10].map((i) => (
<CreatorCard
key={`creator-${i}`}
rank={i}
creatorImage={images[`creator${i}`]}
creatorName={`0x${makeId(3)}...${makeId(4)}}`}
/>
))}
</div>
</div>
</div>
</div>
</div>
);
};
export default Home;
makeId.js makeId.js
export const makeId = (length) => {
let result = '';
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i += 1) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
I tried adding extension settings in my eslintrc.js I found on a similar post;我尝试在类似帖子中找到的 eslintrc.js 中添加扩展设置; while that fixed my import error I would still get a compile error on the website.
虽然这修复了我的导入错误,但我仍然会在网站上遇到编译错误。
The problem was it needed to be './utils/makeId' instead of '../utils/makeId', I'm not sure why but I guess because /utils is in the same folder as index.js the proper syntax is./ not../.问题是它需要是 './utils/makeId' 而不是 '../utils/makeId',我不确定为什么,但我猜是因为 /utils 与 index.js 在同一个文件夹中,正确的语法是。/ 不是../。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.