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

Executables without extension #116

Closed
ValeriiVasin opened this issue May 17, 2016 · 11 comments
Closed

Executables without extension #116

ValeriiVasin opened this issue May 17, 2016 · 11 comments
Labels
enhancement you can do this Good candidate for a pull request.

Comments

@ValeriiVasin
Copy link

ValeriiVasin commented May 17, 2016

I found that ts-node works only for files with .ts(x) extension. And it is not possible to create binary/executable file, e.g. ./bin/my-ts-executable (without extension) but with shebang #!/usr/bin/env ts-node.

It works fine with babel-node shebangs.

Is there some limitations by TypeScript compiler or smth?

@blakeembrey
Copy link
Member

No, it's a limitation of Node. It requires an extension to be able to run the correct extension loader. The only workaround for this may be to override .js - unless someone knows a better approach.

@blakeembrey blakeembrey added enhancement you can do this Good candidate for a pull request. labels Jun 6, 2016
@huan
Copy link

huan commented Jul 25, 2016

I ran into this issue today.

shebang works fine as long as the filename is in '.ts' extention, in this example, "t.ts".

#!/usr/bin/env ts-node
console.log('ohai from TypeScript')
import * as http from 'http'

But if rename "t.ts" to "t", and run "./t”, will get this error:

./t:3
import * as http from 'http'
^^^^^^
SyntaxError: Unexpected token import

It looks when run without extention, the script is treated as javascript instead of typescript, no mater what shebang we use.

can we find some way to "fake" the file extention name to node? because I would like to use typescript without extention, like this: ./t :)

@davenonymous
Copy link

Running into the same issue: I would like to drop the .ts extension from some scripts, but can't. Any usable workaround/fix known?

@blakeembrey
Copy link
Member

@thraaawn Same as my previous comment, no. It's a limitation of node that it treats unknown files as .js. The only workaround would be to enable allowJs with TypeScript (haven't tested it, but theoretically).

@ValeriiVasin
Copy link
Author

Any idea how babel-node bypasses this limitation?

@blakeembrey
Copy link
Member

blakeembrey commented Oct 28, 2016

Because it registers the .js extension which would fallback to the behaviour I mentioned above?

@ValeriiVasin
Copy link
Author

ValeriiVasin commented Oct 28, 2016

would not it be a good fit for the ts-node to do same to allow people write custom executables without an extension? Even though such a "hack" if you will.

Not sure is it hack or not b/c i have not really got what you meant :)

@blakeembrey
Copy link
Member

No. That means every file runs through TypeScript, even JavaScript files. If you want that, use allowJs as I suggested above.

@ValeriiVasin
Copy link
Author

Examples how to do such executables even through some tsconfig options would be much appreciated.

I tried this:

  • created bin/test file with proper execution rights
#!/usr/bin/env ts-node

console.log(1 + 2);
  • added allowJs option to tsconfig.json:
"allowJs": true
  • added npm script (to use local ts-node from node_modules/.bin - could be used through $PATH modification as well)
"test-it": "./bin/test"
  • run
npm run test-it

it took quite a while before i got an error:

/Users/vvasin/Projects/ts-node-test/node_modules/typescript/lib/typescript.js:55445
                throw new Error("Could not find file: '" + fileName + "'.");
                      ^
Error: Could not find file: '/Users/vvasin/Projects/ts-node-test/bin/test'.
    at getValidSourceFile (/Users/vvasin/Projects/ts-node-test/node_modules/typescript/lib/typescript.js:55445:23)
    at Object.getEmitOutput (/Users/vvasin/Projects/ts-node-test/node_modules/typescript/lib/typescript.js:59348:30)
    at getOutput (/Users/vvasin/Projects/ts-node-test/node_modules/ts-node/src/index.ts:302:32)
    at /Users/vvasin/Projects/ts-node-test/node_modules/ts-node/src/index.ts:343:18
    at Object.compile (/Users/vvasin/Projects/ts-node-test/node_modules/ts-node/src/index.ts:502:17)
    at Module.m._compile (/Users/vvasin/Projects/ts-node-test/node_modules/ts-node/src/index.ts:406:44)
    at Module._extensions..js (module.js:416:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/vvasin/Projects/ts-node-test/node_modules/ts-node/src/index.ts:409:12)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)

what am i doing wrong here?

@blakeembrey
Copy link
Member

I'll have to play around with it later, but it looks like it is going through TypeScript as a result. As for why the file isn't found, perhaps there's an issue reading it?

@blakeembrey
Copy link
Member

blakeembrey commented Mar 21, 2017

I'm going to close this. In your stack trace you can see that all files are being compiled with TypeScript (see ts-node in your stack trace - it did go through the ts-node loader) if you configure it using allowJs. Again, this is just how node works - it'll use the extension loader defined or fallback on the .js loader. We can't make it use ts-node as a result. One hack, if there's enough request, would be to expose a ts-js-node or similar executable that's made with allowJs: true by default, but I'm not sure many people want that - for one, it'd start intercepting all .js files, not just the entry file without an extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement you can do this Good candidate for a pull request.
Projects
None yet
Development

No branches or pull requests

4 participants