google / google/closure-compiler

ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "util"

Open
#3,719 5 comments 1 reaction 1 assignee Claimed by @ChadKillingsworth View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

## Context

I would like to compile a node based project which dependencies include `util` and other node builtins.

However, the compiler won't load them despite using the `--process_common_js_modules` and `--module_resolution NODE` flags. Every other dependencies is loaded properly with `--js`.

My understanding was that, by using `--externs` and `--output_wrapper` (or `--output_wrapper_file`), the compiler would inject and use them as global variables within the final output. I've used [@externs/nodejs](https://www.npmjs.com/package/@externs/nodejs) to that effect.

Following here is a very basic failing setup:

## Setup

### Compiler
``` sh
npm i -D google-closure-compiler
# + google-closure-compiler@20201006.0.0
```

> I also tried `google-closure-compiler@20191111.0.0` from a year ago in case it would be related to the latest version.

### Source

```javascript
const util = require('util')

console.log('it works...')

module.exports = {}
```

### Command

```sh
google-closure-compiler index.js --process_common_js_modules --compilation_level ADVANCED --module_resolution NODE
```

### Error

```sh
index.js:1:13: ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "util"
1| const util = require('util')
^

1 error(s), 0 warning(s)
```

## Question

Is my initial assumption correct? Or is it not possible to compile with node builtins.
If yes, could you help out with the configuration?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.