如何从外部/ components中导入文件

乔伊

我的文件夹结构如下:

在此处输入图片说明

在我的App.Js(位于components文件夹下)中,我具有:

import variables from '/src/EnvVariables/variables.json';

但是,我得到一个错误:

You attempted to import /src/EnvVariables/variables.json which falls
outside of the project src/ directory. Relative imports outside of
src/ are not supported. You can either move it inside src/, or
add a symlink to it from project's node_modules/.

我也尝试过:

import variables from './src/EnvVariables/variables.json';
import variables from '/src/EnvVariables/variables.json';
import variables from '/src/EnvVariables/variables.json';
import variables from '/EnvVariables/variables.json';
import variables from './EnvVariables/variables.json';

所有这些都给出上述错误,或者说“无法解析文件”。

正如他们所说,该文件位于/ src下的文件夹中。但是它仍然告诉我它必须在/ src /目录下。如何导入我的variables.json文件?我不想只是将其放在“组件”下,而是希望更好地组织它。

夏季

您是否尝试过导入相对路径?(具有一个或多个../部分)

import variables from "../EnvVariables/variables.json"

当然,如果您喜欢绝对路径,则可以设置https://webpack.js.org/configuration/resolve/,但是我相信首先应该尝试第一个解决方案

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章