Embracing the Future: Meteor 3 with Node v20 and Express
Tags: meteor, express, javascript, meteor3
Published: 2024-05-10 20:59:23

Meteor, a powerful full-stack JavaScript platform, has undergone a significant transformation with its latest update, Meteor 3. _**This upgrade not only modernizes the framework but also enhances its capabilities**_ by integrating the latest Node.js features, including version 20 LTS, and moving from Fibers to native `async/await` syntax. Let’s dive into how these changes benefit the Meteor ecosystem.

## Table of contents:
- [Launch Week - Day 1](#launch-week-day-1)
- [The Shift from Fibers to Async/Await](#the-shift-from-fibers-to-asyncawait)
- [Meteor 3 and Node v20: A Powerful Combination](#meteor-3-and-node-v20-a-powerful-combination)
   * [Top-Level Await](#toplevel-await)
   * [Stable Timers Promises API](#stable-timers-promises-api)
- [Integrating Express into Meteor](#integrating-express-into-meteor)
- [Preparing for the Official Release: Understanding Meteor 3's Current State](#preparing-for-the-official-release-understanding-meteor-3s-current-state)
   * [Recent Updates and How to Use Meteor 3 Now](#recent-updates-and-how-to-use-meteor-3-now)
   * [Community Feedback and Next Steps](#community-feedback-and-next-steps)
- [Conclusion](#conclusion)

## Launch Week - Day 1

We're excited to launch the new Meteor 3.0 Docs and Migration Guide today!

Check out the new V3 Docs at (https://v3-docs.meteor.com/), which focuses solely on Meteor 3.0 and steps away from backward compatibility. While much remains familiar from Meteor 2.x, there are key differences, and these new docs are here for 3.0 only. If you’re still on version 2, your go-to resource remains https://docs.meteor.com. As we roll out the official version 3, it will take the lead on our website, transitioning the current documentation to v2-docs.meteor.com.

The Migration Guide (https://v3-migration-docs.meteor.com/) is here to help those of you on Meteor 2.x seamlessly transition to 3.0. It highlights the changes, answers your frequent questions, and guides you through the transition, including modified and removed functions. Plus, we’ve included helpful links to community resources!

We’re super excited to see your projects take advantage of Meteor 3.0! Go ahead, explore the new resources, and let us know your thoughts. Your feedback is crucial - share your suggestions, report issues, and contribute. Let's make Meteor 3.0 the best version yet!

And while we're here, let's explore Meteor 3.0 more.

## The Shift from Fibers to Async/Await

For years, Meteor relied on Fibers to simplify asynchronous code, making it look and behave synchronously. This approach was instrumental in avoiding callback hell and enhancing code readability. However, Fibers became a limiting factor as it was not compatible with newer versions of Node.js, particularly from version 16 onwards.

Removing Fibers was a critical step in bringing Meteor up to date with the most current Node.js stack. This transition allows Meteor to leverage all the new features that Node v20 provides. Async/await, a feature now fully supported in Meteor 3, is a prime example.

Example of Async/Await in Meteor:


```js
Meteor.methods({
  async getUserData(userId) {
    try {
      const user = await UsersCollection.findOneAsync({ _id: userId });
      return user;
    } catch (error) {
      throw new Meteor.Error('user-fetch-failed', 'Could not retrieve user data');
    }
  }
});
```

This example demonstrates how you can fetch user data asynchronously. Using `async/await` ensures the code is clean and easy to read, and error handling is straightforward.

## Meteor 3 and Node v20: A Powerful Combination

Node v20 brings several enhancements that Meteor 3 utilizes to improve performance and developer experience. These include Top-Level Await and Timers Promises API!

### Top-Level Await

Allows using the await keyword outside of async functions within modules, simplifying the initialization of applications.

```javascript
// At the top level in your Meteor app's server-side code
const settings = await SettingsCollection.findOneAsync({ key: 'app-config' });
Meteor.startup(() => {
  configureApp(settings);
});
```
> This feature allows developers to perform asynchronous operations before the application fully starts, ensuring that necessary configurations are in place.

### Stable Timers Promises API

Provides a promise-based alternative to traditional timer functions like `setTimeout` and `setInterval`, enhancing the way delays and periodic tasks are handled in a non-blocking manner.

```js
import { setTimeout } from 'timers/promises';

Meteor.methods({
  async delayedGreeting(name) {
    // Wait for 2 seconds before continuing
    await setTimeout(2000);
    
    // Process after the delay
    return `Hello, ${name}!`;
  }
});

```

These are only two examples of how Meteor 3 takes advantage of the features Node 20 offers. Another great addition to Meteor is the integration with Express.

## Integrating Express into Meteor

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qd7bczrr0bztliqpw7t.gif)

The adoption of Express in Meteor 3 opens up new possibilities for developers, especially when building RESTful APIs or serving static files. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

Here is an example of using the `WebApp.handlers` as your express instance as you typically would:

```javascript
import { LinksCollection } from "/imports/api/links";
import { WebApp } from "meteor/webapp";

WebApp.handlers.get("/all-links", async (req, res) => {
  const links = await LinksCollection.find().fetchAsync();
  res.json(links);
});

// other code ...
```

In this example, express is used to define a simple API endpoint that returns data from a Meteor collection. This setup shows how seamlessly Express can integrate into the Meteor ecosystem, providing familiar tools for those accustomed to traditional Node.js development.

> You can read more about Meteor with Express [here](https://dev.to/meteor/meteor-v3-uses-express-under-the-hood-how-to-use-and-deploy-it-5c2n)!


## Preparing for the Official Release: Understanding Meteor 3's Current State

As we approach the official launch of Meteor 3, it's important to recognize that we're currently in our Release Candidate (RC) phase. This is a critical moment where we're very close to the full release, and we've already undergone several reviews to refine and enhance the framework. 

This is the current status of versions for Meteor 3:

**Latest version:** 3.0-rc.2
**Node.js version:** 20.12.2 LTS
**NPM version:** 10.5.0

### Recent Updates and How to Use Meteor 3 Now

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ej8z052umpe22t6xkn7e.gif)

Meteor 3 has undergone significant updates in its RC versions, focusing on fixing key issues to ensure stability and performance. You can check all the work we have done up to now in our [3.0 Milestone](https://github.com/meteor/meteor/milestone/101?closed=1) on GitHub or in the [Roadmap](https://forums.meteor.com/t/fibers-public-roadmap-and-meteor-3-0/59627) post we have in our forums where we update the community periodically about our progress.

If you're eager to start using Meteor 3 right away, you can install the current version using the following command:

```sh
npx meteor@rc
```

For those looking to upgrade from Meteor 2 to Meteor 3, the process is straightforward. You can update your project by running the following:

```sh
meteor update --release 3.0-rc.2
meteor reset  # resets local DB and project to a fresh state
```

But it'll be this simple only if you are already in the current 2.16 version and have prepared your app with the new async API.

If you are not in version 2.16 yet, make sure to follow the [migration guides](https://guide.meteor.com/2.14-migration) from your current version up to Meteor 2.16.

If you're already in version 2.16 but still haven't prepared your app, you can follow our [3.0 Migration Guide](https://v3-migration-docs.meteor.com/).

### Community Feedback and Next Steps

Your feedback is invaluable as we finalize Meteor 3 for its official release. We plan to issue new release candidates every two weeks, incorporating community input to refine and improve the framework. This iterative process is essential for ensuring that Meteor 3 meets our community's needs and expectations.

This means your feedback is extremely important. Create new apps, update existing ones, and provide us with your findings. This way it will be faster to find and solve edge-case issues and release the final version sooner.

## Conclusion

The transition to Meteor 3 with Node v20 and Express is a significant leap forward for the Meteor community. This upgrade ensures that Meteor remains a competitive and modern framework capable of handling the needs of today's web and mobile applications. By embracing these changes, developers can enjoy improved performance, enhanced scalability, and a more intuitive coding experience.

For a deeper dive into implementing these features and more, visit the official [Meteor documentation](https://v3-docs.meteor.com/) and our [Migration Guide](https://v3-migration-docs.meteor.com/) and explore the rich ecosystem that Meteor continues to offer. As always, you can be part of our wonderful community in our [Forums](https://forums.meteor.com/) and [Slack](meteor-community.slack.com).

We also can't finish this without thanking everybody who helped the Meteor team throughout this process. Thanks to everybody in our forums and Slack for feedback and suggestions, but also thanks to everybody who directly contributed with [PRs and reviews](https://github.com/meteor/meteor/pull/12359). This couldn't be done without you :heart:

[@denihs](https://github.com/denihs)
[@Grubba27](https://github.com/Grubba27)
[@nachocodoner](https://github.com/nachocodoner)
[@fredmaiaarantes](https://github.com/fredmaiaarantes)
[@vit0rr](https://github.com/vit0rr)
[@filipenevola](https://github.com/filipenevola)
[@zodern](https://github.com/zodern)
[@radekmie](https://github.com/radekmie)
[@StorytellerCZ](https://github.com/StorytellerCZ)
[@gunnartorfis](https://github.com/gunnartorfis)
[@xet7](https://github.com/xet7)
[@harryadel](https://github.com/harryadel)
[@CLAassistant](https://github.com/CLAassistant)
[@simonwebs](https://github.com/simonwebs)
[@TylerThompson](https://github.com/TylerThompson)
[@rodrigok](https://github.com/rodrigok)
[@zarvox](https://github.com/zarvox)
[@srsgores](https://github.com/srsgores)
[@tassoevan](https://github.com/tassoevan)
[@Torgen](https://github.com/Torgen)
[@MarcosSpessatto](https://github.com/MarcosSpessatto)
[@vincentcarpentier](https://github.com/vincentcarpentier)
[@ggazzo](https://github.com/ggazzo)
[@StevenMia](https://github.com/StevenMia)
[@acemtp](https://github.com/acemtp)