使用sonar扫描ts项目时报如下错误,
Failed to find 'typescript' module. Please check, NODE_PATH contains location of global 'typescript' or install locally in your project
ERROR: Error: Cannot find module 'typescript'
ERROR: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
ERROR: at Function.Module._load (internal/modules/cjs/loader.js:562:25)
ERROR: at Module.require (internal/modules/cjs/loader.js:692:17)
ERROR: at require (internal/modules/cjs/helpers.js:25:18)
ERROR: at Object.<anonymous> (/home/jenkins/data/workspace/coding/test/.scannerwork/sonarts-bundle/node_modules/tslint/lib/language/walker/blockScopeAwareRuleWalker.js:20:10)
ERROR: at Module._compile (internal/modules/cjs/loader.js:778:30)
ERROR: at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
ERROR: at Module.load (internal/modules/cjs/loader.js:653:32)
ERROR: at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
ERROR: at Function.Module._load (internal/modules/cjs/loader.js:585:3)
ERROR: Failed to find 'typescript' module. Please check, NODE_PATH contains location of global 'typescript' or install locally in your project
ERROR: External process `node --max-old-space-size=2048 /home/jenkins/data/workspace/coding/test/.scannerwork/sonarts-bundle/node_modules/tslint-sonarts/bin/tsrunner` returned an empty output. Run with -X for more information
解决方法:安装typescript,npm install -g typescript
安装完成后执行tsc -v查看,如果tsc命令报错Unexpected token ?,可能是typescript版本不兼容
卸载typescript:npm uninstall -g typescript
指定版本安装,我本地node版本v10.24.1,npm版本6.14.12,安装typescript版本3.5.3
执行:npm install -g typescript@3.5.3
如果安装完成之后还报上面错误,可能是新拉的代码仓库没有安装依赖,执行下npm i就好了