sveltejs / rollup-plugin-svelte

Compile Svelte components with Rollup
MIT License
502 stars 79 forks source link

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

Closed iohzrd closed 2 years ago

iohzrd commented 2 years ago

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.

Conduitry commented 2 years ago

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 https://github.com/sindresorhus/file-type/issues/502 that I just found about this.