2018-05-27 15:17:38 -03:00
---
id : browser
title : Browser
---
2021-03-15 02:25:18 +02:00
Run Prettier in the browser using its **standalone** version. This version doesn’ t depend on Node.js. It only formats the code and has no support for config files, ignore files, CLI usage, or automatic loading of plugins.
2020-11-05 06:07:29 +02:00
The standalone version comes as:
2022-10-06 15:20:19 +08:00
- ES modules: `standalone.mjs` , starting in version 3.0 (In version 2, `esm/standalone.mjs` .)
2020-11-05 06:07:29 +02:00
- UMD: `standalone.js` , starting in version 1.13
2018-05-27 15:17:38 -03:00
2020-06-28 03:47:29 +02:00
The [`browser` field ](https://github.com/defunctzombie/package-browser-field-spec ) in Prettier’ s `package.json` points to `standalone.js` . That’ s why you can just `import` or `require` the `prettier` module to access Prettier’ s API, and your code can stay compatible with both Node and the browser as long as webpack or another bundler that supports the `browser` field is used. This is especially convenient for [plugins ](plugins.md ).
2020-05-25 09:51:54 +03:00
2018-05-27 15:17:38 -03:00
### `prettier.format(code, options)`
2021-03-15 02:25:18 +02:00
Required options:
2018-05-27 15:17:38 -03:00
2021-03-15 02:25:18 +02:00
- **[`parser` ](options.md#parser ) (or [`filepath` ](options.md#file-path ))**: One of these options has to be specified for Prettier to know which parser to use.
2018-05-27 15:17:38 -03:00
2025-01-30 03:16:21 +01:00
- **`plugins` **: Unlike the `format` function from the [Node.js-based API ](api.md#prettierformatsource-options ), this function doesn’ t load plugins automatically. The `plugins` option is required because all the parsers included in the Prettier package come as plugins (for reasons of file size). These plugins are files in [https://unpkg.com/browse/prettier@%PRETTIER_VERSION%/plugins ](https://unpkg.com/browse/prettier@%PRETTIER_VERSION%/plugins ). Note that `estree` plugin should be loaded when printing JavaScript, TypeScript, Flow, or JSON.
2020-12-13 05:10:59 +08:00
2021-03-15 02:25:18 +02:00
You need to load the ones that you’ re going to use and pass them to `prettier.format` using the `plugins` option.
2020-12-13 05:10:59 +08:00
2021-03-15 02:25:18 +02:00
See below for examples.
2020-12-13 05:10:59 +08:00
2018-05-27 15:17:38 -03:00
## Usage
### Global
```html
2024-06-11 09:18:16 +08:00
< script src = "https://unpkg.com/prettier@%PRETTIER_VERSION%/standalone.js" ></ script >
< script src = "https://unpkg.com/prettier@%PRETTIER_VERSION%/plugins/graphql.js" ></ script >
2019-11-09 00:33:28 +08:00
< script >
2022-09-26 19:28:43 +08:00
( async () => {
const formatted = await prettier . format ( "type Query { hello: String }" , {
parser : "graphql" ,
plugins : prettierPlugins ,
});
})();
2018-05-27 15:17:38 -03:00
</ script >
```
2020-06-28 03:47:29 +02:00
Note that the [`unpkg` field ](https://unpkg.com/#examples ) in Prettier’ s `package.json` points to `standalone.js` , that’ s why `https://unpkg.com/prettier` can also be used instead of `https://unpkg.com/prettier/standalone.js` .
2020-05-25 09:51:54 +03:00
2019-07-05 02:41:07 -03:00
### ES Modules
2020-12-13 05:10:59 +08:00
```html
< script type = "module" >
2024-06-11 09:18:16 +08:00
import * as prettier from "https://unpkg.com/prettier@%PRETTIER_VERSION%/standalone.mjs" ;
import prettierPluginGraphql from "https://unpkg.com/prettier@%PRETTIER_VERSION%/plugins/graphql.mjs" ;
2019-07-05 02:41:07 -03:00
2022-09-26 19:28:43 +08:00
const formatted = await prettier . format ( "type Query { hello: String }" , {
2020-12-13 05:10:59 +08:00
parser : "graphql" ,
2023-07-17 09:34:44 +02:00
plugins : [ prettierPluginGraphql ],
2020-12-13 05:10:59 +08:00
});
</ script >
2019-07-05 02:41:07 -03:00
```
2018-05-27 15:17:38 -03:00
### AMD
```js
define ([
2024-06-11 09:18:16 +08:00
"https://unpkg.com/prettier@%PRETTIER_VERSION%/standalone.js" ,
"https://unpkg.com/prettier@%PRETTIER_VERSION%/plugins/graphql.js" ,
2022-09-26 19:28:43 +08:00
], async ( prettier , ... plugins ) => {
const formatted = await prettier . format ( "type Query { hello: String }" , {
2020-12-13 05:10:59 +08:00
parser : "graphql" ,
plugins ,
});
2018-05-27 15:17:38 -03:00
});
```
### CommonJS
```js
const prettier = require ( "prettier/standalone" );
2022-10-06 15:20:19 +08:00
const plugins = [ require ( "prettier/plugins/graphql" )];
2023-07-17 09:34:44 +02:00
2022-09-26 19:28:43 +08:00
( async () => {
const formatted = await prettier . format ( "type Query { hello: String }" , {
parser : "graphql" ,
plugins ,
});
})();
2018-05-27 15:17:38 -03:00
```
2020-06-28 03:47:29 +02:00
This syntax doesn’ t necessarily work in the browser, but it can be used when bundling the code with browserify, Rollup, webpack, or another bundler.
2018-05-27 15:17:38 -03:00
### Worker
```js
2024-06-11 09:18:16 +08:00
importScripts ( "https://unpkg.com/prettier@%PRETTIER_VERSION%/standalone.js" );
importScripts (
"https://unpkg.com/prettier@%PRETTIER_VERSION%/plugins/graphql.js" ,
);
2023-07-17 09:34:44 +02:00
2022-09-26 19:28:43 +08:00
( async () => {
const formatted = await prettier . format ( "type Query { hello: String }" , {
parser : "graphql" ,
plugins : prettierPlugins ,
});
})();
2018-05-27 15:17:38 -03:00
```
2021-03-15 02:25:18 +02:00
## Parser plugins for embedded code
If you want to format [embedded code ](options.md#embedded-language-formatting ), you need to load related plugins too. For example:
```html
< script type = "module" >
2024-06-11 09:18:16 +08:00
import * as prettier from "https://unpkg.com/prettier@%PRETTIER_VERSION%/standalone.mjs" ;
import prettierPluginBabel from "https://unpkg.com/prettier@%PRETTIER_VERSION%/plugins/babel.mjs" ;
import prettierPluginEstree from "https://unpkg.com/prettier@%PRETTIER_VERSION%/plugins/estree.mjs" ;
2021-03-15 02:25:18 +02:00
console . log (
2022-09-26 19:28:43 +08:00
await prettier . format ( "const html=/* HTML */ `<DIV> </DIV>`" , {
2021-03-15 02:25:18 +02:00
parser : "babel" ,
2023-07-17 09:34:44 +02:00
plugins : [ prettierPluginBabel , prettierPluginEstree ],
2021-03-15 02:25:18 +02:00
}),
);
// Output: const html = /* HTML */ `<DIV> </DIV>`;
</ script >
```
The HTML code embedded in JavaScript stays unformatted because the `html` parser hasn’ t been loaded. Correct usage:
```html
< script type = "module" >
2024-06-11 09:18:16 +08:00
import * as prettier from "https://unpkg.com/prettier@%PRETTIER_VERSION%/standalone.mjs" ;
import prettierPluginBabel from "https://unpkg.com/prettier@%PRETTIER_VERSION%/plugins/babel.mjs" ;
import prettierPluginEstree from "https://unpkg.com/prettier@%PRETTIER_VERSION%/plugins/estree.mjs" ;
import prettierPluginHtml from "https://unpkg.com/prettier@%PRETTIER_VERSION%/plugins/html.mjs" ;
2021-03-15 02:25:18 +02:00
console . log (
2022-09-26 19:28:43 +08:00
await prettier . format ( "const html=/* HTML */ `<DIV> </DIV>`" , {
2021-03-15 02:25:18 +02:00
parser : "babel" ,
2023-07-17 09:34:44 +02:00
plugins : [ prettierPluginBabel , prettierPluginEstree , prettierPluginHtml ],
2021-03-15 02:25:18 +02:00
}),
);
// Output: const html = /* HTML */ `<div></div>`;
</ script >
```