Skip to content

Commit 01fbfa2

Browse files
committed
v0.1.8: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/vsts/work/_tasks/component-detection-github-submission-task_5065f07b-c417-4050-bf2b-f8520ce76aba/0.1.7/sourcemap-register.cjs' imported from /home/vsts/work/_tasks/component-detection-github-submission-task_5065f07b-c417-4050-bf2b-f8520ce76aba/0.1.7/index.mjs
Updated build script to copy both index.js → index.mjs and sourcemap-register.cjs Both files are now included in the Azure DevOps task directory The ES module can now properly resolve its dependencies
1 parent 3c4648b commit 01fbfa2

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

component-detection-github-submission-task/sourcemap-register.cjs

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

component-detection-github-submission-task/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 0,
1111
"Minor": 1,
12-
"Patch": 7
12+
"Patch": 8
1313
},
1414
"instanceNameFormat": "Component Detection $(filePath)",
1515
"groups": [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"prepare": "ncc build index.ts -o dist --source-map --license licenses.txt",
1010
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
1111
"all": "npm run lint && npm run prepare && npm run test",
12-
"ado:build": "ncc build ado-index.ts -o ado-dist --source-map --license licenses.txt --target es2020 && node -e \"require('fs').copyFileSync('ado-dist/index.js', 'component-detection-github-submission-task/index.mjs')\"",
12+
"ado:build": "ncc build ado-index.ts -o ado-dist --source-map --license licenses.txt --target es2020 && node -e \"require('fs').copyFileSync('ado-dist/index.js', 'component-detection-github-submission-task/index.mjs'); require('fs').copyFileSync('ado-dist/sourcemap-register.cjs', 'component-detection-github-submission-task/sourcemap-register.cjs')\"",
1313
"ado:package": "tfx extension create --manifest-globs vss-extension.json",
1414
"ado:all": "npm run ado:build && npm run ado:package"
1515
},

scripts/convert-to-cjs.cjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/**
22
* ES Module to CommonJS Converter for Azure DevOps Extension
3-
*
3+
*
44
* This script is necessary because:
55
* 1. ncc (Node.js Compiler Collection) always outputs ES modules with import/export syntax
66
* 2. Azure DevOps task runtime expects CommonJS modules with require() syntax
77
* 3. Even with "type": "commonjs" in package.json, the bundled code contains ES module syntax
88
* 4. Azure DevOps cannot execute ES modules, resulting in "Cannot use import statement outside a module" errors
9-
*
9+
*
1010
* The script converts:
1111
* - import statements → require() calls
1212
* - import.meta.url → __filename (CommonJS equivalent)
1313
* - ES module destructuring → CommonJS destructuring
14-
*
15-
* This allows the same TypeScript codebase to work in both GitHub Actions (ES modules)
14+
*
15+
* This allows the same TypeScript codebase to work in both GitHub Actions (ES modules)
1616
* and Azure DevOps (CommonJS) environments.
1717
*/
1818

scripts/convert-to-cjs.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const inputFile = path.join(__dirname, '..', 'ado-dist', 'index.js');
5+
const outputFile = path.join(__dirname, '..', 'component-detection-github-submission-task', 'index.js');
6+
7+
// Read the ES module output from ncc
8+
let content = fs.readFileSync(inputFile, 'utf8');
9+
10+
// Convert ES module imports/exports to CommonJS
11+
// Replace import statements at the beginning
12+
content = content.replace(
13+
/^import\s+(['"].*?['"])\s*;/gm,
14+
'require($1);'
15+
);
16+
17+
// Replace import with destructuring
18+
content = content.replace(
19+
/^import\s*{\s*([^}]+)\s*}\s*from\s*(['"].*?['"])\s*;/gm,
20+
'const { $1 } = require($2);'
21+
);
22+
23+
// Replace default imports
24+
content = content.replace(
25+
/^import\s+(\w+)\s+from\s*(['"].*?['"])\s*;/gm,
26+
'const $1 = require($2);'
27+
);
28+
29+
// Replace createRequire import (this is specific to the ncc output)
30+
content = content.replace(
31+
/import\s*{\s*createRequire\s+as\s+__WEBPACK_EXTERNAL_createRequire\s*}\s*from\s*['"]module['"];/g,
32+
'const { createRequire: __WEBPACK_EXTERNAL_createRequire } = require("module");'
33+
);
34+
35+
// Replace any remaining import statements
36+
content = content.replace(
37+
/^import\s+(.*?)\s*;/gm,
38+
'require($1);'
39+
);
40+
41+
// Write the converted content
42+
fs.writeFileSync(outputFile, content, 'utf8');
43+
44+
console.log(`Converted ES module to CommonJS: ${outputFile}`);

vss-extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifestVersion": 1,
33
"id": "component-detection-github-submission-task",
44
"name": "Component Detection for GitHub Dependency Submission",
5-
"version": "0.1.7",
5+
"version": "0.1.8",
66
"publisher": "AdvancedSecurityOSS",
77
"description": "Upload information about your dependencies to the GitHub dependency graph using Microsoft's component detection library",
88
"public": false,

0 commit comments

Comments
 (0)