How to initiate IPFS node once and keep using the same PeerID in App?

I have a newbie question about IPFS node creation. The IPFS node can be created as follows:

const IPFS = require(‘ipfs’)

async function main () {
const node = await IPFS.create()
const version = await node.version()

console.log(‘Version:’, version.version)
// …
}

main()

The app should use the same PeerID of the node after initial node creation. That is whenever the app is up and running, a IPFS node with the same PeerID will be launched. Here is what the app should to do (in pseudo code):

const IPFS = require(‘ipfs’)

async function main () {
if (node haven’t been existing) {
const node = await IPFS.create()
else { // node created before
node = LaunchNodeWithExistingPeerID()
}

const version = await node.version() }

console.log(‘Version:’, version.version)
// …
}

main()

This should actually happen automatically! When you start up a node the second time, as long as the repo name is the same (it looks like you aren’t setting it, so it will be the same), it will re-use the identity it generated last time.

Try logging the IPFS node’s identity (with await node.id()) on subsequent runs, and if it’s the same you’re good to go.