创建React应用时遭遇循环依赖警告,求解决方案
解决npx create-my-app执行时的循环依赖属性访问警告
问题复现
执行命令:
npx create-my-app app
出现大量类似如下的警告:
(node:2056) Warning: Accessing non-existent property 'cat' of module exports inside circular dependency (Use `node --trace-warnings ...` to show where the warning was created) (node:2056) Warning: Accessing non-existent property 'cd' of module exports inside circular dependency ...
解决方案
这些警告的核心原因是依赖的shelljs包版本与当前Node.js版本不兼容,旧版shelljs的导出逻辑在Node 16+版本中会触发循环依赖与属性访问警告。尝试以下步骤解决:
清除npm全局缓存
npm cache clean --force若曾全局安装过
create-my-app(或create-react-app,可能为笔误),先卸载全局包npm uninstall -g create-my-app使用最新版包创建应用
npx create-my-app@latest app若警告仍存在,手动锁定
shelljs兼容版本:- 在项目根目录的
package.json中添加resolutions字段(需npm 7+或Yarn支持):
"resolutions": { "shelljs": "^0.8.5" }- 重新安装依赖:
npm install- 在项目根目录的
确保使用Node.js LTS版本(如18.x、20.x),避免版本兼容性问题
内容的提问来源于stack exchange,提问作者waqar
相关产品推荐
相关产品推荐

