javascripttypescriptts-morph

Why `project.addSourceFilesAtPaths("../../tsconfig.json");` cannot get the correct result source files?


I use ts-morph to find all the source files:

the key code:

let tsmorph = require('ts-morph') 

const project = new tsmorph.Project();

project.addSourceFilesAtPaths("../../tsconfig.json");

const sourceFiles = project.getSourceFiles();

console.log(sourceFiles)  // there get empty array:[]

when I run npx ts-node test/unit/ts-morph-test.ts: but there get empty array:[]

why it cannot get the correct results?

my code uploaded to codesandbox: https://codesandbox.io/s/2ntc8q

the test project code structure: you see it obviously has ts files.

enter image description here


Solution

  • You could try below code:

    let tsmorph = require('ts-morph')
    
    const project = new tsmorph.Project();
    project.addSourceFilesFromTsConfig('/Users/johnson/github_repo/for-compare-test/Gono/packages/core/tsconfig.json')