sveltejs / rollup-plugin-svelte

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

Unable to use file-type on the browser side #199

Closed MiteshGandhi2893 closed 2 years ago

MiteshGandhi2893 commented 2 years ago

We are trying to use the file-type npm library on the browser side (chrome). Below is the code for the same in angular.

import { fileTypeFromBuffer } from 'file-type';

async getFileType() {
  const fileBuffer = await getLocalFile();
   const type = await fileTypeFromBuffer(fileBuffer);
   return type;
}

We are getting following errors, while running command npm start.

Error: ./node_modules/file-type/core.js
Module not found: Error: Can't resolve 'node:buffer' in '/home/demo/node_modules/file-type'

Error: ./node_modules/file-type/core.js
Module not found: Error: Can't resolve 'node:stream' in '/home/demo/node_modules/file-type'

Error: ./node_modules/file-type/core.js
Module not found: Error: Can't resolve 'strtok3/core' in '/home/demo/node_modules/file-type'

We are using the latest version for file-type i.e. 17.1.1 Please let us know the solution for this, or are there any steps or changes to be done before using the library on the browser side. We tried various solutions related to external dependencies but still couldn't get it to work.

ghostdevv commented 2 years ago

You would need to use a library that supports Web

Conduitry commented 2 years ago

This is unrelated to Svelte. If you want to use a library that uses Node APIs on the browser, you'll have to polyfill them in some way or use another Rollup plugin to replace them with browser-compatible versions.