admin管理员组文章数量:1391951
In the image below on the left in the folders you can see my /src Typescript (blue) piled into my /dist (purple) Javascript using tsc.
You can see in the source file on the left that references a .ts module file that it isn't piled into referencing a .js module file on the right.
Why not? How could the Javascript possibly run if tsc doesn't convert references?
Second question: I then tried manually changing the piled reference from .ts to .js and running node dist/server.js but I get the error cannot find module tools/typescriptImport.js'. Why does node fail to find the module when it is correctly referenced (and you can see on the far right that it is a module)?
In the image below on the left in the folders you can see my /src Typescript (blue) piled into my /dist (purple) Javascript using tsc.
You can see in the source file on the left that references a .ts module file that it isn't piled into referencing a .js module file on the right.
Why not? How could the Javascript possibly run if tsc doesn't convert references?
Second question: I then tried manually changing the piled reference from .ts to .js and running node dist/server.js but I get the error cannot find module tools/typescriptImport.js'. Why does node fail to find the module when it is correctly referenced (and you can see on the far right that it is a module)?
Share Improve this question asked Jan 14, 2016 at 7:35 RichardRichard 16.9k25 gold badges78 silver badges109 bronze badges 5-
1
From the
import
declaration, try removing the.ts
extension and set the path to./tools/typescriptImports
. The preferred way to without the extension so it'sextension agnostic
. – weirdpanda Commented Jan 14, 2016 at 7:38 - As expected, tsc then leaves the line unchanges in the .js output, with no extension. But node still can't find the module. – Richard Commented Jan 14, 2016 at 7:40
-
Most of the times, this problem is solved by adding a
.d.ts
file. Try creating a simpletypescriptImports.d.ts
file and reference it. – weirdpanda Commented Jan 14, 2016 at 7:43 - 1 Did you try the relative path as I told you? – weirdpanda Commented Jan 14, 2016 at 7:45
- Ah, I didn't notice the './', nor thought it makes a difference. It works now, thanks. I'll mark your answer as the answer. – Richard Commented Jan 14, 2016 at 7:49
2 Answers
Reset to default 2For starters, you have to remove the .ts
extension from the import
. TypeScript says that it treats it as a static string and won't change it.
Second, out of experience, I guess using a .d.ts
file may solve your module not found
error. I have solved many times by using this small hack. You can reference it using /// <reference path="tools/typeScriptImports.d.ts" />
. Imagine .d.ts
as the header file for TypeScript.
Lastly, try and make the path relative to the server.js
file. So: ./tools/typeScriptImports
.
You are not supposed to write extension .ts
in import
mands.
Corresponding documentation: http://www.typescriptlang/Handbook#modules-going-external
本文标签: javascriptWhy does Typescript compiler not change ts to jsStack Overflow
版权声明:本文标题:javascript - Why does Typescript compiler not change .ts to .js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744671800a2618870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论