How to import npm package with "exports" field in package.json

Today I installed firebase-admin package and followed the document:

import { initializeApp } from 'firebase-admin/app';

but I got the error:

Unable to resolve some modules:
  "firebase-admin/app"
...
If you notice problems related to these missing modules, consider running:
  meteor npm install --save firebase-admin

The inside of firebase-admin/package.json has this exports block:

"exports": {
    ".": "./lib/index.js",
    "./app": {
      "require": "./lib/app/index.js",
      "import": "./lib/esm/app/index.js"
    },
    "./app-check": {
      "require": "./lib/app-check/index.js",
      "import": "./lib/esm/app-check/index.js"
    },
...

Does anyone know how to work with npm package like this?
Thank you.

1 Like

Ran into the same situation today with 1. unist-util-visit-parents. Uses a different export if it’s node. Meteor complains the module doesn’t exist.

Anyone know of a fix? I haven’t upgraded this project to 3.0 yet, but not sure if that’s resolved in 3.0?

1 Like

AFAIK the problem is on line 31 "type": "module"
Your Meteor package.json is probably set to “commonjs” so you cannot import ESM modules.

Also discussed here: ESM packages support · Issue #11631 · meteor/meteor · GitHub
I don’t know what is the situation in Meteor 3.
It seems that I cannot tag anyone in this thread but I thing the Meteor Team would know the status of ESMs in Meteor 3.

The original problem withe Firebase I think was related to a wrong import. That library is server side only and perhaps that user was trying to import it on the client (browser). That is the central tech library inside the Push package I use.

1 Like

This code works for me:

  import firebase from "firebase-admin";

  app = firebase.initializeApp({
    credential: firebase.credential.cert({
      projectId: project_id,
      clientEmail: client_email,
      privateKey: private_key,
    }),
  });

  export default app;
2 Likes