|
| 1 | +# Migrating from Faker v5 to v6 |
| 2 | + |
| 3 | +[[toc]] |
| 4 | + |
| 5 | +### ESM Support |
| 6 | + |
| 7 | +**New Format**: We're now ESM compatible! We've dropped the Browser bundle in favor of ESM. |
| 8 | + |
| 9 | +So if you'd like to use `Faker` in the **browser**, simply include it using a [JavaScript module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#applying_the_module_to_your_html). |
| 10 | + |
| 11 | +```html |
| 12 | +<script type="module"> |
| 13 | + import { faker } from 'https://unpkg.com/@faker-js/faker'; |
| 14 | +
|
| 15 | + console.log(`${faker.name.firstName()} ${faker.name.lastName()}`); |
| 16 | +</script> |
| 17 | +``` |
| 18 | + |
| 19 | +### Remove all references to `faker` from your project. The new package is located at `@faker-js/faker` |
| 20 | + |
| 21 | +:::warning |
| 22 | +You **MUST** swap all references from the `faker` package to the new `@faker-js/faker` package. |
| 23 | + |
| 24 | +In addition to releasing all _future_ versions under the `@faker-js/faker` package namespace, we have also provided all _historical_ versions of Faker. |
| 25 | + |
| 26 | +If you depend on a specific version of Faker you still can reference the version directly. |
| 27 | + |
| 28 | +`npm i @faker-js/[email protected] -D` will work just fine 😄. |
| 29 | +::: |
| 30 | + |
| 31 | +### TypeScript |
| 32 | + |
| 33 | +:::tip TypeScript Improvements |
| 34 | +Faker now ships with its own types! Remove `@types/faker` from your `package.json` to avoid conflicts. |
| 35 | +::: |
| 36 | + |
| 37 | +### Tree-shaking |
| 38 | + |
| 39 | +Faker now supports tree-shaking! We highly recommend that you take advantage of your bundler's tree-shaking capabilities and change how you import Faker. This is especially true if you're importing Faker in the browser. |
| 40 | + |
| 41 | +Faker is a giant package made up of many megabytes of strings. Only import what you need. |
| 42 | + |
| 43 | +:::tip |
| 44 | +Migrating to the new tree-shakeable syntax should be quick and painless. |
| 45 | +::: |
| 46 | + |
| 47 | +For JS: |
| 48 | + |
| 49 | +```js |
| 50 | +const { faker } = require('@faker-js/faker'); |
| 51 | + |
| 52 | +// Or specific locale |
| 53 | +const fakerDe = require('@faker-js/faker/locale/de'); |
| 54 | +``` |
| 55 | + |
| 56 | +For TS: |
| 57 | + |
| 58 | +```ts |
| 59 | +import { faker } from '@faker-js/faker'; |
| 60 | + |
| 61 | +// Or specific locale |
| 62 | +import fakerDe from '@faker-js/faker/locale/de'; |
| 63 | +``` |
| 64 | + |
| 65 | +Please [open an issue on GitHub](https://github.com/faker-js/faker/issues/new?assignees=&labels=pending+triage&template=freestyle.md) if we've missed any steps. |
| 66 | + |
| 67 | +Happy Faking! |
| 68 | + |
| 69 | +- Shini, Jess, and the Faker Team |
0 commit comments