Exclude a JavaScript file from a node
Delete/Exclude js/custom.js from the global-styling section in THEMENAME.libraries.yml and define it as a library at the end of the same file

# THEMENAME.libraries.yml
custom-js:
  version: 1.x
  js:
    js/custom.js: {}
  dependencies:
    - core/jquery


And then include the library conditionally to where you want it using THEMENAME_preprocess_page function in THEMENAME.theme

//THEMENAME.theme
function THEMENAME_preprocess_page(&amp;$variables) {

    //include it if its front_page
    if ($variables['is_front'] == TRUE) {
        $variables['#attached']['library'][] = 'THEMENAME/custom-js';
    }

    //include it if its NOT node 4
    $node = \Drupal::routeMatch()-&gt;getParameter('node');
    if ($node instanceof \Drupal\node\NodeInterface &amp;&amp; $node-&gt;id() != 4) {
        $variables['#attached']['library'][] = 'THEMENAME/custom-js';
    }  
}       


For more info read the docs