Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS-Node: Could not find sourceFile

My project has been using ts-node to run a mix of JavaScript and TypeScript. Recently it stopped working, without an obvious reason. At the most simple level, here is how it's run and the error it produces:

$ TS_NODE_PROJECT=./tsconfig.json ../../node_modules/.bin/ts-node app.js                                                                                   MSTR-1513
INFO   | Arrow/1.6.0
No deployment manifest found
Uncaught Exception Could not find sourceFile: '/Users/jonah/Projects/myapp/server/src/v1/route/Routes.ts' in [].
Error: Could not find sourceFile: '/Users/jonah/Projects/myapp/server/src/v1/route/Routes.ts' in [].
    at getValidSourceFile (/Users/jonah/Projects/myapp/node_modules/typescript/lib/typescript.js:122211:23)
    at Object.getEmitOutput (/Users/jonah/Projects/myapp/node_modules/typescript/lib/typescript.js:122580:30)
    at getOutput (/Users/jonah/Projects/myapp/node_modules/ts-node/src/index.ts:354:30)
    at Object.compile (/Users/jonah/Projects/myapp/node_modules/ts-node/src/index.ts:395:32)
    at Module.m._compile (/Users/jonah/Projects/myapp/node_modules/ts-node/src/index.ts:473:43)
    at Module._extensions..js (module.js:663:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/jonah/Projects/myapp/node_modules/ts-node/src/index.ts:476:12)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

Excerpt of app.js:

const Routes = require('./v1/route/Routes').default;
server.app.use('/v1', new Routes().router);

I'm very confused by this part of the error: Could not find sourceFile: '/Users/jonah/Projects/myapp/server/src/v1/route/Routes.ts'. I can paste that exact path into the terminal and and see that the file does in fact exist. Here is tsconfig.json:

{
  "compileOnSave": true,
  "compilerOptions": {

    "allowJs": false,
    "removeComments": true,
    "noImplicitAny" : false,
    "module": "commonjs",
    "target": "es2017",
    "sourceMap": true,
    "watch": false,
    "types": ["mocha"],
    "forceConsistentCasingInFileNames": false
  },
  "include": [
    "./v1/**/*.ts",
    "../test/v1/**/*.ts"
  ],
  "exclude": [
    "../../node_modules"
  ]
}

Running currently latest of TypeScript (3.5.2) and ts-node (8.3.0). What type of circumstances might produce this kind of error? I've even tried messing around with the includes to make sure the file being imported is covered. Running the TypeScript compiler on its own works just fine.

../../node_modules/.bin/tsc --project tsconfig.json
like image 868
Jonah Avatar asked Mar 19 '26 16:03

Jonah


1 Answers

I have a feeling this has to do with circular dependencies. Especially with the answer from @Brenne saying:

Changing the order of the imports helped.

These kind of inexplicable errors have always tended to be circular dependencies in my experience.

I'm only familiar with the webpack plugin to detect these. However, you can play around with removing the require and seeing if it builds properly. From there, remove pieces other requires/imports from the './v1/route/Routes' file to see if any of those are causing the circular dep

like image 161
nromito Avatar answered Mar 21 '26 06:03

nromito



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!