Skip to content

Commit

Permalink
Move the Importing in a CommonJS project detailed explanation to the …
Browse files Browse the repository at this point in the history
…end of the usage, to avoid overtaking the "Install" notes
  • Loading branch information
Borewit committed Dec 10, 2024
1 parent bb100cf commit 1ab0ae1
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,16 @@ npm install file-type

> [!IMPORTANT]
> This package is a **pure** ESM package, which means it can only be imported as an ESM module.
> [Learn more about ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
If you use it with Webpack, you need the latest Webpack version and ensure you configure it correctly for ESM.

### Importing in a CommonJS project

We recommend updating your project to ESM for better compatibility and future-proofing.
However, if you need to use file-type in a CommonJS project, you can do so as follows:

```js
(async () => {
// Dynamically import the file-type ESM module
const { fileTypeFromFile } = await import('file-type');
})();
```
#### CommonJS in TypeScript Projects
In CommonJS TypeScript projects, using `await import` may not work as expected because the TypeScript compiler transpiles `import()` to `require()`,
which is incompatible with ESM.

To work around this, you can use a utility like [load-esm](https://github.com/Borewit/load-esm),
which facilitates dynamic ESM imports in TypeScript CommonJS environments:

```js
import { loadModule } from 'load-esm';
(async () => {
// Dynamically import the file-type ESM module
const { fileTypeFromFile } = await loadModule('file-type');
})();
```
See also:
* [Learn more about ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
* [Importing in a CommonJS project](#importing-in-a-commonjs-project).

## Usage

#### Node.js
### Node.js

Determine file type from a file:

Expand Down Expand Up @@ -119,7 +96,7 @@ const write = fs.createWriteStream(`decrypted.${streamWithFileType.fileType.ext}
streamWithFileType.pipe(write);
```

#### Browser
### Browser

```js
import {fileTypeFromStream} from 'file-type';
Expand All @@ -133,6 +110,32 @@ console.log(fileType);
//=> {ext: 'jpg', mime: 'image/jpeg'}
```

### Importing in a CommonJS project

We recommend updating your project to ESM for better compatibility and future-proofing.
However, if you need to use file-type in a CommonJS project, you can do so as follows:

```js
(async () => {
// Dynamically import the file-type ESM module
const { fileTypeFromFile } = await import('file-type');
})();
```
#### CommonJS in TypeScript Projects
In CommonJS TypeScript projects, using `await import` may not work as expected because the TypeScript compiler transpiles `import()` to `require()`,
which is incompatible with ESM.

To work around this, you can use a utility like [load-esm](https://github.com/Borewit/load-esm),
which facilitates dynamic ESM imports in TypeScript CommonJS environments:

```js
import { loadModule } from 'load-esm';
(async () => {
// Dynamically import the file-type ESM module
const { fileTypeFromFile } = await loadModule('file-type');
})();
```

## API

### fileTypeFromBuffer(buffer)
Expand Down

0 comments on commit 1ab0ae1

Please sign in to comment.