Skip to content
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

Global namespace collision for native node fetch when near-api-js is imported #1193

Closed
4 tasks done
andrew-scott-fischer opened this issue Oct 27, 2023 · 1 comment
Closed
4 tasks done
Labels
bug Something isn't working

Comments

@andrew-scott-fischer
Copy link
Contributor

andrew-scott-fischer commented Oct 27, 2023

Prerequisites

  • I'm using the latest version of near-api-js.
  • I have tried to start with a fresh project and reproduce the defect with minimal code changes.
  • I have read the console error messages carefully (if applicable).

Description

Attempting to use the node native fetch API in node >= 18 when the near-api-js is imported directly or by a transitive dependency results in the node global fetch property being overwritten by near-api-js.

These two lines of code here and here explicitly override the global fetch property.

They should not do this.

Reproducible demo

This can be reporoduced in a node console:

$ node
Welcome to Node.js v18.17.1.
Type ".help" for more information.
> fetch
[AsyncFunction: fetch]
> require('near-api-js')
{
  keyStores: {
    KeyStore: [Getter],
    InMemoryKeyStore: [Getter],
    BrowserLocalStorageKeyStore: [Getter],
    UnencryptedFileSystemKeyStore: [Getter],
    MergeKeyStore: [Getter]
  },
  accountCreator: [Getter],
  providers: [Getter],
  utils: [Getter],
  transactions: [Getter],
  validators: [Getter],
  multisig: [Getter],
  Account: [Getter],
  Connection: [Getter],
  Contract: [Getter],
  InMemorySigner: [Getter],
  Signer: [Getter],
  KeyPair: [Getter],
  Near: [Getter],
  ConnectedWalletAccount: [Getter],
  WalletConnection: [Getter],
  connect: [Getter],
  DEFAULT_FUNCTION_CALL_GAS: [Getter]
}
> fetch
[Function: default_1]

Steps to reproduce

  1. open a project which has near-api-js somewhere in it's dependency tree
  2. open a node console
  3. type fetch
  4. see output is [AsyncFunction: fetch]
  5. type require('near-api-js')
  6. type fetch
  7. see output is [Function: default_1]

Expected behavior

I would not expect a dependency to overwrite the global fetch property.

Actual behavior

near-api-js overwrites the global fetch property

Your environment

  • NEAR JavaScript API version used: 2.1.4
  • Frontend framework (if applicable): N/A
  • Relevant dependencies (if applicable): near-api-js

Self-service

@andrew-scott-fischer andrew-scott-fischer added the bug Something isn't working label Oct 27, 2023
@andrew-scott-fischer
Copy link
Contributor Author

I'm having issues getting tests to run. After installing the requisite tools, my pnpm-lock.yaml changes versions from 5.4 to '6.0'. Is there something I'm missing?

@frol frol closed this as completed Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants