简体   繁体   English

TS 节点:找不到源文件

[英]TS-Node: Could not find sourceFile

My project has been using ts-node to run a mix of JavaScript and TypeScript.我的项目一直使用ts-node来运行 JavaScript 和 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 : 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' .我对错误的这一部分感到非常困惑: 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 :这是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).运行当前最新的 TypeScript (3.5.2) 和 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.我什至尝试过使用include来确保导入的文件被覆盖。 Running the TypeScript compiler on its own works just fine.单独运行 TypeScript 编译器可以正常工作。

../../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:特别是@Brenne 的回答说:

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 .我只熟悉webpack 插件来检测这些. However, you can play around with removing the require and seeing if it builds properly.但是,您可以尝试删除 require 并查看它是否正确构建。 From there, remove pieces other requires/imports from the './v1/route/Routes' file to see if any of those are causing the circular dep从那里,从'./v1/route/Routes'文件中删除其他需要/导入的部分,以查看是否有任何导致循环 dep

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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