
_spPageContextInfo is not defined in SharePoint modern page,so we could not use it in modern page.
If you are customizing classic page,you need to make sure to use _spPageContextInfo after dom is loaded.If SP.JS file is not loaded that can also cause issue while running _spPageContextInfo command. You can execute command as mentioned below.
Code for your reference:code.txt