运行tsc-watch时出现“找不到模块'typescript/bin/tsc'”错误

3
当我尝试运行tsc-watch时,出现了"找不到模块'typescript/bin/tsc'"的错误。 yarn tsc-watch --noClear -p tsconfig.json
yarn run v1.22.19
$ /Users/jason/Work/VDQ/VDQApp/node_modules/.bin/tsc-watch --noClear -p tsconfig.json
Cannot find module 'typescript/bin/tsc'
Require stack:
- /Users/jason/Work/VDQ/VDQApp/node_modules/tsc-watch/lib/tsc-watch.js
- /Users/jason/Work/VDQ/VDQApp/node_modules/tsc-watch/index.js

这个错误很容易在没有JSBundling/package.json/Yarn设置的情况下重现。

我的package.json文件如下:

{
  "name": "app",
  "private": "true",
  "dependencies": {
    "@hotwired/stimulus": "^3.1.0",
    "@hotwired/turbo-rails": "^7.2.0",
    "@popperjs/core": "^2.11.6",
    "bootstrap": "^5.2.2",
    "bootstrap-icons": "^1.9.1",
    "esbuild": "^0.15.10",
    "sass": "^1.55.0",
    "tsc": "^2.0.4"
  },
  "scripts": {
    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=assets",
    "build:css": "sass ./app/assets/stylesheets/application.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules",
    "failure:js": "rm ./app/assets/builds/application.js && rm ./app/assets/builds/application.js.map",
    "compile:typescript": "tsc-watch --noClear -p tsconfig.json --onSuccess \"yarn build\" --onFailure \"yarn failure:js\""
  },
  "devDependencies": {
    "tsc-watch": "^5.0.3"
  }
}

我的tsconfig.json文件如下:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "noImplicitAny": true
  }
}
1个回答

0
这种情况发生在你未能全局添加typescript包时。
令人困惑的是,typescript命令是通过"tsc"来调用的,但包名却是"typscript"。
修复方法:
使用"yarn global add typescript"命令
或者
使用"npx install -g typescript"命令

应该执行命令 yarn global add typescript - Eva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接