[英]Expo react native web gives error import scoped imports
I have a project which is react-native-web
, and the expo is configured on it.我有一个项目
react-native-web
,并在其上配置了 expo 。 The stating script has scoped imports @
.声明脚本的范围为导入
@
。 I see that metro.config.js has aliases for those imports for which expo web is complaining, here is metro.config.js
:我看到 metro.config.js 对那些 expo web 抱怨的导入有别名,这里是
metro.config.js
:
/**
* Metro configuration for React Native
* https://github.com/facebook/react-native
*
* @format
*/
const path = require("path")
const extraNodeModules = {
"@modules": path.resolve(__dirname, "modules"),
"@screens": path.resolve(__dirname, "screens"),
"@options": path.resolve(__dirname, "options")
}
const watchFolders = [
path.resolve(__dirname, "modules"),
path.resolve(__dirname, "screens"),
path.resolve(__dirname, "options")
]
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false
}
})
},
resolver: {
extraNodeModules: new Proxy(extraNodeModules, {
get: (target, name) =>
//redirects dependencies referenced from extraNodeModules to local node_modules
name in target
? target[name]
: path.join(process.cwd(), "node_modules", name)
})
},
watchFolders,
resetCache: true
}
I did some research and I modified my babel.config.js
to this:我做了一些研究,并将我的
babel.config.js
修改为:
module.exports = {
presets: ['babel-preset-expo', 'module:metro-react-native-babel-preset'],
plugins: [
["module:react-native-dotenv", {
"moduleName": "@env",
"path": ".env",
"blacklist": null,
"whitelist": null,
"safe": false,
"allowUndefined": true
},
],
"import-glob",
[
'module-resolver',
{
alias: {
'modules': './modules',
},
}
]
]
};
I know that the above babel is only for modules, it is just for example.我知道上面的 babel 仅适用于模块,仅用于示例。 I am getting this on running
expo start
then w for web:我在运行
expo start
然后 w 运行 web 时得到这个:
./App.js:13
Module not found: Can't resolve '@modules'
11 |
12 | import { screens } from "@screens";
> 13 | import { hooks, slices, navigators, initialRoute } from "@modules";
14 | import { connectors } from "@store";
15 |
16 | const Stack = createStackNavigator()
./App.js:18
Module not found: Can't resolve '@options'
16 | const Stack = createStackNavigator()
17 |
> 18 | import { GlobalOptionsContext, OptionsContext, getOptions } from "@options"
19 |
20 | const getNavigation = (modules, screens, initialRoute) => {
21 | const Navigation = () => {
./App.js:12
Module not found: Can't resolve '@screens'
10 | } from "@reduxjs/toolkit"
11 |
> 12 | import { screens } from "@screens";
13 | import { hooks, slices, navigators, initialRoute } from "@modules";
14 | import { connectors } from "@store";
15 |
./App.js:14
Module not found: Can't resolve '@store'
12 | import { screens } from "@screens";
13 | import { hooks, slices, navigators, initialRoute } from "@modules";
> 14 | import { connectors } from "@store";
15 |
16 | const Stack = createStackNavigator()
Any clues as to how could I make this work?关于如何使这项工作的任何线索?
Actually, you have implemented the absolute-path
configs with some mistakes:实际上,您已经实现了
absolute-path
配置,但存在一些错误:
metro bundler configuration is not related to the absolute-path
feature, remove whatever you added to the metro.config.js
file Metro 捆绑器配置与
absolute-path
功能无关,请删除您添加到metro.config.js
文件中的任何内容
if you are using TypeScript you must modify tsconfig.json
:如果您使用的是 TypeScript 您必须修改
tsconfig.json
:
{ "compilerOptions": { "baseUrl": "./app" /* Base directory to resolve non-absolute module names. */, "paths": { "@screens": ["./screens"] } },
inside babel.config.json
you added the config wrongly, maybe you forgot to add the @
and the root
key in the config file, follow below example:在
babel.config.json
里面你错误地添加了配置,可能你忘记在配置文件中添加@
和root
键,如下例所示:
module.exports = { plugins: [ [ 'module-resolver', { root: ['./app'], alias: { '@screens': './app/screens', }, extensions: [ '.ios.ts', '.ios.tsx', '.android.ts', '.android.tsx', '.ts', '.tsx', '.js', '.jsx', '.json', ], }, ], ], };
Hint: please consider, my application root directory is app
.提示:请考虑,我的应用程序根目录是
app
。 all TypeScript/JavaScript codes are in the app
folder.所有 TypeScript/JavaScript 代码都在
app
文件夹中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.