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.

Feature Request: Native Node module resolution support

See original GitHub issue

It 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lifegpccommented, Mar 13, 2021

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 get Failed 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.

1reaction
ChadKillingsworthcommented, Jan 4, 2021

I’ve long wanted to just be able to use typescript d.ts files as externs with no additional work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CommonJS modules | Node.js v19.2.0 Documentation
CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and ......
Read more >
Documentation - Module Resolution - TypeScript
There are two possible module resolution strategies: Node and Classic. You can use the moduleResolution option to specify the module resolution strategy.
Read more >
11 Features in Node.js 18 you need to try - NodeSource
js. This is a standardized web API for conducting HTTP or other types of network requests. Previously Node.js did not support it by...
Read more >
5 ways to make HTTP requests in Node.js - LogRocket Blog
Make HTTP requests in Node.js using the native module as well as npm ... It also features a promise-based API, and HTTP/2 support...
Read more >
Module Resolution | Metro - Meta Open Source
Metro needs to know where in your project to load the react-native module from. ... Metro implements a version of Node's module resolution...
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