Feature Request: Native Node module resolution support
See original GitHub issueIt would be great for Node module resolution to just work, without extra arguments.
Right now, you need --module_resolution NODE
which does nothing unless --process_common_js_modules
is also passed, and even then, it’s shaky. I would confidently say this doesn’t work in a way that’s reasonably accessible (even though I have managed to get it to work in the past).
I am not sure what flag I need to tell the compiler to look in node_modules/
, even when I manually pass in with --js
. A tale in a few commands:
$ cat test.js
import { Widget } from 'web-widgets';
console.log(Widget);
Execute in Node without issues:
$ node test.js
[Function: e] {
superClass_: {},
exportStyles: [Function (anonymous)],
from: [Function (anonymous)],
styles: [Getter]
}
Check that we definitely have the module installed (hence why the code executes):
$ ls node_modules/web-widgets/
dist LICENSE package.json README.md
Run compiler, complete with flag soup:
$ google-closure-compiler -O ADVANCED --process_common_js_modules --module_resolution NODE --entry_point test.js test.js
test.js:1: ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "web-widgets"
1| import { Widget } from 'web-widgets';
???
I feel like this ticket has been opened a thousand times and it ends with @ChadKillingsworth saying you need to pass in node_modules/
as --js
arguments as well as package.json
. I have tried that, and it does not work. it also parses everything in node_modules/
and dumps a ton of errors. We do not want that. We just want to import the module we are importing.
If I can throw a source file at V8 without issues, I should be able to throw it at the Closure Compiler without issues as well. This is true for everything up until ES6 modules, where everything just breaks down.
Thank you everyone who maintains this tool, it is one of the most important technologies we have in improving the web and I apologize for my frustration. I would be happy to contribute regarding this issue.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:11 (4 by maintainers)
Top GitHub Comments
I am a little confusion about this strange behavior. If I use
java -jar compiler.jar --js src\a.js --js "D:\myweb\node_modules\js-base64\package.json" --js "D:\myweb\node_modules\js-base64\base64.mjs" --compilation_level ADVANCED --module_resolution NODE --process_common_js_modules
to compile the code, I will getFailed to load module "js-base64"
. If I use releative path to replace absolute path (for example:java -jar compiler.jar --js src\a.js --js "node_modules\js-base64\package.json" --js "node_modules\js-base64\base64.mjs" --compilation_level ADVANCED --module_resolution NODE --process_common_js_modules
), it works fine. I am sorry that my English is not well.I’ve long wanted to just be able to use typescript d.ts files as externs with no additional work.