You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行npm run format报错:../npm/bin/npx-cli.js不存在,寻求解决方案

解决npm run format时npx-cli.js不存在的错误

问题场景

我用npx create-react-app my-app --template typescript创建了React+TypeScript项目,Node.js版本v18.15.0。按Prettier官方步骤配置后,在package.json里添加了format脚本:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "format": "npx prettier . --write"
  }

执行npm run format时出现错误:

> my-app@0.1.0 format
> npx prettier . --write

<PATH-TO-PARENT-DIRECTORY-OF-PROJECT>/node_modules/.bin/npx: line 1: ../npm/bin/npx-cli.js: No such file or directory

项目其他脚本正常运行,怀疑问题出在npx本身,设备是MacBook Pro 16英寸(2021款),Apple M1 Pro芯片,macOS Ventura 13.14.1。

解决方案

  • 直接调用本地Prettier,无需npx
    既然已经将Prettier安装为开发依赖,npm脚本会自动在node_modules/.bin目录下查找可执行文件,所以可以去掉npx,修改format脚本:

    "format": "prettier . --write"
    

    保存后重新执行npm run format即可。

  • 清理缓存并重新安装依赖
    可能是依赖安装过程中出现缓存或文件损坏,执行以下命令:

    npm cache clean --force
    rm -rf node_modules package-lock.json
    npm install
    

    完成后再次尝试运行格式化脚本。

  • 更新npm版本
    Node.js 18.15.0自带的npm版本可能存在npx路径关联问题,执行命令更新npm到最新稳定版:

    npm install -g npm
    

    更新完成后回到项目目录重新运行npm run format。

  • 检查npx可执行文件路径
    手动打开项目下的node_modules/.bin/npx文件,查看其内容中指向的npx-cli.js路径是否正确。如果路径错误,说明安装过程中出现了文件关联问题,执行前面的重新安装依赖步骤即可修复。

内容的提问来源于stack exchange,提问作者Tanishq Vyas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 21:55:55