Skip to content

JSX in Node

wvbe edited this page Jan 22, 2023 · 2 revisions

Run the following commands:

npm install docxml
npm install -D @babel/cli @babel/core @babel/plugin-syntax-jsx @babel/plugin-transform-react-jsx

Create file .babelrc:

{
	"plugins": [
		"@babel/plugin-syntax-jsx",
		["@babel/plugin-transform-react-jsx", { "pragma": "jsx" }]
	]
}

Create a JavaScript file using docxml, call it index.js or whatever:

const { default: Docx, jsx, Paragraph } = require('docxml');

Docx.fromJsx(
	<Paragraph>Hello world!</Paragraph>,
)
	.toFile('out.docx')
	.then(() => console.log('Done'));

And then either build (and run):

npx babel index.js --out-dir lib
node lib/index.js

Or run directly:

npx @babel/node index.js

You will now have a file out.docx with the content per your index.js.

Clone this wiki locally