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

Use with node #95

Closed
efattal opened this issue Dec 13, 2023 · 4 comments
Closed

Use with node #95

efattal opened this issue Dec 13, 2023 · 4 comments

Comments

@efattal
Copy link

efattal commented Dec 13, 2023

Hello,

I would like to use is-online in the main process of an Electron app. My code is simply this:

import isOnline from "is-online";

mainWindow
    .on("ready-to-show", async () => {
        setInterval(async () => {
            ipcMain.emit("online:status", await isOnline());
        }, 5000);
    })

But I get this error :

(node:14752) UnhandledPromiseRejectionWarning: ReferenceError: navigator is not defined`

It's like if the executed code comes from the browser.js instead of index.js. What's wrong with my implementation? Thanks for your help.

@sindresorhus
Copy link
Owner

Your bundler config is incorrect. The package correctly defines the exports for Node.js and browser:

is-online/package.json

Lines 17 to 21 in b0d113a

"exports": {
"types": "./index.d.ts",
"node": "./index.js",
"default": "./browser.js"
},

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2023
@efattal
Copy link
Author

efattal commented Dec 14, 2023

Thanks a lot for you answer. I saw that your exports correctly defines the target, that's why I was suprised. I'm using electron-forge+vite. Any tip about what I should check in the configuration? Thanks again for your help.

@efattal
Copy link
Author

efattal commented Dec 14, 2023

Actually my vite.main.config.js contains this but it is not enough. Any idea why?

export default defineConfig({
  resolve: {
    // Some libs that can run in both Web and Node.js, such as `axios`, we need to tell Vite to build them in Node.js.
    browserField: false,
    mainFields: ["module", "jsnext:main", "jsnext", "node"],
  },
  build: {
    rollupOptions: {
      external: nativeModules,
    },
  },
});

@efattal
Copy link
Author

efattal commented Dec 14, 2023

OK, so finally the correct configuration needs the conditions field.

export default defineConfig({
  resolve: {
    // Some libs that can run in both Web and Node.js, such as `axios`, we need to tell Vite to build them in Node.js.
    browserField: false,
    mainFields: ["module", "jsnext:main", "jsnext"],
    conditions: ["node"],
  },
  build: {
    rollupOptions: {
      external: nativeModules,
    },
  },
});

Thanks for all the fantastic job you do. Kudos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants