reactjs - create-react-app 无法解析模块
问题描述
我正在升级旧的 react 应用程序以使用最新最好的应用程序,但我似乎无法弄清楚为什么我的所有模块都无法解决。
似乎在某些时候您可以简单地指向导入中的文件夹,现在它不再解析
在我的 Webpack 中,我相信我已经正确设置了它。有人知道进口有什么变化吗?
resolve: {
// This allows you to set a fallback for where Webpack should look for modules.
// We placed these paths second because we want `node_modules` to "win"
// if there are any conflicts. This matches Node resolution mechanism.
// https://github.com/facebookincubator/create-react-app/issues/253
modules: ["node_modules", paths.appNodeModules].concat(
// It is guaranteed to exist because we tweak it in `env.js`
process.env.NODE_PATH.split(path.delimiter).filter(Boolean),
path.resolve(__dirname, "src")
),
// These are the reasonable defaults supported by the Node ecosystem.
// We also include JSX as a common component filename extension to support
// some tools, although we do not recommend using it, see:
// https://github.com/facebookincubator/create-react-app/issues/290
// `web` extension prefixes have been added for better support
// for React Native Web.
extensions: [
".web.js",
".mjs",
".js",
".json",
".web.jsx",
".jsx",
".scss",
".css",
".json"
]
解决方案
问题是 path.resolve(__dirname, "src") 似乎没有解决我认为的问题。
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
module.exports = {
dotenv: resolveApp('.env'),
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
appIndexJs: resolveApp('src/main.jsx'),
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
yarnLockFile: resolveApp('yarn.lock'),
testsSetup: resolveApp('src/setupTests.js'),
appNodeModules: resolveApp('node_modules'),
publicUrl: getPublicUrl(resolveApp('package.json')),
servedPath: getServedPath(resolveApp('package.json')),
};
modules: ["node_modules", paths.appNodeModules].concat(
// It is guaranteed to exist because we tweak it in `env.js`
process.env.NODE_PATH.split(path.delimiter).filter(Boolean),
paths.appSrc
),
// These are the reasonable defaults supported by the Node ecosystem.
// We also include JSX as a common component filename extension to support
// some tools, although we do not recommend using it, see:
// https://github.com/facebookincubator/create-react-app/issues/290
// `web` extension prefixes have been added for better support
// for React Native Web.
extensions: [
".web.js",
".mjs",
".js",
".json",
".web.jsx",
".jsx",
".scss",
".css",
".json"
]
推荐阅读
- rcpp - 我有一个数据框,其中的列没有名称,我想在 RCPP 中命名它们?我该怎么做?
- python - Pyspark:在不同的日子里发牢骚
- windows - 在 Win32 中平滑 WM_NCPAINT
- laravel - 控制台错误:Moment 时区需要 Moment.js。在 Vue 中
- python - ModelChoiceFilter 在 Django 中抛出错误
- node.js - Docker 中的 Keycloak、Nodejs API 和 Traefik -> 只有 403
- python - Qt:防止在子小部件上拖放并显示禁止光标
- javascript - 用卡片实现 Vue 可拖动?
- google-drive-api - G Suite Marketplace 安装数和应用用户数不匹配
- java - 如何使用 Java 流有条件地设置当前项目值?