Skip to content

Commit f9e8155

Browse files
committed
Convert in-repo demo scripts to .cjs for type:module
The rosocket and web/javascript demos run straight from a clone (no npm install) and use CommonJS (require/__dirname), which breaks under the package's "type":"module". Rename them to .cjs and unwrap the now-ESM default export when requiring index.js: - demo/rosocket/server.js -> server.cjs (require('../../index.js').default) - demo/web/javascript/runtime.js -> runtime.cjs (.default unwrap) - demo/web/javascript/static.js -> static.cjs (builtins/__dirname only) Update run-command and prose references in the demo READMEs, index.html, and the web/typescript server.ts comment. The electron and typescript demos are standalone projects that consume the published dual package via its exports map (require + import both supported) and need no changes.
1 parent 685a42e commit f9e8155

7 files changed

Lines changed: 28 additions & 28 deletions

File tree

demo/rosocket/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ library required.
1111
- Subscribe to and publish on `/chatter` (`std_msgs/msg/String`).
1212
- Call `/add_two_ints` (`example_interfaces/srv/AddTwoInts`) — the
1313
service implementation lives in
14-
[`example/services/service/service-example.js`](../../example/services/service/service-example.js)
14+
[`example/services/service/service-example.cjs`](../../example/services/service/service-example.cjs)
1515
and is launched in a second terminal.
1616

1717
## Layout
1818

19-
- `server.js``rclnodejs` node + `startRosocket` bridge only.
19+
- `server.cjs``rclnodejs` node + `startRosocket` bridge only.
2020
- `index.html` — single-file browser client using only built-in
2121
`WebSocket` and `JSON`.
2222

@@ -27,12 +27,12 @@ library required.
2727
source /opt/ros/$ROS_DISTRO/setup.bash
2828

2929
# 2. Terminal A — start the WebSocket gateway
30-
node demo/rosocket/server.js
30+
node demo/rosocket/server.cjs
3131
# [rosocket-demo] listening on ws://localhost:9000 (bind=0.0.0.0)
3232

3333
# 3. Terminal B — start the AddTwoInts service so the browser has
3434
# something to call
35-
node example/services/service/service-example.js
35+
node example/services/service/service-example.cjs
3636
```
3737

3838
The server binds to `0.0.0.0:9000` so it is reachable from any host
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
// ws://<host>:9000/service/add_two_ints example_interfaces/srv/AddTwoInts
1818
//
1919
// Run inside WSL (where ROS 2 is sourced):
20-
// node demo/rosocket/server.js
20+
// node demo/rosocket/server.cjs
2121
//
2222
// To exercise the service, start the existing AddTwoInts example in a
2323
// second terminal (it implements `/add_two_ints`):
24-
// node example/services/service/service-example.js
24+
// node example/services/service/service-example.cjs
2525
//
2626
// Then open demo/rosocket/index.html on the Windows host browser. WSL2
2727
// forwards localhost so `ws://localhost:9000` works as-is. See README.md
2828
// for fallback instructions.
2929

30-
const rclnodejs = require('../../index.js');
30+
const rclnodejs = require('../../index.js').default;
3131
const { startRosocket } = require('../../rosocket');
3232

3333
const PORT = Number(process.env.PORT) || 9000;

demo/web/javascript/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ cd demo/web/javascript
1414

1515
```bash
1616
source /opt/ros/<distro>/setup.bash
17-
node runtime.js
17+
node runtime.cjs
1818
# rclnodejs/web : ws://localhost:9000/capability
1919
# also http://localhost:9001/capability (call/publish, curl-able)
2020
```
2121

22-
`runtime.js` exposes a tiny `/add_two_ints` service + 1 Hz
22+
`runtime.cjs` exposes a tiny `/add_two_ints` service + 1 Hz
2323
`/web_demo_tick` publisher so every panel has live data.
2424

2525
**Shell 2 — static-file server (hosts `index.html` + maps `/sdk/*` to
2626
the in-repo [`web/`](../../../web/) folder so the page can `import`
2727
the SDK from a plain URL):**
2828

2929
```bash
30-
node static.js
30+
node static.cjs
3131
# Static files : http://localhost:8080/
3232
```
3333

@@ -67,18 +67,18 @@ curl -sS -X POST http://localhost:9001/capability/call/add_two_ints \
6767

6868
Subscribe stays on WebSocket.
6969

70-
## Without the bundled `runtime.js`
70+
## Without the bundled `runtime.cjs`
7171

72-
`runtime.js` bundles the rclnodejs/web runtime and the demo's sample
72+
`runtime.cjs` bundles the rclnodejs/web runtime and the demo's sample
7373
ROS 2 nodes (the `/add_two_ints` service + the `/web_demo_tick`
7474
publisher) into one process so the demo runs out of the box. In a
7575
real project you already have those ROS 2 nodes running elsewhere,
76-
so you only need the runtime. **Replace shell 1's `node runtime.js`
77-
with the CLI** — shell 2 (`node static.js`) and the browser code are
76+
so you only need the runtime. **Replace shell 1's `node runtime.cjs`
77+
with the CLI** — shell 2 (`node static.cjs`) and the browser code are
7878
unchanged:
7979

8080
```bash
81-
# shell 1 (instead of `node runtime.js`); the `-p rclnodejs` tells npx
81+
# shell 1 (instead of `node runtime.cjs`); the `-p rclnodejs` tells npx
8282
# the `rclnodejs-web` binary lives inside the `rclnodejs` package:
8383
npx -p rclnodejs rclnodejs-web web.json
8484

demo/web/javascript/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22
<!--
33
rclnodejs/web demo. Open via http://localhost:8080/ once both
4-
`node runtime.js` (rclnodejs/web runtime) and `node static.js`
4+
`node runtime.cjs` (rclnodejs/web runtime) and `node static.cjs`
55
(page server) are running — see README.md.
66
-->
77
<html lang="en">
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// rclnodejs/web demo — runtime side (rclnodejs/web runtime + the demo's
10-
// ROS 2 nodes; named `runtime.js` to avoid being confused with the
11-
// page-side `static.js`).
10+
// ROS 2 nodes; named `runtime.cjs` to avoid being confused with the
11+
// page-side `static.cjs`).
1212
//
1313
// 1. Source ROS 2 (`source /opt/ros/<distro>/setup.bash`)
14-
// 2. From this folder run `node runtime.js`
15-
// 3. In another shell run `node static.js` to host
14+
// 2. From this folder run `node runtime.cjs`
15+
// 3. In another shell run `node static.cjs` to host
1616
// `index.html` on http://localhost:8080/ — same split as the
1717
// TypeScript demo's `tsx server.ts` + `vite`.
1818

1919
'use strict';
2020

21-
const rclnodejs = require('../../../index.js');
21+
const rclnodejs = require('../../../index.js').default;
2222
// In a downstream project this is the public, supported import:
2323
// const { createRuntime, WebSocketTransport, HttpTransport } =
2424
// require('rclnodejs/web/server');
@@ -135,7 +135,7 @@ async function main() {
135135
console.log(formatCapabilities(caps));
136136
console.log();
137137
console.log(
138-
'Static page: run `node static.js` in another shell, then open http://localhost:8080/'
138+
'Static page: run `node static.cjs` in another shell, then open http://localhost:8080/'
139139
);
140140

141141
// ---- Graceful shutdown ----------------------------------------------
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
//
77
// http://www.apache.org/licenses/LICENSE-2.0
88
//
9-
// rclnodejs/web demo — static-file server (page side; named `static.js`
10-
// to avoid being confused with the runtime-side `runtime.js`).
9+
// rclnodejs/web demo — static-file server (page side; named `static.cjs`
10+
// to avoid being confused with the runtime-side `runtime.cjs`).
1111
//
1212
// Serves index.html on port 8080 and maps `/sdk/*` to the in-repo
1313
// `web/` folder so the page can `import { connect } from '/sdk/index.js'`
1414
// without bundling. In a downstream project you'd `npm install rclnodejs`
1515
// and `import { connect } from 'rclnodejs/web'` instead.
1616
//
17-
// Pair with `node runtime.js` (the rclnodejs/web runtime + the demo's
17+
// Pair with `node runtime.cjs` (the rclnodejs/web runtime + the demo's
1818
// ROS 2 nodes) in another shell — the same split as the TypeScript
1919
// demo's `tsx server.ts` + `vite`. Production deployments use nginx,
2020
// a CDN, or any other static host.

demo/web/typescript/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// TypeScript demo server. Run with `npm run server` (which uses tsx) or
10-
// `npx tsx server.ts`. Behaviour matches demo/web/javascript/runtime.js
10+
// `npx tsx server.ts`. Behaviour matches demo/web/javascript/runtime.cjs
1111
// — same runtime + same demo nodes — except this side is written in
1212
// TypeScript so the typed SDK story is visible end to end. The static
1313
// page server is Vite (`npm run dev`), parallel to the JS demo's
14-
// separate `node static.js`.
14+
// separate `node static.cjs`.
1515

1616
// rclnodejs is a CommonJS module without first-class ESM types; using
1717
// require keeps the server independent of how a downstream project
@@ -30,7 +30,7 @@ const RUNTIME_PORT = Number(process.env.RUNTIME_PORT || 9000);
3030
const HTTP_PORT = Number(process.env.HTTP_PORT || 9001);
3131

3232
// Render the registry as a small human-readable table — see the matching
33-
// helper in demo/web/javascript/runtime.js.
33+
// helper in demo/web/javascript/runtime.cjs.
3434
function formatCapabilities(
3535
caps: Record<'call' | 'publish' | 'subscribe', Record<string, string>>
3636
): string {

0 commit comments

Comments
 (0)