Configure ES Modules

We will use JavaScript ES Modules throughout this tutorial.

ES Modules allow you to use import and export syntax instead of CommonJS require() and module.exports.

Open package.json and add the type property.

The project configuration should look like this:

Module System

This tutorial consistently uses ES Modules. Do not mix require() and import syntax in the same project unless you intentionally configure interoperability.

package.json
{
  "name": "socket-io-chat",
  "version": "1.0.0",
  "description": "Real-time chat application using Node.js and Socket.IO",
  "type": "module",
  "scripts": {
    "start": "node src/server.js",
    "dev": "node --watch src/server.js"
  }
}

The type property tells Node.js to interpret .js files as ES Modules. The start script runs the application, while the dev script restarts the application when watched files change.

Key Takeaways
  • The type module setting enables ES Modules.

  • npm scripts simplify application execution.

  • Node.js watch mode is useful during development.