Using NodeJS builtins
See original GitHub issueI was extremely pleased to see my TS compile to perfect -O ADVANCED
output after adding the tsconfig.json
, but when I add a simple builtin import:
import { exec } from 'child_process';
I receive the following error:
TSCC: tsickle converts TypeScript modules to Closure modules via CommonJS internally."module" flag is overridden to "commonjs".
TSCC: tsickle uses a custom tslib optimized for closure compiler. importHelpers flag is set.
TSCC: Module name child_process was not provided as a closure compilation source
TSCC: The compilation has terminated with an error.
This is one of the larger pains with Closure Compiler in general - Node builtins should be automatically detected (opt-out with a flag) and supplied as externs, and left untouched in output. TSCC does a fantastic job of handling thousands of other problems, but if we can’t compile programs containing Node builtins, we can’t realize the full benefit.
Any thoughts on this or recommendations? The test program is below.
import path from 'path';
console.log(path.resolve('.'));
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Node.js Built-in Modules - W3Schools
Node.js Built-in Modules ; util, To access utility functions ; v8, To access information about V8 (the JavaScript engine) ; vm, To compile...
Read more >The built-in globals in Node.js
Node.js has a number of built-in global identifiers that every Node.js developer should have some familiarity with. Some of these are true ...
Read more >Useful Built-in Node.js APIs - SitePoint
Learn about the most used and useful APIs built in to the standard Node.js runtime to save you time and improve your app's...
Read more >How does Node.js load its built-in/native modules?
--node-builtin-modules-path configures Node.js to look up the builtins from disk at runtime, with the caveat that you cannot add or delete files ...
Read more >Built in Node Js Modules - DEV Community
Built in Node Js Modules · Prerequisite · TOC · Import and Export modules · Path module : To handle file paths ·...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I suppose you can do something like this:
As far as I know, tsickle does not support
esModuleInterop
flag.tslib.__importDefault
is generated by this flag, so if you remove theesModuleInterop
flag, it will compile well, I guess. I am not sure what--dependency_mode PRUNE_LEGACY
exactly does, but the compilation should not fail without adding any additional flags to the closure compiler.Most of time, you can replace default imports to namespace imports (
import * as ns from '...'
) and the runtime behavior will be the same.It’d be better to have
esModuleInterop
supported, but IMO a better place for the support would be from tsickle, not from here, that’s why I am not currently working on it.