Developer dayan
Developer dayan

Reputation: 69

sh: 1: cross-env: not in node js app found while deploying to heroku

i am trying to deploy my project to heroku, but this is what i get, i have tried everything i could but still this problem please help me, My package.JSON

     {
      "name": "storybooks",
      "version": "1.0.0",
      "description": "App for creating public and private stories",
      "main": "app.js",
      "scripts": {
        "start": "cross-env NODE_ENV=production node app",
        "dev": "cross-env NODE_ENV=development nodemon app",
        "postinstall": "node ./scripts/post-install.js"
      },
      "author": "Dayan",
      "license": "MIT",
      "dependencies": {
        "all": "^0.0.0",
        "connect-mongo": "^3.2.0",
        "dotenv": "^8.2.0",
        "ejs": "^3.1.6",
        "express": "^4.17.1",
        "express-handlebars": "^4.0.4",
        "express-session": "^1.17.1",
        "fs-extra": "^9.0.1",
        "method-override": "^3.0.0",
        "moment": "^2.27.0",
        "mongoose": "^5.9.19",
        "morgan": "^1.10.0",
        "passport": "^0.4.1",
        "passport-google-oauth20": "^2.0.0"
      },
      "devDependencies": {
        "cross-env": "^7.0.3",
        "nodemon": "^2.0.4"
      }
    }

My files structure

I will appreciate the help

This is my .env

PORT = 3000
MONGO_URI = mongodb+srv://<Myuserhere>@cluster0.sjptc.mongodb.net/storyBooks
GOOGLE_CLIENT_ID = ##
GOOGLE_CLIENT_SECRET = ##

My database file

const mongoose = require('mongoose')

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
    })

    console.log(`MongoDB Connected: ${conn.connection.host}`)
  } catch (err) {
    console.error(err)
    process.exit(1)
  }
}

module.exports = connectDB

My passport file

const GoogleStrategy = require('passport-google-oauth20').Strategy
const mongoose = require('mongoose')
const User = require('../models/User')

module.exports = function (passport) {
  passport.use(
    new GoogleStrategy(
      {
        clientID: process.env.GOOGLE_CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET,
        callbackURL: '/auth/google/callback',
      },
      async (accessToken, refreshToken, profile, done) => {
        const newUser = {
          googleId: profile.id,
          displayName: profile.displayName,
          firstName: profile.name.givenName,
          lastName: profile.name.familyName,
          image: profile.photos[0].value,
        }

        try {
          let user = await User.findOne({ googleId: profile.id })

          if (user) {
            done(null, user)
          } else {
            user = await User.create(newUser)
            done(null, user)
          }
        } catch (err) {
          console.error(err)
        }
      }
    )
  )

  passport.serializeUser((user, done) => {
    done(null, user.id)
  })

  passport.deserializeUser((id, done) => {
    User.findById(id, (err, user) => done(err, user))
  })
}

So this is the code for the files, for the package json, passport.js, db.js, config.env but i cannot figure out the errors that i am getting, i solved the other one with the help of a developer but this is what i get one **MongoParseError: Invalid connection string**

Upvotes: 1

Views: 2746

Answers (1)

Enrique Tejeda
Enrique Tejeda

Reputation: 366

cross-env is a development dependency. Heroku, by default, only installs production dependencies. That's why it's throwing that error.

Apart from that, i don't recommend you to use a dev server on heroku, if you run a server (i see the express dependency) you need to run the app.js like node app.js on npm start

Upvotes: 0

Related Questions