Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import not working the same with ts-node and tsc + node #786

Closed
bjg2 opened this issue Feb 19, 2019 · 5 comments
Closed

import not working the same with ts-node and tsc + node #786

bjg2 opened this issue Feb 19, 2019 · 5 comments

Comments

@bjg2
Copy link

bjg2 commented Feb 19, 2019

ts-node version v8.0.2
tsc version 3.3.3

Please take a look at ts-node-fail repo, there is a small repro.

If I execute

ts-node test.ts

while there are no transpiled js files (so there's no config.js) config.ts import is messed up and I get output:

hi from test!

If I execute

tsc test.ts
node test.js

import works as expected and I get full output

hi from config!
hi from test!

Am I using something wrong? Seems like ts-node just has the bad execution.

@3mard
Copy link
Contributor

3mard commented Feb 20, 2019

To understand why is this happening you should understand how nodejs resolves files

File Modules#
If the exact filename is not found, then Node.js will attempt to load the required filename with the added extensions: .js, .json, and finally .node.

Now let's examine what happens when you use tsc:
tsc test.ts

ls

.
├── config.js
├── config.json
├── config.ts
├── package-lock.json
├── package.json
├── test.js
└── test.ts

Now according to the node docs when you try to import ./config it will first try to import config.js and if not found it will try to import config.json, it will find config.js and thus 'hi from config!' will be prinited

Now let's examine how ts-node work :
Basically ts-node registesr an extension handler for [ts,tsx] files

so require.extensions becomes [ '.js', '.json', '.node', '.ts', '.tsx' ] and according to the order of the array import './config' will import the json file not the typescript file

@bjg2
Copy link
Author

bjg2 commented Feb 20, 2019

Is it possible to change the order of the files in require.extensions, so that .ts extension is just after .js? I guess the idea of ts-node is to have exactly the same behavior as tsc + node, this bug was subtle and it actually took me a lot of time to find it because I didn't expect that it works differently.

@3mard
Copy link
Contributor

3mard commented Feb 20, 2019

I think it's a bad practice to use such kind of ambiguous setup , and it was a bad design decision of nodejs to allow such ambiguity

Best to my knowledge #649 is trying fix this

@bjg2
Copy link
Author

bjg2 commented Feb 21, 2019

Yeah, I will rename files and thanks for the video. :) Moved the conversation to the issue mentioned (#648).

@blakeembrey
Copy link
Member

Closing with #837.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants