2
votes

I have a project containing both Javascript and Typescript files. I'm using SonarCloud to analyse this project from an Azure DevOps pipeline.

I set the task Prepare Analysis Configuration in my build pipeline like this:

- task: SonarCloudPrepare@1
  inputs:
    SonarCloud: 'Sonarcloud'
    organization: 'MyOrg'
    scannerMode: 'CLI'
    configMode: 'manual'
    cliProjectKey: 'My key'
    cliProjectName: 'My name'
    cliSources: '.'

When running the pipeline, I have the following error in my pipeline on Sonar Cloud Analyze step

INFO: Found 1 tsconfig.json file(s): [/home/vsts/work/1/s/tsconfig.json]
##[error]ERROR: Cannot find module 'typescript'
##[error]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: 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]ERROR: Missing TypeScript dependency

The analysis works well for javascript files but not for typescript files. I have the typescript package installed as dev dependency in my package.json but it seems it is ignored by SonarCloud.

The documentation and topics I found are related to SonarQube version but I can't figure out how to setup this with SonarCloud.

1
Have you tried adding a npm install task before these two tasks?LoLance

1 Answers

2
votes

By default, node_modules folder in local project folder won't be added into source control if you're using Visual Studio.

And this error would occur since Run Code Analysis task can't find the dependency packages defined in your package.json file. My reproducible step:

enter image description here

Then I add one npm install task before Prepare Analysis task to install missing packages:

enter image description here

NodeJS is my project name. Also in Devops repos, this is the name of the folder where package.json exists.

Then this issue went away in my pipeline:

enter image description here

Hope it also helps for your issue :)

In addition: You can also choose to add local node_modules folder into source control if you want. But this is not recommended in Azure Devops Service.