2018-12-25 08:20:35 +08:00
---
id : version-stable-browser
title : Browser
original_id : browser
---
2021-05-10 00:00:41 +03: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-20 13:44:18 +09:00
The standalone version comes as:
- ES modules: `esm/standalone.mjs` , starting in version 2.2
- UMD: `standalone.js` , starting in version 1.13
2018-12-25 08:20:35 +08:00
2020-07-01 08:44:21 +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 ).
2018-12-25 08:20:35 +08:00
### `prettier.format(code, options)`
2021-05-10 00:00:41 +03:00
Required options:
2018-12-25 08:20:35 +08:00
2021-05-10 00:00:41 +03: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-12-25 08:20:35 +08:00
2021-05-10 00:00:41 +03: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 named
2020-12-13 05:10:59 +08:00
2023-03-20 14:01:10 +08:00
- `parser-*.js` in <https://unpkg.com/browse/prettier@2 .8.5/> and
- `parser-*.mjs` in <https://unpkg.com/browse/prettier@2 .8.5/esm/>
2020-12-13 05:10:59 +08:00
2021-05-10 00:00:41 +03: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-05-10 00:00:41 +03:00
See below for examples.
2020-12-13 05:10:59 +08:00
2018-12-25 08:20:35 +08:00
## Usage
### Global
```html
2023-03-20 14:01:10 +08:00
< script src = "https://unpkg.com/prettier@2.8.5/standalone.js" ></ script >
< script src = "https://unpkg.com/prettier@2.8.5/parser-graphql.js" ></ script >
2019-11-09 01:09:19 +01:00
< script >
2020-12-13 05:10:59 +08:00
prettier . format ( "type Query { hello: String }" , {
2019-11-09 01:09:19 +01:00
parser : "graphql" ,
plugins : prettierPlugins ,
});
2018-12-25 08:20:35 +08:00
</ script >
```
2020-07-01 08:44:21 +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` .
2019-08-12 19:17:55 +02:00
### ES Modules
2020-12-13 05:10:59 +08:00
```html
< script type = "module" >
2023-03-20 14:01:10 +08:00
import prettier from "https://unpkg.com/prettier@2.8.5/esm/standalone.mjs" ;
import parserGraphql from "https://unpkg.com/prettier@2.8.5/esm/parser-graphql.mjs" ;
2019-08-12 19:17:55 +02:00
2020-12-13 05:10:59 +08:00
prettier . format ( "type Query { hello: String }" , {
parser : "graphql" ,
plugins : [ parserGraphql ],
});
</ script >
2019-08-12 19:17:55 +02:00
```
2018-12-25 08:20:35 +08:00
### AMD
```js
define ([
2023-03-20 14:01:10 +08:00
"https://unpkg.com/prettier@2.8.5/standalone.js" ,
"https://unpkg.com/prettier@2.8.5/parser-graphql.js" ,
2018-12-25 08:20:35 +08:00
], ( prettier , ... plugins ) => {
2020-12-13 05:10:59 +08:00
prettier . format ( "type Query { hello: String }" , {
parser : "graphql" ,
plugins ,
});
2018-12-25 08:20:35 +08:00
});
```
### CommonJS
```js
const prettier = require ( "prettier/standalone" );
const plugins = [ require ( "prettier/parser-graphql" )];
2020-12-13 05:10:59 +08:00
prettier . format ( "type Query { hello: String }" , {
parser : "graphql" ,
plugins ,
});
2018-12-25 08:20:35 +08:00
```
2020-07-01 08:44:21 +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-12-25 08:20:35 +08:00
### Worker
```js
2023-03-20 14:01:10 +08:00
importScripts ( "https://unpkg.com/prettier@2.8.5/standalone.js" );
importScripts ( "https://unpkg.com/prettier@2.8.5/parser-graphql.js" );
2020-12-13 05:10:59 +08:00
prettier . format ( "type Query { hello: String }" , {
parser : "graphql" ,
plugins : prettierPlugins ,
});
2018-12-25 08:20:35 +08:00
```
2021-05-10 00:00:41 +03: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" >
2023-03-20 14:01:10 +08:00
import prettier from "https://unpkg.com/prettier@2.8.5/esm/standalone.mjs" ;
import parserBabel from "https://unpkg.com/prettier@2.8.5/esm/parser-babel.mjs" ;
2021-05-10 00:00:41 +03:00
console . log (
prettier . format ( "const html=/* HTML */ `<DIV> </DIV>`" , {
parser : "babel" ,
plugins : [ parserBabel ],
})
);
// 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" >
2023-03-20 14:01:10 +08:00
import prettier from "https://unpkg.com/prettier@2.8.5/esm/standalone.mjs" ;
import parserBabel from "https://unpkg.com/prettier@2.8.5/esm/parser-babel.mjs" ;
import parserHtml from "https://unpkg.com/prettier@2.8.5/esm/parser-html.mjs" ;
2021-05-10 00:00:41 +03:00
console . log (
prettier . format ( "const html=/* HTML */ `<DIV> </DIV>`" , {
parser : "babel" ,
plugins : [ parserBabel , parserHtml ],
})
);
// Output: const html = /* HTML */ `<div></div>`;
</ script >
```