How to I get the moduleName for each SourceFile?
See original GitHub issueIs there a way to get the moduleName
s for a SourceFile? They are all showing up as uknown
when I do this?
http://www.typescriptlang.org/docs/handbook/module-resolution.html
import * as ts from "typescript";
import * as fs from "fs";
// import {isTypeReference, isClassDeclaration} from "tsutils";
const tsPath = "node_modules/@types/mocha/index.d.ts"
const options: ts.CompilerOptions = { target: ts.ScriptTarget.ES2015 }
const host = ts.createCompilerHost(options, true);
const program = ts.createProgram([tsPath], options, host)
const checker = program.getTypeChecker()
for (const sf of program.getSourceFiles()){
console.log(sf.fileName + ": " + sf.moduleName)
}
C:\Users\camer\ts\TsAst [moduleName +1 ~0 -0 !]> node .\modulename.js
C:/Users/camer/ts/TsAst/node_modules/typescript/lib/lib.es6.d.ts: undefined
node_modules/@types/mocha/index.d.ts: undefined
C:/Users/camer/ts/TsAst/node_modules/@types/node/inspector.d.ts: undefined
C:/Users/camer/ts/TsAst/node_modules/@types/node/index.d.ts: undefined
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to retrieve a module's path? - python - Stack Overflow
First, sys.modules is checked to see if the module was already imported. If so, then sys.modules[name].spec is returned. If that happens ...
Read more >Get-Module (Microsoft.PowerShell.Core)
This command gets all module files, both imported and available, and then groups them by module name. This lets you see the module...
Read more >6. Modules — Python 3.11.1 documentation
The file name is the module name with the suffix .py appended. ... Each module has its own private namespace, which is used...
Read more >Pitch: Introduce #module to get the current module name
SwiftLog currently uses a hack which tries to parse the module name from the full file path which is then uses as the...
Read more >How do I find the location of Python module sources?
For a pure python module you can find the location of the source files by looking at the module.__file__. For example,
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m afraid there is no ready-to-use solution. You need to use
fileName
and try to find an algorithm that works for all cases.I’d suggest:
node_modules
in the namenode_modules
starts with an @, it’s a scoped package -> use the next directory name, too@types
, remove it@types/slack__client
->@slack/client
@angular/common/http
is not exported from@angular/common
main
property to get this rightThanks, I’ll give that a shot.