简体   繁体   中英

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 include s 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

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

我不熟悉 tsc,因为我总是在 webpack 中使用 babel,但我认为如果你想将 js 编译为 ts,你需要启用allowJs选项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM