sveltejs/rollup-plugin-svelte

Error when trying to import/use "file-type" library.

iohzrd opened this issue · 1 comments

When I import it how the doc suggests.
import { fileTypeFromBuffer } from "file-type";
I get:

[!] (plugin svelte) Error: No such built-in module: node:buffer/package.json
Error: No such built-in module: node:buffer/package.json
    at new NodeError (node:internal/errors:372:5)
    at Function.Module._load (node:internal/modules/cjs/loader:785:13)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.resolveId (/home/iohzrd/src/identia/node_modules/rollup-plugin-svelte/index.js:67:11)
    at /home/iohzrd/src/identia/node_modules/rollup/dist/shared/rollup.js:22779:37

I've tried importing it like so:
import { fileTypeFromBuffer } from "file-type/index";
But then I get:

(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
file-type/index (imported by src/components/Post.svelte)
(!) Missing global variable name
Use output.globals to specify browser global variable names corresponding to external modules
file-type/index (guessing 'index$5')

I tried various solutions related to external dependencies but still couldn't get it to work.

This is unrelated to Svelte. If a library depends on the Node buffer library, that will need to be polyfilled (by a bundler plugin) with a browser-compatible implementation.

The browser export for that package (https://unpkg.com/browse/file-type@17.1.1/browser.js) contains references to node:buffer, which doesn't sound correct to me. There's a discussion on sindresorhus/file-type#502 that I just found about this.