Jacob Sherwood
Jacob Sherwood

Reputation: 311

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" - Firebase

Working through this Full Stack React & Firebase Tutorial and trying to use Firebase Authentication, to create a new user, I am getting the below error.

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /socialapp-functions/functions/node_modules/firebase/package.json
    at throwExportsNotFound (internal/modules/esm/resolve.js:299:9)
    at packageExportsResolve (internal/modules/esm/resolve.js:522:3)
    at resolveExports (internal/modules/cjs/loader.js:449:36)
    at Function.Module._findPath (internal/modules/cjs/loader.js:489:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:875:27)
    at Function.Module._load (internal/modules/cjs/loader.js:745:27)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (/socialapp-functions/functions/index.js:17:18)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
⚠  We were unable to load your functions code. (see above)

The tutorial details code like this:

// Signup route
app.post("/signup", (req, res) => {
  const newUser = {
    email: req.body.email,
    password: req.body.password,
    confirmPassword: req.body.confirmPassword,
    handle: req.body.handle,
  };

  firebase
    .auth()
    .createUserWithEmailAndPassword(newUser.email, newUser.password)
    .then((data) => {
      return res
        .status(201)
        .json({ message: `user ${data.user.uid} signed up successfully` });
    })
    .catch((err) => {
      console.error(err);
      return res.status(500).json({ error: err.code });
    });
});

Upvotes: 7

Views: 23873

Answers (5)

user10000527
user10000527

Reputation:

The way you do this using Firebase SDK changed with the new version 9.

Instead of

const firebase = require("firebase");

firebase
.auth()
.createUserWithEmailAndPassword(newUser.email, newUser.password)
.then((data) => {
  return res
    .status(201)
    .json({ message: `user ${data.user.uid} signed up successfully` });
})

Do this

const { getAuth, createUserWithEmailAndPassword } = require( "firebase/auth");

const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
.then((data) => {
  return res
    .status(201)
    .json({ message: `user ${data.user.uid} signed up successfully` });
})

Upvotes: 1

Deepak Singh
Deepak Singh

Reputation: 1145

In June 2022

In Node js

The permanent solution to all my problems was uninstalling the firebase v9+

Next i did:

npm uninstall firebase

then

npm i [email protected]

Hope this solves your issue too

Edit: Also u won't be requiring firebase-admin after this

Upvotes: 8

Asad Ali
Asad Ali

Reputation: 13

just install xhr2 in your node_modules, and use firebase-admin instead of firebase then try.

Upvotes: 0

Aziz
Aziz

Reputation: 55

With these versions it works!! Downgrade/upgrade these dependencies to match the following in <package.json>

"dependencies": {
"firebase": "^8.10.0",
"firebase-admin": "^8.13.0",
"firebase-functions": "^3.14.1"
},

Upvotes: 3

Jacob Sherwood
Jacob Sherwood

Reputation: 311

Per the question and answer here I think the issue is the tutorial has code for Firebase@8 when I'm running Firebase@9.

Referencing the Firebase Admin SDK docs to Manage Users, I am using

const admin = require("firebase-admin");

Instead of

const firebase = require("firebase");

I updated my code to the below and can now successfully add users on a POST.

// Signup route
app.post("/signup", (req, res) => {
  const newUser = {
    email: req.body.email,
    password: req.body.password,
    confirmPassword: req.body.confirmPassword,
    handle: req.body.handle,
    phone: req.body.phone,
    name: req.body.name,
    photoURL: req.body.photo,
  };

  admin
    .auth()
    .createUser({
      email: newUser.email,
      emailVerified: false,
      phoneNumber: newUser.phone,
      password: newUser.password,
      displayName: newUser.name,
      photoURL: newUser.photoURL,
      disabled: false,
    })
    .then((data) => {
      return res
        .status(201)
        .json({ message: `user ${data.uid} signed up successfully` });
    })
    .catch((err) => {
      console.error(err);
      return res.status(500).json({ error: err.code });
    });
});

Upvotes: 11

Related Questions