D
Deno

help

External package code throws error: window is undefined

Yyuusf11/8/2023
Hello, I have imported an npm package to my project. And in this package's code, there is a condition:
if (typeof window === 'undefined') {
throw new Error('Error initializing the sdk: window is undefined');
}
if (typeof window === 'undefined') {
throw new Error('Error initializing the sdk: window is undefined');
}
Program throws "window is undefined" error at this condition above. I am able to access window object in my Deno project. Also, when I add a breakpoint to condition line in the cache folder files, I can access to window object from debug console as well. It would be great if someone help me resolve this problem. Thanks in advance.
DAHDeno AI Helper11/8/2023
Hello! 🦕 It seems like the npm package you're using is trying to access the window object, which is a global object typically available in browser environments. In Deno, the window object is not globally available like in a browser environment, but it is part of the built-in libraries provided to the TypeScript compiler. The documentation mentions that Deno has several built-in libraries that are not present in other platforms, like tsc. This is what enables Deno to properly check code written for Deno. However, in some situations, this automatic behavior can cause challenges, for example like writing code that is intended to also run in a browser. In these situations, the "lib" property of a compilerOptions can be used to modify the behavior of Deno when type checking code. If you're trying to use a package that's designed for the browser, you might need to adjust your compilerOptions to include the "deno.window" library. Here's an example of what your compilerOptions might look like:
{
"compilerOptions": {
"lib": ["deno.window"]
}
}
{
"compilerOptions": {
"lib": ["deno.window"]
}
}
This should allow Deno to recognize the window object. However, please note that this might not fully solve your problem if the npm package you're using relies on other browser-specific APIs. If you're still having trouble, could you please provide more information about the npm package you're using? That might help us understand the issue better.
Yyuusf11/8/2023
I tried adding "deno.window" to "lib" in compilerOptions but it didn't work. btw, I am using Deno v1.38.0
Mmarvinh.11/8/2023
npm packages get the node global object and "deno code" get's deno's global object. That's why window is not present
Yyuusf11/9/2023
I see, that makes sense. Is there a way to make npm packages run in deno context? Or any other workaround would be appreciated I ended up moving the npm package files inside the deno project which resolved my issue. Thanks @marvinh. for your reply

Looking for more? Join the community!

Recommended Posts
Error with importsI have this fancy peace of code that import a bunch of files from a directory: ```ts const utils = Deno.env via fileHow can I use set env from a file and get them via deno.env.get() function as i want to push my codeRunning LLaMA on DenoHello! I use Deno to run my apps on the edge, what Deno solution or other provider would be ideal Occasional very slow isolate start timesI have a Vue.js app (using Vite) project with a few edge functions running on Deno Deploy. What I'mChanging terminal zoom in windows?Is it possible to change the terminal zoom using deno? Also, this is about windows terminal, not conWhimsical error shows up when I run a fileI'm trying to make a discord bot with Deno and d.js but for some reasons I get this error ```tex errI cannot able to install itwhen i run ```sh irm https://deno.land/install.ps1 | iex ``` i got this error messageDeno lint custom rulesHey is there currently a way to create custom rules for Deno lint? And if so how can I specify a cususing OpenAI npm in Deno deploy playground:i don't know how to return the resualt , now this just return [object Object] i don't how to use asFrontend & Backend in one Deno Deploy projectI have a Nuxt 3 frontend and an Oak backend. Is it technically possible to deploy both in one deno dIs there a split_to_array function like join_to_stringI need to split a string with some delimiter `=[, ` and ask my self if there is something in deno whHow to set proxy to overall all requests coming from application?In Node JS, we can `import https` and set `https.proxy = "proxyDetails` However, I couldn't find a w