Skip to content

Compiler doesn't resolve node dependency using "main" key value in package.json #3163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jmosney opened this issue Dec 5, 2018 · 3 comments
Labels
triage-done Has been reviewed by someone on triage rotation.

Comments

@jmosney
Copy link

jmosney commented Dec 5, 2018

package.json can declare a key "main" with a value that's a reference to a file in the package (npm docs). However Google Closure Compiler seems to ignore that value and always look for a file "index.js" as the default. So when a file using that package declares a dependency using require, GCC throws an error.

Example

End user's project

index.js
const xtend = require('xtend');
package.json
{ "dependencies": { "xtend": "4.0.0" } }

npm project

immutable.js
... useful utility...

package.json
{ "main": "immutable" }

When the end user runs GCC (ensuring they've supplied the xtend package using the --js flag to the command) they will get this error:
my-project.js:1: ERROR - Failed to load module "xtend" var xtend = require('xtend'); ^ 1 error(s)

This is sort of related to #3149 (Packages with dir as "main" in package.json do not resolve.) but I'm filing it anyway just in case the solution for that bug doesn't address this.

I've patched xtend here to fix material-components-web compilation via GCC for my project (I pull in my fork via my package.json).

@EatingW
Copy link
Contributor

EatingW commented Dec 5, 2018

Created Google internal issue b/120550812.

@EatingW EatingW added the triage-done Has been reviewed by someone on triage rotation. label Dec 5, 2018
@ChadKillingsworth
Copy link
Collaborator

@jmosney Did you supply the package.json file to the compilation? You need to pass --js node_modules/immutable/package.json along with your other source files.

@jmosney
Copy link
Author

jmosney commented Dec 7, 2018

Ah, that did it! Thanks very much Chad!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage-done Has been reviewed by someone on triage rotation.
Projects
None yet
Development

No branches or pull requests

3 participants