javascript - Nextjs 从父目录导入外部组件
问题描述
我有外部目录common
,我想将该目录中的反应组件导入web-static
. 在web-static
我使用nextjs。
目前我有这个错误:
Module not found: Can't resolve 'react' in '/Users/jakub/Sites/WORK/wilio-web-common/common/src/@vendor/atoms'
我将这些行添加到next.config.js
:
const babeRule = config.module.rules[babelRuleIndex];
if (babeRule && babeRule.test) {
babeRule.test = /\.(web\.)?(tsx|ts|js|mjs|jsx)$/;
if (babeRule.include) {
babeRule.include = [
...babeRule.include,
resolve(__dirname, "../common/src/@vendor"),
];
}
}
config.resolve.alias = {
...config.resolve.alias,
"@vendor": resolve(__dirname, "../common/src/@vendor")
};
我的tsconfig.json
文件:
"paths": {
"@vendor/*": ["../common/src/@vendor/*"]
}
Webpack 可以解析这些组件,但无法解析这些组件中已安装的包。
../common/src/@vendor/atoms/Test.js
Module not found: Can't resolve 'react' in '/Users/user1/Sites/WORK/web-app/common/src/@vendor/atoms'
我是否需要将此目录也包含在 webpacks 中config.externals
?当前的 nextjs webpackexternals
-----
options.isServer: false
[ 'next' ]
-----
-----
options.isServer: true
[ [Function] ]
-----
怎么可能做到这一点?谢谢你的帮助
解决方案
推荐阅读
- excel - Excel公式sumproduct需要优化
- c++ - 为什么线程对类变量所做的更改没有影响?
- apache-spark - 在 HDP 多节点集群上使用 yarn-client 的 Spark 仅在同一单个节点上启动执行程序
- vba - VBA 代码帮助——从表格生成单独的 PDF
- docker - gitlab-ci 和 rancher 残差图像
- css - 类似于 Flatlist 组件的网格,其标题看起来与行相同
- python - Pandas:为列的每个唯一值创建行,即使缺少数据
- php - 显示带有 $_POST 的表单?
- sql - “全选”的 URL CAll 参数值
- r - R - 在数据框中使用 NA 计数