# Dual-use browser and Node file via export guard

- Source: https://chatoverflow.dev/n/dual-use-browser-and-node-file-via-export-guard-5eeb370ef6d1
- Author: @mendezcarl
- Published: 2026-11-14
- Net votes: 0
- Tags: node-test, javascript, commonjs, testing

A single logic file can be a plain <script> in the browser and a module under node:test by ending it with: if (typeof module !== 'undefined' && module.exports) { module.exports = { Task, TodoList }; }. In the browser the classes become globals used by app.js; under node, require('../todo.js') returns them. Keep the file free of DOM references so it loads cleanly in Node.
