| 1 | # OpenCC for Node.js |
| 2 | |
| 3 | OpenCC provides high quality conversion between Simplified Chinese, |
| 4 | Traditional Chinese, and regional variants. This package contains the official |
| 5 | Node.js bindings for the OpenCC native library. |
| 6 | |
| 7 | ## Installation |
| 8 | |
| 9 | ```bash |
| 10 | npm install opencc |
| 11 | ``` |
| 12 | |
| 13 | The package requires Node.js `>=20.17`. |
| 14 | |
| 15 | OpenCC ships native bindings. When a matching prebuilt binary is available, |
| 16 | installation uses it automatically. Otherwise npm builds the native addon from |
| 17 | source, which requires a working C++ toolchain and Python supported by |
| 18 | `node-gyp`. |
| 19 | |
| 20 | ## Basic Usage |
| 21 | |
| 22 | ES modules: |
| 23 | |
| 24 | ```js |
| 25 | import OpenCC from 'opencc'; |
| 26 | |
| 27 | const converter = new OpenCC('s2t.json'); |
| 28 | console.log(converter.convertSync('汉字')); |
| 29 | ``` |
| 30 | |
| 31 | Promise API: |
| 32 | |
| 33 | ```js |
| 34 | import OpenCC from 'opencc'; |
| 35 | |
| 36 | const converter = new OpenCC('s2tw.json'); |
| 37 | console.log(await converter.convertPromise('鼠标和软件')); |
| 38 | ``` |
| 39 | |
| 40 | Named import: |
| 41 | |
| 42 | ```js |
| 43 | import { OpenCC } from 'opencc'; |
| 44 | |
| 45 | const converter = new OpenCC('t2s.json'); |
| 46 | console.log(converter.convertSync('漢字')); |
| 47 | ``` |
| 48 | |
| 49 | CommonJS is still supported for older projects: |
| 50 | |
| 51 | ```js |
| 52 | const { OpenCC } = require('opencc'); |
| 53 | |
| 54 | const converter = new OpenCC('s2t.json'); |
| 55 | console.log(converter.convertSync('汉字')); |
| 56 | ``` |
| 57 | |
| 58 | Callback API: |
| 59 | |
| 60 | ```js |
| 61 | import OpenCC from 'opencc'; |
| 62 | |
| 63 | const converter = new OpenCC('t2s.json'); |
| 64 | |
| 65 | converter.convert('漢字', (err, text) => { |
| 66 | if (err) throw err; |
| 67 | console.log(text); |
| 68 | }); |
| 69 | ``` |
| 70 | |
| 71 | TypeScript type declarations are included. |
| 72 | |
| 73 | ## API |
| 74 | |
| 75 | ### `new OpenCC(config?, options?)` |
| 76 | |
| 77 | Creates a converter. If `config` is omitted, OpenCC uses `s2t.json`. |
| 78 | |
| 79 | `config` can be one of the built-in configuration file names, such as |
| 80 | `s2t.json`, an absolute path to a custom OpenCC configuration file, or an |
| 81 | OpenCC configuration object. |
| 82 | |
| 83 | `options.includeTofuRiskDictionaries` controls whether dictionaries marked as |
| 84 | possibly outputting tofu are loaded. Here, tofu means Chinese characters that |
| 85 | cannot be displayed and may render as missing-glyph boxes. It defaults to |
| 86 | `true` for JavaScript API compatibility. |
| 87 | |
| 88 | `options.configDirectory` sets the base directory used to resolve relative |
| 89 | dictionary paths in inline configuration objects. It defaults to the package's |
| 90 | bundled OpenCC assets directory. |
| 91 | |
| 92 | ### `OpenCC.fromConfig(config, options?)` |
| 93 | |
| 94 | Creates a converter from an OpenCC configuration object. This is equivalent to |
| 95 | passing the object to `new OpenCC(config, options)`. |
| 96 | |
| 97 | ### `converter.convertSync(input)` |
| 98 | |
| 99 | Converts text synchronously and returns the converted string. |
| 100 | |
| 101 | ### `converter.convert(input, callback)` |
| 102 | |
| 103 | Converts text asynchronously. |
| 104 | |
| 105 | The callback receives `(err, convertedText)`. |
| 106 | |
| 107 | ### `converter.convertPromise(input)` |
| 108 | |
| 109 | Converts text asynchronously and returns a `Promise<string>`. |
| 110 | |
| 111 | ### `OpenCC.version` |
| 112 | |
| 113 | The version string of the bundled OpenCC native library. |
| 114 | |
| 115 | ### `OpenCC.generateDict(inputFileName, outputFileName, formatFrom, formatTo)` |
| 116 | |
| 117 | Generates a dictionary file from another dictionary format. |
| 118 | |
| 119 | For example, convert a text dictionary to OpenCC's binary `ocd2` format: |
| 120 | |
| 121 | ```js |
| 122 | import { OpenCC } from 'opencc'; |
| 123 | |
| 124 | OpenCC.generateDict('my-dict.txt', 'my-dict.ocd2', 'text', 'ocd2'); |
| 125 | ``` |
| 126 | |
| 127 | ## Command Line |
| 128 | |
| 129 | Install OpenCC globally to use the npm command line tool: |
| 130 | |
| 131 | ```bash |
| 132 | npm install -g opencc |
| 133 | ``` |
| 134 | |
| 135 | The package installs an `opencc` command: |
| 136 | |
| 137 | ```bash |
| 138 | opencc -c s2t.json -i input.txt -o output.txt |
| 139 | ``` |
| 140 | |
| 141 | It can also read from stdin and write to stdout: |
| 142 | |
| 143 | ```bash |
| 144 | echo '汉字' | opencc -c s2t.json |
| 145 | ``` |
| 146 | |
| 147 | Options: |
| 148 | |
| 149 | ```text |
| 150 | -c, --config <file> Configuration file. Defaults to s2t.json. |
| 151 | -i, --input <file> Read original text from <file>. Defaults to stdin. |
| 152 | -o, --output <file> Write converted text to <file>. Defaults to stdout. |
| 153 | --include-tofu-risk-dictionaries |
| 154 | Include dictionaries that may output tofu: Chinese |
| 155 | characters rendered as missing-glyph boxes. |
| 156 | -v, --version Print OpenCC version. |
| 157 | -h, --help Print help. |
| 158 | ``` |
| 159 | |
| 160 | The npm CLI skips dictionaries marked as possibly outputting tofu by default. |
| 161 | Here, tofu means Chinese characters that cannot be displayed and may render as |
| 162 | missing-glyph boxes. Use `--include-tofu-risk-dictionaries` to load them. The |
| 163 | npm CLI supports conversion only. Use the native OpenCC command line tool for |
| 164 | inspection, segmentation output, custom resource search paths, or other |
| 165 | advanced CLI features. |
| 166 | |
| 167 | ## Built-in Configurations |
| 168 | |
| 169 | | Configuration | Conversion | |
| 170 | | --- | --- | |
| 171 | | `s2t.json` | Simplified Chinese to Traditional Chinese (OpenCC Standard) / 簡體到 OpenCC 標準繁體 | |
| 172 | | `t2s.json` | Traditional Chinese (OpenCC Standard) to Simplified Chinese / OpenCC 標準繁體到簡體 | |
| 173 | | `s2tw.json` | Simplified Chinese to Traditional Chinese (Taiwan Standard) / 簡體到台灣正體 | |
| 174 | | `tw2s.json` | Traditional Chinese (Taiwan Standard) to Simplified Chinese / 台灣正體到簡體 | |
| 175 | | `s2hk.json` | Simplified Chinese to Traditional Chinese (Hong Kong variant) / 簡體到香港繁體 | |
| 176 | | `hk2s.json` | Traditional Chinese (Hong Kong variant) to Simplified Chinese / 香港繁體到簡體 | |
| 177 | | `s2twp.json` | Simplified Chinese to Traditional Chinese (Taiwan Standard, with Taiwan Phrases) / 簡體到台灣正體(含台灣常用詞彙) | |
| 178 | | `tw2sp.json` | Traditional Chinese (Taiwan Standard) to Simplified Chinese (Mainland China Phrases) / 台灣正體到簡體(含中國大陸常用詞彙) | |
| 179 | | `t2tw.json` | Traditional Chinese (OpenCC Standard) to Traditional Chinese (Taiwan Standard) / OpenCC 標準繁體到台灣正體 | |
| 180 | | `tw2t.json` | Traditional Chinese (Taiwan Standard) to Traditional Chinese (OpenCC Standard) / 台灣正體到 OpenCC 標準繁體 | |
| 181 | | `t2hk.json` | Traditional Chinese (OpenCC Standard) to Traditional Chinese (Hong Kong variant) / OpenCC 標準繁體到香港繁體 | |
| 182 | | `hk2t.json` | Traditional Chinese (Hong Kong variant) to Traditional Chinese (OpenCC Standard) / 香港繁體到 OpenCC 標準繁體 | |
| 183 | | `t2jp.json` | Traditional Chinese Characters (Kyūjitai) to New Japanese Kanji (Shinjitai) / OpenCC 標準繁體(日文舊字體)到日文新字體 | |
| 184 | | `jp2t.json` | New Japanese Kanji (Shinjitai) to Traditional Chinese Characters (Kyūjitai) / 日文新字體到 OpenCC 標準繁體(日文舊字體) | |
| 185 | |
| 186 | ## Custom Configurations |
| 187 | |
| 188 | Pass a path to a custom OpenCC configuration JSON file: |
| 189 | |
| 190 | ```js |
| 191 | import OpenCC from 'opencc'; |
| 192 | |
| 193 | const converter = new OpenCC('/absolute/path/to/custom.json'); |
| 194 | console.log(converter.convertSync('汉字')); |
| 195 | ``` |
| 196 | |
| 197 | Relative config paths are resolved from the current working directory by the |
| 198 | CLI. In the JavaScript API, relative config names are resolved from the package |
| 199 | assets directory, so custom configs should be passed as absolute paths. |
| 200 | |
| 201 | You can also pass an OpenCC configuration object directly: |
| 202 | |
| 203 | ```js |
| 204 | import OpenCC from 'opencc'; |
| 205 | |
| 206 | const converter = OpenCC.fromConfig({ |
| 207 | name: 'Custom Inline Config', |
| 208 | segmentation: { |
| 209 | type: 'mmseg', |
| 210 | dict: { type: 'inline', entries: { '鼠标': '鼠标' } }, |
| 211 | }, |
| 212 | conversion_chain: [{ |
| 213 | dict: { type: 'inline', entries: { '鼠标': '滑鼠' } }, |
| 214 | }], |
| 215 | }); |
| 216 | |
| 217 | console.log(converter.convertSync('鼠标坏了')); |
| 218 | ``` |
| 219 | |
| 220 | When an inline configuration references relative dictionary files, pass |
| 221 | `configDirectory`: |
| 222 | |
| 223 | ```js |
| 224 | const converter = OpenCC.fromConfig(config, { |
| 225 | configDirectory: '/absolute/path/to/opencc-resources', |
| 226 | }); |
| 227 | ``` |
| 228 | |
| 229 | Custom dictionaries can be generated with `OpenCC.generateDict()` or with the |
| 230 | native `opencc_dict` tool from the full OpenCC distribution. |
| 231 | |
| 232 | ## Optional Jieba Configurations |
| 233 | |
| 234 | Some OpenCC configurations use jieba segmentation. Install the optional |
| 235 | `opencc-jieba` package to make jieba-backed configs available: |
| 236 | |
| 237 | ```bash |
| 238 | npm install opencc opencc-jieba |
| 239 | ``` |
| 240 | |
| 241 | When `opencc-jieba` is installed, the JavaScript API and npm `opencc` CLI can |
| 242 | load its configs automatically: |
| 243 | |
| 244 | Available plugin-backed configs include `s2t_jieba.json`, |
| 245 | `s2tw_jieba.json`, `s2hk_jieba.json`, `s2twp_jieba.json`, and |
| 246 | `tw2sp_jieba.json`. |
| 247 | |
| 248 | ```js |
| 249 | import OpenCC from 'opencc'; |
| 250 | |
| 251 | const converter = new OpenCC('s2twp_jieba'); |
| 252 | console.log(converter.convertSync('软件鼠标')); |
| 253 | ``` |
| 254 | |
| 255 | The npm CLI also accepts the same config names for text conversion: |
| 256 | |
| 257 | ```bash |
| 258 | opencc -c s2twp_jieba |
| 259 | ``` |
| 260 | |
| 261 | Use the native OpenCC CLI for diagnostic modes such as `--inspect` and |
| 262 | `--segmentation`. |
| 263 | |
| 264 | ## Related npm Packages |
| 265 | |
| 266 | The [`opencc`](https://www.npmjs.com/package/opencc) npm package is the |
| 267 | Node.js native binding for the official OpenCC C++ project. It is intended for |
| 268 | Node.js, depends on native or prebuilt binaries, and follows the official |
| 269 | OpenCC engine. It can use extended segmentation algorithms such as Jieba when |
| 270 | supported by the official OpenCC configuration and runtime. |
| 271 | |
| 272 | The [`opencc-js`](https://www.npmjs.com/package/opencc-js) npm package is a |
| 273 | pure JavaScript implementation for browsers and Node.js. It bundles dictionary |
| 274 | data generated from `opencc-data`, so it does not require native binaries and |
| 275 | does not fetch dictionary text files at runtime. |
| 276 | |
| 277 | `opencc-js` has aligned its conversion flow with the official OpenCC |
| 278 | implementation, including phrase segmentation for built-in converters, and is |
| 279 | tested against upstream OpenCC test cases and golden outputs. It still should |
| 280 | not be treated as guaranteed to produce 100% identical results for every |
| 281 | possible input. |
| 282 | |
| 283 | `opencc-js` currently supports the built-in OpenCC mmseg-style segmentation |
| 284 | used by its bundled converters, but it does not support extended segmentation |
| 285 | algorithms such as Jieba. |
| 286 | |
| 287 | The [`opencc-wasm`](https://www.npmjs.com/package/opencc-wasm) npm package is |
| 288 | another browser-capable implementation. It uses WebAssembly, keeps its |
| 289 | configuration and conversion logic aligned with the official `opencc` package, |
| 290 | and can support Jieba segmentation through the official OpenCC runtime. |
| 291 | |
| 292 | ## Development |
| 293 | |
| 294 | From the repository root: |
| 295 | |
| 296 | ```bash |
| 297 | npm install |
| 298 | npm test |
| 299 | ``` |
| 300 | |
| 301 | The `OpenCCConfig` TypeScript declarations in `opencc.d.ts`, |
| 302 | `opencc.d.cts`, and `opencc.d.mts` are generated from |
| 303 | `data/config/opencc_config.schema.json`. Regenerate them after changing the |
| 304 | schema: |
| 305 | |
| 306 | ```bash |
| 307 | npm run generate:node-config-types |
| 308 | ``` |
| 309 | |
| 310 | To build prebuilt native addons for publishing, see |
| 311 | [`node/PUBLISHING.md`](./PUBLISHING.md). |