denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
93.99k stars 5.23k forks source link

Bug: No named export `gql` in `graphq-tag` found #25311

Open marvinhagemeister opened 2 weeks ago

marvinhagemeister commented 2 weeks ago

Reported in Discord: https://discord.com/channels/684898665143206084/1279102082452029520/1279102082452029520

Steps to reproduce

Run this snippet:

import { gql } from "npm:graphql-tag@2.12.6";
console.log(gql);

Output:

error: Uncaught SyntaxError: The requested module 'npm:graphql-tag@2.12.6' does not provide an export named 'gql'
import { gql } from "npm:graphql-tag@2.12.6";
         ^
    at <anonymous> (file:///Users/marvinh/dev/test/deno-graphql-tag/main.ts:1:10)

The problem occurs because Deno picks the wrong entry point.

  "main": "./main.js", // <- deno picks this one
  "module": "./lib/index.js", // <- ...but it should have picked this
  "jsnext:main": "./lib/index.js",

Version: Deno 1.46.2+d71eebb

dsherret commented 2 weeks ago

The problem occurs because Deno picks the wrong entry point.

Deno is picking the same entry point as Node. I believe it's correct to use "main". I think probably there is something wrong with the cjs export analysis.

> cat index.mjs
import * as gql from "graphql-tag";
console.log(gql);
> deno -A index.mjs                                                                                                                                                                                                                                          
[Module: null prototype] {                                                                                                                                                                                                                                                            
  default: <ref *1> [Function: gql] {                                                                                                                                                                                                                                                 
    gql: [Circular *1],                                                                                                                                                                                                                                                               
    resetCaches: [Function: resetCaches],
    disableFragmentWarnings: [Function: disableFragmentWarnings],
    enableExperimentalFragmentVariables: [Function: enableExperimentalFragmentVariables],
    disableExperimentalFragmentVariables: [Function: disableExperimentalFragmentVariables],
    default: [Circular *1]
  }
}
> node index.mjs
[Module: null prototype] {
  __esModule: undefined,
  default: <ref *1> [Function: gql] {
    gql: [Circular *1],
    resetCaches: [Function: resetCaches],
    disableFragmentWarnings: [Function: disableFragmentWarnings],
    enableExperimentalFragmentVariables: [Function: enableExperimentalFragmentVariables],
    disableExperimentalFragmentVariables: [Function: disableExperimentalFragmentVariables],
    default: [Circular *1]
  },
  disableExperimentalFragmentVariables: [Function: disableExperimentalFragmentVariables],
  disableFragmentWarnings: [Function: disableFragmentWarnings],
  enableExperimentalFragmentVariables: [Function: enableExperimentalFragmentVariables],
  gql: <ref *1> [Function: gql] {
    gql: [Circular *1],
    resetCaches: [Function: resetCaches],
    disableFragmentWarnings: [Function: disableFragmentWarnings],
    enableExperimentalFragmentVariables: [Function: enableExperimentalFragmentVariables],
    disableExperimentalFragmentVariables: [Function: disableExperimentalFragmentVariables],
    default: [Circular *1]
  },
  resetCaches: [Function: resetCaches]
}
igl commented 1 week ago

The module is clearly cjs and re-exports a function from a UMD module

module.exports = require('./lib/graphql-tag.umd.js').gql;

They use roll-up to generate it and it has been 3 years since the last update. These hybrid packages make me mad.

The published code: https://www.npmjs.com/package/graphql-tag?activeTab=code