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

npx查找模块路径错误:执行tsc命令提示找不到模块

问题描述

在VS Code的bash终端执行以下命令:

$ npx tsc with-typescript.ts

出现如下错误:

The system cannot find the path specified.
node:internal/modules/cjs/loader:1189
  throw err;
  ^

Error: Cannot find module 'C:\Users\joshu\Programming Projects\Udemy Courses\typescript\bin\tsc'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1186:15)        
    at Module._load (node:internal/modules/cjs/loader:1012:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:158:12)
    at node:internal/main/run_main_module:30:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v22.2.0

已完成操作:

  • 在项目目录~/Programming Projects/Udemy Courses/Python & React/Web Development Bootcamp/React js Practice/typescript-practice执行npm init -y和npm install typescript,成功生成package.json和node_modules文件夹
  • 删除package.json、package-lock.json和node_modules后重新安装
  • 卸载并重新安装Node.js
  • 确认npm prefix设置为项目根目录

问题核心:npx错误地在Udemy Courses目录下查找typescript模块,该目录下不存在此文件夹。

解决方案

按优先级尝试以下方法:

  • 直接调用本地tsc路径
    绕开npx的路径解析,直接执行项目本地安装的tsc:

    ./node_modules/.bin/tsc with-typescript.ts
    
  • 添加package.json脚本命令
    在项目的package.json的scripts字段中添加编译命令:

    {
      "scripts": {
        "build": "tsc with-typescript.ts"
      }
    }
    

    然后执行:

    npm run build
    

    npm会自动在本地node_modules/.bin目录中查找tsc,不会出现路径解析问题。

  • 清理npx缓存后重试
    npx可能缓存了旧路径信息,清理后重新执行:

    npx clear-npx-cache
    npx tsc with-typescript.ts
    
  • 确认终端工作目录
    先检查终端当前所在目录:

    pwd
    

    如果不在项目根目录(typescript-practice),切换目录(注意路径空格用引号包裹):

    cd "~/Programming Projects/Udemy Courses/Python & React/Web Development Bootcamp/React js Practice/typescript-practice"
    
  • 全局安装typescript临时替代
    若上述方法无效,可全局安装typescript让npx调用全局版本:

    npm install -g typescript
    npx tsc with-typescript.ts
    

内容的提问来源于stack exchange,提问作者Lehr Bear

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 12:14:51