google / google/closure-compiler

CommonJS modules `missingRequire` throws 'ERROR - missing require'

Open
#3,029 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

The CommonJS module transform seems to run afoul of the `missingRequire` checks. I've noticed two different errors, depending on whether the imported module is used as a function or as a parent class.

All commands were run with the v20180716 version of the compiler, running with `--jscomp_off=lintChecks` due to #2605.
```
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20180716
Built on: 2018-07-17 21:55
```

Plain functions
---
**other.js**
```javascript
function Other() {
}

module.exports = Other;
```

**example.js**
```javascript
const Other = require('./other');

Other();
```

Results in:
```
$ closure-compiler --jscomp_error=* --jscomp_off=lintChecks --module_resolution=NODE --process_common_js_modules other.js example.js

example.js:3: ERROR - missing require: 'module$other'
Other();
^^^^^^^

1 error(s), 0 warning(s), 100.0% typed
```

Yet when compiling `other.js` standalone, `module$other` is defined!
```
$ closure-compiler --jscomp_error=* --jscomp_off=lintChecks --module_resolution=NODE --process_common_js_modules other.js

var module$other={default:function(){}};
```

ES6 Classes
---
**other.js**
```javascript
class Other {
constructor() {
}
}
module.exports = Other;
```

**example.js**
```javascript
const Other = require('./other');

class Example extends Other {

}

module.exports = Example;
```

Results in:
```
$ closure-compiler --jscomp_error=* --jscomp_off=lintChecks --module_resolution=NODE --process_common_js_modules other.js example.js

example.js: ERROR - Bad type annotation. Unknown type module$other.default

example.js:5: ERROR - missing require: 'module$other.default'
class Example extends Other {
^^^^^^^

2 error(s), 0 warning(s), 92.3% typed
```

What is interesting, is that when I compile `other.js` standalone, it does properly define `module$other.default`:
```
$closure-compiler --jscomp_error=* --jscomp_off=lintChecks --module_resolution=NODE --process_common_js_modules other.js

var module$other={default:function(){}};
```

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.