As a Node engineer, I am happy with common.js (cjs) and find it has a nice simplicity. That said EcmaScript Modules (ESM) are the way of the future.

The Fast and Easy ESM for NodeJs

Sometimes we just need a clean, fast, and easy solution. If you would like to learn more about the history and why ESM is really a better option than cjs scroll down. Now, let's get into the code!

After running your npm init command, in your package.json file, add in the main object (directly under "main" is a great place, but not required):

  "type": "module",

If you are a React developer, then ESM is common and part of your daily coding life. If not, how you import and export are slightly different from the traditional way of doing it in NodeJs.

// modules.js example

// export named function that an argument
export const nodeSays = (name) => `Hello ${name} from Node!`

// export named new Date method
export const time = new Date().toLocaleTimeString()

// export named array of objects
export const names = [
  {
    name: 'Node',
    serverSide: true,
  },
  {
    name: 'JavaScript',
    serverSide: false,
  },
]

// export default object
export default { lunch: 'ribs and slaw', dinner: 'steak and salad' }

Now to import in ESM all the exports above,

// index.js example

// meals is the default export and is not in {}
// named exports are in {}
import meals, { nodeSays, time, names } from './modules.js'

console.log(nodeSays('zac'), time)
console.log(meals)
console.log(names)

// output
Hello zac from Node! 1:22:43 PM
{ lunch: 'ribs and slaw', dinner: 'steak and salad' }
[
  { name: 'Node', serverSide: true },
  { name: 'JavaScript', serverSide: false }
]

What Exactly is a Javascript Module (ESM)?

TLDR;

If you're a web developer, then you know that Javascript is an essential tool for building dynamic and interactive websites. But what you may not know is that Javascript modules can help you to better organize your code and make it more reusable. In this article, we'll look at what Javascript modules are and how they can improve your workflow.

So what exactly is a Javascript module? Essentially, it's a way of packaging up your code so that it can be reused in other projects. Modules can contain variables, functions, and classes, and they can be imported into other Javascript files. This makes it easy to modularize your code and avoid duplication. It also makes it easier to share code between different projects.

There are several benefits to using Javascript modules.

  • First, they help to keep your code organized. By putting related code into separate modules, you can avoid having a giant mess of code in one file. This makes it easier to find what you're looking for and makes your code more maintainable in the long run.
  • Second, modules make your code more reusable. If you have a piece of code that you want to use in multiple projects, you can simply import the module into each project instead of copying and pasting the code each time.
  • Finally, modules can make your code more reliable. If one module contains a bug, you can simply update that module without needing to worry about breaking other parts of your codebase.

If you're interested in learning more about Javascript modules, then there are plenty of resources available online. Motivated learners can find tons of tutorials and articles that will teach them everything they need to know about using modules in their Javascript projects. So if you're looking to take your web development skills to the next level, then investigate Javascript modules today!

What are the Differences in Javascript and Node.js

Javascript is a programming language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else. (Okay, not everything. But it is pretty versatile.) In order to use Javascript on a web page you need two things: an HTML document in which to write your Javascript code, and a text editor in which to write said code.

Node.js is a Javascript runtime environment that includes everything you need to execute a program written in Javascript. It comprises an interpreter and a library. The interpreter is used to interpret Javascript code so that it can be executed. The library provides various Javascript objects and functions that can be used by the Javascript code.

Node.js basically allows you to run Javascript code outside of a browser. This is useful for many things such as creating command line tools, running Javascript code on a server (like for express.js or socket.io), or even just running simple Javascript scripts (like those used for data analysis or automation).

So why would you want to use Node.js? Well, Javascript is a great language for many things, but one thing it has always been lacking is the ability to run Javascript code outside of a web browser. Node.js solves this problem by providing an environment in which Javascript can be executed without a web browser. This opens up Javascript to a world of new possibilities!

If you're interested in learning more about Node.js, especially for backend development, I recommend CodeDamn. They have great content, playgrounds, and even a free web development bootcamp!