davidmyersdev / vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments.
MIT License
263 stars 17 forks source link

Uncaught Error: Module "node:path" has been externalized for browser compatibility. Cannot access "node:path.join" in client code. #4

Closed milahu closed 1 year ago

milahu commented 1 year ago

ideally node: protocol should work

quickfix: remove the node: protocol from imports

-import { join as joinPath } from 'node:path'
+import { join as joinPath } from 'path'
ghost commented 1 year ago

I also got into the same problem as you did. My site uses not Vite but rather Astro; however, I think they're not different from the rest since Astro is powered by Vite underneath.

Reproduction

import { readdirSync } from "node:fs"

const format = (text: string): string => {
    const capitalizedString = text.charAt(0).toUpperCase() + text.slice(1)
    const finalString = capitalizedString.replace(/\-/gm, ' ')

    return finalString
}

export const getFilesInDirectory = (path: string): { text: string, link: string }[] => {
    const dir = path.split('/').pop() as string
    const filesInDir =
        readdirSync(path, { withFileTypes: true })
            .filter((item) => !item.isDirectory())
            .map(({ name }) => name.split('.')[0])
    const files = filesInDir.map(file => {
        return {
            text: format(file),
            link: `${dir}/${file}`,
        }
    })

    return files
}

Expected behavior

Click the search input and be able to see the Algolia-integrated search box.

Actual behavior

Nothing happens, instead, I see the following error log in the devtool console:

Screenshot 2022-09-25 174052

I did install your plugin in order to get it fixed, but no magic happens.

milahu commented 1 year ago

i moved to https://github.com/sodatea/vite-plugin-node-stdlib-browser

... just works

davidmyersdev commented 1 year ago

@quoc1707 my other plugin does not take node: protocol imports into account, but I'll likely want to update it to allow those to be externalized. As for this package, I'm going to guess the underlying library I'm using doesn't recognize the protocol imports either. If you don't mind me asking, what's your use case for using the node: protocol if you plan to polyfill the imports anyway?

milahu commented 1 year ago

what's your use case for using the node: protocol if you plan to polyfill the imports anyway?

make node imports explicit

davidmyersdev commented 1 year ago

This has been fixed in v0.3.0.

ghost commented 1 year ago

@quoc1707 my other plugin does not take node: protocol imports into account, but I'll likely want to update it to allow those to be externalized. As for this package, I'm going to guess the underlying library I'm using doesn't recognize the protocol imports either. If you don't mind me asking, what's your use case for using the node: protocol if you plan to polyfill the imports anyway?

I just want to clarify that the import is a Node.js built-in module, since there're many alternative packages for these nowadays, like fs-extra or pathe.

davidmyersdev commented 1 year ago

@quoc1707 I figured that's what you meant. It definitely makes sense that you'd want to be able to polyfill the builtins, and I was able to get it working with node-stdlib-browser. I looked into sodatea/vite-plugin-node-stdlib-browser as suggested by @milahu, but it didn't correctly handle calls to process in the production build (dev server worked fine though). I think the newest version is a bit more robust, and I plan to introduce some configuration to allow you to toggle the node: behavior. I can imagine a scenario where you might want to polyfill regular imports (e.g. 'fs') and leave protocol imports alone (e.g. node:fs), and having a bit more control over the polyfills, in general, is something I will likely need in the near future.