google / google/closure-compiler
JSC_JS_MODULE_LOAD_WARNING - Unable to debug React dependency failure
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
# Background
I have spent the last 24 hours trying to get the compiler to compile a script that requires React (a totally insane use case I'm sure). I browsed no less than 6 threads featuring a fellow named Chad Killingsworth who absolutely insists that ES6/CommonJS modules can be depended on with a variety of magic spells:
- include `--process_common_js_modules`
- include `--module_resolution NODE`
- include `--js node_modules/react/**/*.js` and `--js node_modules/react-dom/...` (because why would the compiler do this for you if you have `--module_resolution NODE` set when you can manually include all your dependencies by hand, even if they're defined in a syntactically repetitive way that Closure Compiler is, you know, built to recognize and automate?)
And so on. Not only do these not help in this case, but I can't even get the compiler to import the standalone React script as a sanity check.
# Issue
First, some sanity checks. `react-test.js` is literally just a copy of `node_modules/react/cjs/react.production.min.js` (copied to make sure this wasn't CC's node module resolution logic breaking). Node can load it without issue:
`$ node -e 'console.log(require("./react_test.js"))'`
```
{
Children: {
map: [Function: map],
forEach: [Function: forEach],
count: [Function: count],
toArray: [Function: toArray],
only: [Function: only]
},
Component: [Function: F],
Fragment: Symbol(react.fragment),
...
version: '16.13.1'
}
```
Our test file `test.js`, which depends on the React library:
```
const React = require('./react_test.js');
class Test {
test() {
return React.Component;
}
}
module.exports = Test;
```
Try to compile:
```
$ java -jar ~/closure.jar \
--compilation_level ADVANCED \
--process_common_js_modules \
--module_resolution NODE \
--js test.js
```
```
test.js:3: ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "./react_test.js"
const React = require('./react_test.js');
^
1 error(s), 0 warning(s)
```
**"Failed to load module."** Not the most descriptive error. Any ideas how to proceed? If the Node runtime can load the module and work with it, and CC cannot, I can't really buy the "it's a feature, not a bug, just use the right flags" line.
Ultimately, there just seems to be few excuses for this failure - this is a Node module, and there is no good reason why CC cannot just import it off-rip with `require('react')` or `import React from 'react'` if we include `--module_resolution NODE`, and especially no good reason why it won't load the standalone script (no `package.json` logic required!) via `require('./react_test.js')` without providing a meaningful error message.
It seems like this is a relatively common issue with the compiler, and I have tried all of the remedies in most of these posts. Using CC for vanillaJS is a beautiful, seamless experience that cannot be beaten. We should try to keep that same energy for modern JS as well, especially when it comes to the React library.

Contributor guide
Assessment
This issue has not been assessed yet.