Ts-node: Add documentation on Gulp usage

Created on 3 Nov 2015  Β·  33Comments  Β·  Source: TypeStrong/ts-node

From https://github.com/TypeStrong/ts-node/issues/24. Should also clarify that certain issues are coming from node and that this _always_ loads tsconfig.json by default - how to use two tsconfig.json files.

documentation

Most helpful comment

All 33 comments

:+1: Any notes on Gulp would make my life much prettier right now.

@ianbytchek What do you need specifically? Have you tried creating gulpfile.ts and going from there.

I've been googling earlier on today on how to write gulp tasks straight in TS and not worry about the manual tsc step. Didn't come up with much, but found a few references that this is the place to start looking. I saw require-typescript project and your suggestion to deprecate it in favour of this one, then I saw #44, all made me a little confused. So, didn't give it a proper try yet, then this issue caught my eye.

Is it actually possible to write gulpfile in TS and execute it without compiling first? I'm not seeing how gulpfile.ts can help unless compiled first or ran not through gulp?

PS: thanks for all the cool TypeScript work. Started playing with it a few days ago, having a good time.

@ianbytchek Yep, definitely works. Give it a go. Gulp uses https://www.npmjs.com/package/liftoff which uses https://www.npmjs.com/package/rechoir, so .ts will automatically registry ts-node.

Edit: Sorry it's non-obvious, definitely requires a see it to believe approach :pray:

@blakeembrey I'm missing something. So I installed ts-node globally, but get a bunch of errors when run gulp.

[16:07:19] Failed to load external module typescript-node/register
[16:07:19] Failed to load external module typescript-register
[16:07:19] Failed to load external module typescript-require

I tried installing typescript-register which seems to work, but I though that all three packages are deprecated in favour of this one? Did I miss a step?

@ianbytchek Try installing ts-node locally?

I actually tried that too, didn't help. I figured that doing gulp --require ts-node/register makes the trick, but can this be automated?

@ianbytchek Odd, I get the message too - but the file actually does load, I'll look into this.

image

@ianbytchek Just realised what it was. ts-node requires typescript explicitly and without it, ts-node crashes. Make sure you install typescript, I'll improve the user-experience here since Gulp is suppressing the actual error.

@blakeembrey I have it.

npm ls -g --depth 0
/usr/local/lib
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
└── [email protected]

Make sure you install both locally, not globally. I'll have a better error message in stderr ASAP to mitigate this in the future.

Thanks, I'll give it a go now!

Wow it really just works. Sorry, it still doesn't, tried it on the wrong project. Have those locally installed, any other reason why it might be failing? :confused:

[email protected]
[email protected]
[email protected]

@ianbytchek Can you create a copy of the project you're using that I can replicate? That would be perfect :+1:

@blakeembrey try ianbytchek/guild. gulp, typescript and ts-node are peer deps.

# Install dependencies.
cd dependency
tsd install
npm install; npm install gulp; npm install ts-node; npm install typescript

// Run gulp.
cd ../build
gulp # Fails…
gulp --require ts-node/register # Succeeds…

I've been using TypeScript for my gulpfile.ts and this is how I got it to work. I create a gulpfile.js file containing only these two lines:

require('ts-node/register');
require('./gulpfile.ts');

Then I write all my Gulp tasks in gulpfile.ts. Works like a charm. Since ts-node doesn't compile the js file in the same directory, it doesn't clobber the gulpfile.js file.

You shouldn't need to require ts-node/register. See this comment, which leads to these lines.

Disclaimer – I haven't tried a gulpfile.ts like this yet, but it should, theoretically, work.

@joeskeen thanks, this is a better solution than doing --require ts-node/register every time. I'm curious though why it works in some cases like @blakeembrey says, but not for me.

@jedmao yeah, I have seen those before, but for whatever reason, I wasn't able to get it to work without adding a gulpfile.js and requiring ts-node/register. But that was a while ago, things may have changed since then; at any rate, my solution is still working for our project nicely. :)

You boys got me all curious now, so I created gulpfile-ts, which demonstrates that the gulpfile.ts is supported out-of-the-box. It does the require('ts-node/register') for you so you don't have to!

@jedmao thanks for looking into this. This is still failing on me:

ianbytchek@ibmbp:gulpfile-ts-master$ npm install
ianbytchek@ibmbp:gulpfile-ts-master$ tsd install
ianbytchek@ibmbp:gulpfile-ts-master$ gulp
[13:54:11] Failed to load external module typescript-node/register
[13:54:11] Failed to load external module typescript-register
[13:54:11] Failed to load external module typescript-require
/Users/ianbytchek/Downloads/gulpfile-ts-master/gulpfile.ts:1
(function (exports, require, module, __filename, __dirname) { import * as gulp from 'gulp';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Liftoff.handleArguments (/usr/local/lib/node_modules/gulp/bin/gulp.js:116:3)
    at Liftoff.<anonymous> (/usr/local/lib/node_modules/gulp/node_modules/liftoff/index.js:192:16)
    at module.exports (/usr/local/lib/node_modules/gulp/node_modules/liftoff/node_modules/flagged-respawn/index.js:17:3)

There is something wrong… Can you show the output of your npm ls -g --depth 0 and node -v?

/.../GitHub/gulpfile-ts (master u=)$npm ls -g --depth 0
C:\Users\jedma\AppData\Roaming\npm
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
└── [email protected]



/.../GitHub/gulpfile-ts (master u=)$node -v
v5.5.0


/.../GitHub/gulpfile-ts (master u=)$npm -v
3.3.12

FWIW, I'm on Windows 10.

BTW – the task was gulp hello, but I just pushed a commit so the task would be gulp, the default task.

Let me know any other way I can help.

@ianbytchek I finally got around to trying your demo, it appears to work fine for me.

image

@blakeembrey I've figured something odd is going on. I'll try to reinstall some global packages see if that helps.

For what it's worth, I went back and tried gulpfile.ts without gulpfile.js and it worked fine.

_Edit: I thought at first it was slower, but I tried it both ways and it was about the same ~2 second delay before my gulpfile starts up_

@joeskeen Using ts-node is definitely slower, see https://github.com/TypeStrong/ts-node/issues/44 for more info (not sure when I'll ever have time to actually do it).

After updating gulp to 3.9.1 it works. IT WORKS!!!

kip----yesss--napoleon-dynamite-387390_200_270

I've made a PR to log the requireFail reason going forward. That would help here. I wanted to do logging as part of failing to register, but figured I'm already throwing errors so it'd be better to just update tooling to comply with the standard JavaScript contract here (log out the error).

I had same issue as @ianbytchek and resolved only after I updated my global gulp to 3.9.1 (previously had 3.9.0. If 3.9.1 is the minimal requirement then this needs to be specified somewhere

Hi guys, i'm still having this issue when using Gulp 4, any thoughts?

You can always create an issue and describe the problem you're facing.

Updating gulp to 3.9.1 fixed the same I issue I had.

Was this page helpful?
0 / 5 - 0 ratings