question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to I get the moduleName for each SourceFile?

See original GitHub issue

Is there a way to get the moduleNames 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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ajafffcommented, Nov 19, 2017

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:

  • find the last node_modules in the name
  • if the directory after node_modules starts with an @, it’s a scoped package -> use the next directory name, too
  • if the scope is @types, remove it
  • you probably need some special handling for submodule imports
    • for example @angular/common/http is not exported from @angular/common
    • you probably need to read the package.json’s main property to get this right
0reactions
ctaggartcommented, Nov 19, 2017

Thanks, I’ll give that a shot.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found