为什么在尝试从另一个 TypeScript 文件导入常量时会显示此错误?

huangapple go评论59阅读模式
英文:

Why is this error showing when trying to import a constant from another file in typescript?

问题

I am trying to import a constant which is a JSON body from a file inside another folder inside my project.
我正在尝试从项目中的另一个文件夹内导入一个作为JSON主体的常量。

I have tried copying the relative path to it and the constant has the export keyword before the declaration so I have export constant = {}
我已尝试复制其相对路径,并且在声明之前该常量具有导出关键字,所以我有导出常量 = {}

In the main file I am getting the Cannot find module error and I am running out of ideas as I have tried looking on google for possible fixes.
在主文件中,我遇到了“找不到模块”的错误,我已经没有更多的想法,因为我已经尝试在Google上寻找可能的修复方法。

The files skeleton

**The import statement: **
导入语句:

import {post_next_questions_request_body} from '..post-calls-bodies/post-next-questions';
import {post_next_questions_request_body} from '..post-calls-bodies/post-next-questions';

The error:
错误:

Cannot find module '..post-calls-bodies/post-next-questions' or its corresponding type declarations.
找不到模块'..post-calls-bodies/post-next-questions'或其对应的类型声明。

I have tried the following paths:
我已尝试以下路径:

'..post-calls-bodies/post-next-questions'
'..post-calls-bodies/post-next-questions'
'.post-calls-bodies/post-next-questions'
'post-calls-bodies/post-next-questions'

with no success
但都没有成功。

英文:

I am trying to import a constant which is a JSON body from a file inside another folder inside my project.
I have tried copying the relative path to it and the constant has the export keyword before the declaration so I have export constant = {}

In the main file I am getting the Cannot find module error and I am running out of ideas as I have tried looking on google for possible fixes.

The files skeleton


**The import statement: **

import {post_next_questions_request_body} from '..post-calls-bodies/post-next-questions'; 

The error:

Cannot find module '..post-calls-bodies/post-next-questions' or its corresponding type declarations.

I have tried the following paths:
'..post-calls-bodies/post-next-questions'
'.post-calls-bodies/post-next-questions'
'post-calls-bodies/post-next-questions'

with no success

答案1

得分: 0

"Either you didn't import it properly or you it should be

import {post_next_questions_request_body} from '../post-calls-bodies/post-next-questions'; 

Make sure that the path is correct."

英文:

Either you didn't import it properly or you it should be

 import {post_next_questions_request_body} from '../post-calls-bodies/post-next-questions'; 

Make sure that the path is correct.

答案2

得分: 0

你没有明确指定你在哪个文件中写入import语句 - 我会假设你正在next-questions-api-tests中编写你的导入。我知道我听起来很苛刻,但提供这种程度的文本解释会让人们更容易回答,因为我不得不在另一个选项卡中打开图片才能理解你的问题。盲人无法看到那张图片,而我们所有人都无法从中复制和粘贴文本。

无论如何,你的相对路径语法是错误的(你需要在...后面加上斜杠,如../rest/of/the/path./rest/of/the/path)。

此外,如果我的最初的假设是正确的,路径本身也是错误的:你需要向上移动两级。向上移动一次会将你放在tests文件夹中(在API Tests文件夹之外)。再向上移动一次将允许你进入post-call-bodies文件夹。最终,你需要:

import { post_next_questions_request_body } from '../../post-call-bodies/post-next-questions';

附言:如果你没有一个很好的理由,我经验来看,将一个文件夹命名为API Tests不是最佳选择。让我感到困扰的是空格。在许多脚本和程序中,它是麻烦的根源,如果你不真的需要它,类似于APITests或(甚至更好的)api_tests将更加健壮。

英文:

You didn't specified clearly which file you're writing the import statement in - I'm going to assume you're writing your import in next-questions-api-tests. I know I sound pedantic, but providing that level of textual explanation would make it easier for people to answer, since I had to open a picture in another tab to understand your question. Blind people cannot see that picture, and all of us can't copy and paste text from it.

Anyway, your syntax for relative paths is wrong (you need a slash after .. or ., as in ../rest/of/the/path and ./rest/of/the/path).

Also, if my initial assumptions are right, the path itself is wrong: you need to go up two levels. Moving up once will put you in tests folder (just outside API Tests folder). Moving up once more will allow you then to go down inside post-call-bodies. In the end, you need:

import { post_next_questions_request_body } from '../../post-call-bodies/post-next-questions';

P.S.: If you don't have a good reason, calling a folder API Tests is not the best in my experience. What bothers me is the whitespace. It's a source of trouble in so many scripts and programs that if you don't really need it, something like APITests or (even better) api_tests would be more robust.

huangapple
  • 本文由 发表于 2023年5月7日 07:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定