Initialize only once Parse

I am using Parse JS SDK un conjunction with Nextjs Framework. I don’t want to call initialize() on every request, so I need to know if there’s any method or property to check if Parse is already initialized

Hi @marcelobaez. As I did it before, you can create a file and import Parse. Then initialize and export it.
Something like this:

// lib/parse-client/index.js
const Parse = require("parse/node");

Parse.initialize(process.env.APP_ID, process.env.MASTER_KEY);
Parse.serverURL = process.env.SERVER_URL;
Parse.masterKey = process.env.MASTER_KEY;

export default Parse;

And every time you need to use Prase, just import it from this file. This way, you can make sure it’s initialized.
Let me know if you had more questions.

Hi, I have a problem when try to initialize the parse. Try like @hrahimi270 said:

const Parse = require(‘parse/node’);

or

import Parse from ‘parse/node’;

But when I run the nextjs, the console show this error:

info - Using external babel configuration from app/.babelrc
error - ./node_modules/xmlhttprequest/lib/XMLHttpRequest.js:15:0
Module not found: Can’t resolve 'child_process’

are you trying to use this in a nextjs app? or a nodejs app? Since nextjs will run on the browser you want to instead just do import Parse from 'parse'

yes I use nextjs. I fix when is initialize the parse like this:

import Parse from ‘parse’;
if (typeof window !== ‘undefined’) {
Parse.initialize( process.env.NEXT_PUBLIC_APP_ID …

I had to do this because the initialization must be in the browser and not in the server. Because if it is done on the server, it generates an error that localstore does not exist

Have you tried this? Looks like for next you can use @parse/react-ssr

1 Like

Thanks, work like a clock