SonarQube Typescript dependency error in Jenkins

Viewed 2383

Our UI is written on JS using Typescript and builds by 'npm run build ....' I'm trying to add SonarQube analysis, but it fails on Typescript check with error:

ERROR: Cannot find module 'typescript'
ERROR: TypeScript dependency was not found and it is required for analysis.
ERROR: Install TypeScript in the project directory or use NODE_PATH env. variable to set TypeScript location, if it's located outside of project directory.
ERROR: Missing TypeScript dependency

Here is my Jenkinsfile:

nodejs(nodeJSInstallationName: 'NodeJS') {
                env.NODEJS_HOME = "${tool 'NodeJS'}"
                def scannerHome = tool "sonar"
                withSonarQubeEnv('sonar'){
                    sh """
                        ${scannerHome}/bin/sonar-scanner \
                            -Dsonar.projectKey=project-name-${BUILD_ENV} \
                            -Dsonar.sources=src
                    """
                }
            }

But every time I'm running this stage, I'm receiving output with error mentioned above. Please see full output:

INFO: Scanner configuration file: /home/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/sonar/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.3.0.2102
INFO: Java 1.8.0_162 Oracle Corporation (64-bit)
INFO: Linux 5.3.0-1017-aws amd64
INFO: User cache: /home/jenkins/.sonar/cache
INFO: Scanner configuration file: /home/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/sonar/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: Analyzing on SonarQube server 8.1.0
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
WARN: SonarScanner will require Java 11 to run starting in SonarQube 8.x
INFO: Load global settings
INFO: Load global settings (done) | time=546ms
INFO: Server id: *********
INFO: User cache: /home/jenkins/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=122ms
INFO: Load/download plugins (done) | time=160ms
WARN: Plugin SonarQube CNES Report [cnesreport] uses a child first classloader which is deprecated
INFO: Process project properties
INFO: Process project properties (done) | time=12ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=12ms
INFO: Project key: project-name
INFO: Base dir: /home/jenkins/workspace/project-name
INFO: Working dir: /home/jenkins/workspace/project-name/.scannerwork
INFO: Load project settings for component key: 'project-name'
INFO: Load project settings for component key: 'project-name' (done) | time=104ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=143ms
INFO: Detected Jenkins
INFO: Load active rules
INFO: Load active rules (done) | time=2245ms
INFO: Indexing files...
INFO: Project configuration:
INFO: Load project repositories
INFO: Load project repositories (done) | time=104ms
INFO: 7 files indexed
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for css: Sonar way
INFO: Quality profile for js: Sonar way
INFO: Quality profile for ts: Sonar way
INFO: ------------- Run sensors on module project-name
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=108ms
INFO: Sensor SonarCSS Metrics [cssfamily]
INFO: Sensor SonarCSS Metrics [cssfamily] (done) | time=32ms
INFO: Sensor SonarCSS Rules [cssfamily]
INFO: 1 source files to be analyzed
INFO: 1/1 source files have been analyzed
INFO: Sensor SonarCSS Rules [cssfamily] (done) | time=1058ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=3ms
INFO: Sensor JavaScript analysis [javascript]
INFO: TypeScript dependency was not found inside project directory, Node.js will search TypeScript using module resolution algorithm; analysis will fail without TypeScript.
INFO: 5 source files to be analyzed
INFO: 5/5 source files have been analyzed
INFO: Sensor SonarJS [javascript]
INFO: 5 source files to be analyzed
INFO: Sensor SonarJS [javascript] (done) | time=304ms
INFO: 5/5 source files have been analyzed
INFO: Sensor JavaScript analysis [javascript] (done) | time=2296ms
INFO: Sensor TypeScript analysis [javascript]
INFO: Found 1 tsconfig.json file(s): [/home/jenkins/workspace/project-name/tsconfig.json]
ERROR: Cannot find module 'typescript'
ERROR: TypeScript dependency was not found and it is required for analysis.
ERROR: Install TypeScript in the project directory or use NODE_PATH env. variable to set TypeScript location, if it's located outside of project directory.
ERROR: Missing TypeScript dependency
org.sonar.plugins.javascript.eslint.MissingTypeScriptException: Missing TypeScript dependency
    at org.sonar.plugins.javascript.eslint.EslintBridgeServerImpl.loadTsConfig(EslintBridgeServerImpl.java:270)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at org.sonar.plugins.javascript.eslint.TypeScriptSensor.loadTsConfigs(TypeScriptSensor.java:173)
    at org.sonar.plugins.javascript.eslint.TypeScriptSensor.analyzeFiles(TypeScriptSensor.java:109)
    at org.sonar.plugins.javascript.eslint.AbstractEslintSensor.execute(AbstractEslintSensor.java:107)
    at org.sonar.plugins.javascript.eslint.TypeScriptSensor.execute(TypeScriptSensor.java:53)
    at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:59)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:77)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:59)
    at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:386)
    at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:382)
    at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:351)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:141)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72)
    at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66)
    at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
    at com.sun.proxy.$Proxy0.execute(Unknown Source)
    at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
    at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:112)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
    at org.sonarsource.scanner.cli.Main.main(Main.java:61)

INFO: Sensor TypeScript analysis [javascript] (done) | time=117ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=1ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=3ms
INFO: Sensor SonarTS [typescript]
INFO: Since SonarTS v2.0, TypeScript analysis is performed by SonarJS analyzer v6.0 or later. No TypeScript analysis is performed by SonarTS.
INFO: Sensor SonarTS [typescript] (done) | time=0ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=8ms
INFO: CPD Executor 3 files had no CPD blocks
INFO: CPD Executor Calculating CPD for 2 files
INFO: CPD Executor CPD calculation finished (done) | time=10ms
INFO: Analysis report generated in 119ms, dir size=91 KB
INFO: Analysis report compressed in 34ms, zip size=22 KB
INFO: Analysis report uploaded in 222ms
INFO: ANALYSIS SUCCESSFUL, you can browse https://******
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at https://********
INFO: Analysis total time: 10.225 s

I tried to add typescript as Global npm packages to install in Global tool configuration, tried to install it via Jenkinsfile (sh "npm install -g typescript"), tried to manually add its installation path to my NODE_PATH and PATH environment variables, but without any success.

Could anyone help me with it, as I don't know what else I can do?

P.S. Installing NodeJS and Typescript on slave instance will not work, as Jenkins has a lot of slaves and many of them automatically start, stop, spin up etc.

1 Answers

Had the same problem.

Was solved by moving the SonarQube analysis step in Jenkins to be after the build step, which comprises of npm install and npm run build.

Related