Christian Dreher Christian Dreher - 3 months ago 22
TypeScript Question

Proper way of setting up a gulpfile.ts

I've set up a

gulpfile.ts
for my project following this example right here on GitHub. (I will not supply my
gulpfile.ts
, since it's basically the same, just a little bit more complex)

Whenever I ran a gulp task, I got these 4 errors:

[11:53:23] Failed to load external module ts-node/register
[11:53:23] Failed to load external module typescript-node/register
[11:53:23] Failed to load external module typescript-register
[11:53:23] Failed to load external module typescript-require


I then decided to add
ts-node
as a (local) dev-dependency. (Global installation did not change anything)

After
npm install
and running a gulp task, I then got some typescript errors like this

gulpfile.ts (19,23): Cannot find name 'require'. (2304)


I did not find anything on google on how to solve this issue, but after having a look on some example
gulpfile.ts
files, I came across a kind of a workaround. Adding this line:

declare var __dirname, require;


on top of my
gulpfile.ts
solved the issue and I am not getting any error messages anymore. However, this seems to be a dirty hack somehow.

So here's my question: Is this a legit way of setting up a
gulpfile.ts
?

I also took a look at gulpclass files using typings and everything, but I think that this is pretty much of an overkill for a simple build file, isn't it? What are your suggestions? What are the best practices for this?

Answer

Is this a legit way of setting up a gulpfile.ts?

Yes that will work and people do do that. However I recommend you also bring node.d.ts into your project. typings install dt~node --global will do the trick.

Comments