question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using NodeJS builtins

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
theseanlcommented, Jan 2, 2021

I suppose you can do something like this:

{
	"external": {
		"path": "path"
	},
	"compilerFlags": {
		"output_wrapper": "(function(path){%output%})(require('path'))"
	}
}
1reaction
theseanlcommented, Jan 2, 2021

As far as I know, tsickle does not support esModuleInterop flag. tslib.__importDefault is generated by this flag, so if you remove the esModuleInterop 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found