In the modern SharePoint online page - adding script editor web part **is not recommended**. We can add the script editor web part using the SPFx web part. In the GitHub, there is a modern script editor web part which we can install for the same purpose.

1: Clone or Download the repo:

You will have to clone this repo: https://github.com/SharePoint/sp-dev-fx-webparts 
or
Directly download it from here: 
https://github.com/SharePoint/sp-dev-fx-webparts/tree/master/samples/react-script-editor

2. Edit the **Write-manifests.json**.

3. Build the package

4. Build the package and deploy

Demo Example:

[![Modern Script Editor Webpart][1]][1]


Source:

[Add The Script Editor Web part back to SharePoint Modern Experience][2]

[Script editor web part for SharePoint Modern pages.][3]


**How to enable script editor web part in SharePoint online classic page?**

First, **enable the custom scripting from the admin center** by following the below the navigation, **then need to execute the PowerShell script** - I had also faced this but with the combination of these two - got it fixed.

Go to your tenant setting page thru this URL

https://sprnd-admin.sharepoint.com/_layouts/15/online/TenantSettings.aspx

Note: Here “spend” is my tenant name, you need to pass your tenant name.

Now see the “Custom Script” section by default both the radio button is selected as prevent – this is the default behavior and due to this script editor and content editor web part is missing from SharePoint online page.

Now select to allow users to run the custom script for both the radio button as like below:

Allow users from running custom script on personal sites.
Allow users from running custom script on self-service created sites)


[![Allow Custom Script in SharePoint Online][4]][4]


This will not enable the custom script until we execute the below PowerShell Script.

 $adminUserID=”youradminaccount@< sprnd.onmicrosoft.com >”
 
 $userCredential = Get-Credential -UserName $adminUserID -Message “Enter password”
 
 Connect-SPOService -Url https://sprnd-admin.sharepoint.com/ -Credential $userCredential
 
 Set-SPOsite “https://sprnd.sharepoint.com/sites/TestSite001″ -DenyAddAndCustomizePages 0

Once, we execute the above PowerShell script, immediately script editor will be available.

For details refer to the below article:

[SharePoint Online – O365: How to enable script editor web part in SharePoint online][5]


 [1]: https://i.sstatic.net/cmq7P.png
 [2]: https://medium.com/niftit-sharepoint-blog/add-the-script-editor-webpart-back-to-sharepoint-modern-experience-688a7b7208e4
 [3]: https://sureshunakka87.wordpress.com/2019/03/18/script-editor-web-part-for-sharepoint-modern-site-pages/
 [4]: https://i.sstatic.net/ziL4K.jpg
 [5]: https://global-sharepoint.com/sharepoint-online/how-to-enable-script-editor-web-part-in-sharepoint-online-site/