-
Notifications
You must be signed in to change notification settings - Fork 6
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
fetch is not defined
error due to missing fetch
import in tableland-calls.ts
#116
Comments
I get the logic behind Node 18 not needing the |
A member from Discord has noted a similar experience: And also, running
|
If you follow the linked recommendation in the docs, are you able to avoid the above issue, even with an ESM build? This is linked from our docs, and suggests having an entry point which polyfills the fetch API: https://github.com/node-fetch/node-fetch#providing-global-access. There are some funny things with node-fetch between version 2 and version 3 (which is ESM only). I highly doubt we'll get anything that works smoothly between NodeJS and the browser without fetch polyfills at this early stage... |
@dtbuchholz I installed the latest node, I think it's new today, v18.8.0 and removed the |
Sorry for the delay. Gonna just close this out -- I think I just have something weird going on with my mac. I'm going to try and debug it or change to a new one that I have, and once that's resolved, I'll consider revisiting this if it's still an issue. |
I ran into this issue as well |
Did I miss it in the readme? could you please share it here?
I do understand we can do this. However, it just seems a bit incomplete to not have this included into the library if there is an dependency for it. |
@tansanDOTeth it's not in the README, but let me update that; it's the same link as the one noted. If you use Node v18, the issue is resolved (as of a few days ago, v18 is now LTS and v16 & v14 are maintenance: https://github.com/nodejs/Release). This is a tough one. Having the SDK quietly polyfill fetch feels heavy handed and prone to problems, but we could be wrong here. As an example, when On the flip side, this is the most common issue that people run into when using the SDK (including myself), which makes it challenging to ignore. |
Describe the bug
I'm trying to simply use Tableland in an ES module, but I always get the error
fetch is not defined
, stemming fromtableland-calls.js
(using@tableland/sdk@3.1.0
). I usenode v18.7.0
andnpm 8.15.0
. Mypackage.json
has atype: module
defined, and I successfullyimport fetch from "node-fetch"
and usefetch
elsewhere in my code. I try replacing myfetch
import with the following 1-liner in my code (as recommended per the JS SDK docs for CJS/require
), but it has no impact:The only way to stop this error in my application is (usually) by reverting to CJS over ESM and then using the 1-liner/polyfill, which is not ideal. Thus, I'm assuming the issue must originate from Tableland, not my code.
After adding this 1-liner to
tableland-calls.js
indist
, the error is resolved. Upon digging into the SDK code a bit, it looks like the error stems from the facttableland-calls.ts
does not importnode-fetch
. If you importnode-fetch
intotableland-calls.ts
and build, everything is fixed; no morefetch is not defined
error.Disclaimer: I know we use a modern version of Node, but I'm using Node v18 and still having this issue...so I'm not sure exactly why this issue is happening, to be honest. I would expect that I wouldn't have to import
node-fetch
with Node 18. Albeit, I'm not an expert on these intricacies. The fix described below resolves the error, and I've heard this problem pop up a few times from others, before. Not exactly sure if they were using CJS or ESM or what version of Node, though.To Reproduce
node-fetch
as well as@tableland/sdk
; call the fileindex.js
.index.js
:read
query after connecting. You will get an error:index.js
, and nothing is resolved.node_modules/@tableland/sdk/dist/esm/src/lib/tableland-calls.js
, and things work without errors:Alternatives:
Here's the fix:
tableland-calls.ts
, add@types/node-fetch
:node-fetch
intotableland-calls.ts
:index.js
file mentioned above works as expected without any messynode-fetch
issues.Note: in step (2) of the fix, if you don't
--force
, thenethers
andsiwe
won't let you install, throwing this error:Expected behavior:
Be able to use Tableland in a JS ESM without polyfills or workarounds for
node-fetch
.Environment:
@tableland/sdk 3.1.0
node v18.7.0
npm 8.15.0
The text was updated successfully, but these errors were encountered: