How do you make closure-compiler understand NodeJS built-in module requires?

683 views
Skip to first unread message

Bradford Smith

unread,
Jun 28, 2018, 2:07:43 PM6/28/18
to Closure Compiler Discuss
Is there anyone out there who has had success using closure-compiler with NodeJS and can tell us how to make closure-compiler OK with requires of built-in modules where there's no JS source file?

e.g.
const path = require('path');

Thanks,
Bradford

Christopher Allen

unread,
Jul 9, 2018, 5:51:38 PM7/9/18
to Closure Compiler Discuss
I contacted the engineer who originally checked these externs into the closure-compiler repository, and he sent the following illuminating but somewhat disappointing reply:

On 3 July 2018 at 12:28, Nick Santos wrote:
At the time (mid-2014), there were a bunch of closure-compiler forks around that were trying to reconcile closure-compiler's view of the world with NodeJS's view of the world. I saw the same type-checked externs for NodeJS builtins in a few different places.

We decided to check them into the closure-compiler repo. I hoped that this would:
1) reduce duplicated effort
2) demonstrate willingness from closure-compiler team in making them work
3) help the different forks converge towards a solution that was baked into closure-compiler 
 
In practice, this didn't happen. Typescript picked up steam. FlowJS came out later in 2014. Both of them had better answers to type-checking NodeJS apps.  So I think most of the people working on this decided to back those solutions instead.

I must say that this certainly parallels my experience of asking questions about more obscure corners of the closure-compiler type system and getting replies along the lines of "the answer to your question is <long and not entirely satisfactory solution>, but wait until you see this one neat trick using TypeScript!"

Martin Probst

unread,
Jul 10, 2018, 2:57:52 AM7/10/18
to closure-compiler
This might not be an immediate solution to your problem, but my group builds and supports TypeScript / Closure interop tooling that might allow you to use both at the same time. See github.com/angular/tsickle (produce Closure-compliant JS from TS) and github.com/angular/clutz (produce .d.ts from Closure code).

We don't have a lot of experience using this with NodeJS, as Closure Compiler's strengths in optimization are a bit less useful there, but this tool chain is used for several very large web frontends at Google that have a mixed code base of TS and Closure JS code.

--

---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/closure-compiler-discuss/89e93f72-35ae-4810-8565-18113fe7ef06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sato Teppei

unread,
Jul 10, 2018, 11:29:52 AM7/10/18
to Closure Compiler Discuss
Closure Compiler doesn't support external type definition (externs) for CommonJS or ES Modules.
Only global externs are supported.

It's a very important feature for users outside of Google.

2018年6月29日金曜日 3時07分43秒 UTC+9 Bradford Smith:

Christopher Allen

unread,
Jul 16, 2018, 2:47:07 PM7/16/18
to Closure Compiler Discuss
Following up on my own previous message: after a bit of experimentation, I found a way to write externs for the built-in modules by making npm-alike stubs for them.  Unfortunately it only works if you're using --checks-only, but hopefully the trick will be of use to others.
Reply all
Reply to author
Forward
0 new messages