【问题标题】:SonarQube: Typescript module not foundSonarQube:找不到打字稿模块
【发布时间】:2018-06-27 10:03:27
【问题描述】:

我正在使用SonarQube 分析Typescript 项目。

分析在Gitlab 管道阶段运行,使用带有sonar-scanner 的docker 映像。

这是一些输出:

INFO: Download sonar-flex-plugin-2.3.jar
INFO: Download sonar-gitlab-plugin-2.1.0.jar
INFO: Download sonar-auth-gitlab-plugin-1.2.2.jar
INFO: Download sonar-csharp-plugin-6.6.0.3969.jar
INFO: Download sonar-javascript-plugin-3.3.0.5702.jar
INFO: Download sonar-java-plugin-4.15.0.12310.jar
INFO: Download sonar-php-plugin-2.12.0.2871.jar
INFO: Download sonar-python-plugin-1.8.0.1496.jar
INFO: Download sonar-scm-git-plugin-1.3.0.869.jar
INFO: Download sonar-scm-svn-plugin-1.6.0.860.jar
INFO: Download sonar-typescript-plugin-1.2.0.1484.jar
INFO: Download sonar-xml-plugin-1.4.3.1027.jar
INFO: SonarQube server 6.7.0
INFO: Default locale: "en", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Process project properties
INFO: Load project repositories
WARN: Project doesn't exist on the server. All issues will be marked as 'new'.
INFO: Load project repositories (done) | time=10ms
INFO: GlobalWorkingDir null
INFO: Scanning only changed files
INFO: Execute project builders
INFO: Execute project builders (done) | time=498ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=26ms
INFO: Load active rules
INFO: Load active rules (done) | time=310ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=16ms
INFO: Project key: ELS_UI
INFO: -------------  Scan ELS_UI
INFO: Load server rules
INFO: Load server rules (done) | time=37ms
INFO: Base dir: /builds/pkaramol/projectname
INFO: Working dir: /builds/pkara/projectname/.scannerwork
INFO: Source paths: app/components
INFO: Source encoding: UTF-8, default locale: en
WARN: Property 'sonar.php.file.suffixes' is not declared as multi-values/property set but was read using 'getStringArray' method. The SonarQube plugin declaring this property should be updated.
INFO: Index files
INFO: 268 files indexed
INFO: Quality profile for ts: Sonar way
INFO: Sensor SonarJavaXmlFileSensor [java]
INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=2ms
INFO: Sensor Analyzer for "php.ini" files [php]
INFO: Sensor Analyzer for "php.ini" files [php] (done) | time=4ms
INFO: Sensor TypeScript Sensor [typescript]
ERROR: module.js:471
ERROR:     throw err;
ERROR:     ^
ERROR: 
ERROR: Error: Cannot find module 'typescript'
ERROR:     at Function.Module._resolveFilename (module.js:469:15)
ERROR:     at Function.Module._load (module.js:417:25)
ERROR:     at Module.require (module.js:497:17)
ERROR:     at require (internal/module.js:20:19)
ERROR:     at Object.<anonymous> (/builds/pkara/projectname/.scannerwork/sonarts-bundle/node_modules/tslint/lib/linter.js:20:10)
ERROR:     at Module._compile (module.js:570:32)
ERROR:     at Object.Module._extensions..js (module.js:579:10)
ERROR:     at Module.load (module.js:487:32)
ERROR:     at tryModuleLoad (module.js:446:12)
ERROR:     at Function.Module._load (module.js:438: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 /builds/pkara/projectname/.scannerwork/sonarts-bundle/node_modules/tslint-sonarts/bin/tsrunner` returned an empty output. Run with -X for more information

鉴于它下载相关插件:

信息:下载 sonar-typescript-plugin-1.2.0.1484.jar

为什么后来找不到了?

错误:错误:找不到模块“打字稿”

【问题讨论】:

    标签: typescript docker sonarqube gitlab-ci sonarqube-scan


    【解决方案1】:

    它是否正确找到了节点?我们发现 Node 必须在路径上,否则 SQ 分析在任何带有 Typescript 代码的模块上都会失败。实际上,堆栈跟踪中的错误(靠近底部)建议您检查节点配置。

    ERROR: Failed to find 'typescript' module. Please check, NODE_PATH contains 
    location of global 'typescript' or install locally in your project
    

    【讨论】:

    • 嗨,请在此处查看广泛的分析:github.com/SonarSource/SonarTS/issues/453
    • 可以通过node --version命令查看是否安装了Node。对我来说,这是检查我的 appVeyor.yml 文件(install: 部分中的- cmd: node --version )很有用。
    【解决方案2】:

    在非 docker 环境下,你必须先设置 NODE_PATH 指向全局 NPM 目录。在 Windows 10 命令行中,批处理脚本如下所示:

    SET NODE_PATH=%AppData%\npm\node_modules
    C:\SonarQube\sonar-scanner-3.1.0.1141-windows\bin\sonar-scanner.bat -Dsonar.projectKey=PortalUI -Dsonar.sources=.\src -Dsonar.host.url=http://localhost:9000 -Dsonar.login=longtoken_xkxxxxxxx
    

    【讨论】:

    • 在我的情况下,我正在运行 AppVeyor CI 构建并收到 Failed to find 'typescript' module 错误。对我来说,解决方案是将- cmd: SET NODE_PATH=%AppData%\npm\node_modules- cmd: npm install -g typescript 添加到我的appveyor.yml 文件的install 部分。完整的 appveyor.yml 文件可以在here找到。
    【解决方案3】:

    用于分析 TypeScript 文件的插件需要在您的项目中全局或本地提供 typescript 节点模块。

    鉴于您的机器上已经安装了 node.js,您有两种选择:

    • 推荐)在开始对您的项目进行分析之前运行npm install
    • 通过运行全局安装 TypeScript:npm install -g typescript

    见:https://community.sonarsource.com/t/error-cannot-find-module-typescript/4396

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 2013-12-30
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2017-08-26
      相关资源
      最近更新 更多