Magento 2 – External JS for Backend Not Loaded

adminhtmljavascriptjquerymagento-2.1requirejs

I create the external js for magento 2 backend like this:

Namespace/Module/view/adminhtml/requirejs-config.js:

 var config = {
    map: {
        '*': {
            regionsForm: 'Namespace_Module/js/regions-form'
        }
    }
};

in Namespace/Module/view/adminhtml/web/js/regions-form.js

    define([
        'jquery',
        'underscore',
        'mage/url',
        'jquery/ui'
        ], function ($, _,urlBuilder) {
        'use strict';
        $(function() { 
           console.log('a'); 
        });
    });

i already delete pub/static and ran setup:upgrade & static:content, but when i inspect element in my browser i cant find my js file, but in pub/static my file is already there.

Edit:
I ran deploy command php bin/magento setup:static-content:deploy

Best Answer

The reason is that we didn't call the custom script. So, we can call it via template or the head. You can try two cases:

1) We can add directly on the head.

app/code/Namespace/Module/view/adminhtml/layout/default.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <link src="Namespace_Module::js/regions-form.js"/>
    </head>
    <body/>
</page>

In this case, remember to change define to require in the custom script.

require([
    'jquery',
    'underscore',
    'mage/url',
    'jquery/ui'
], function ($, _,urlBuilder) {
    'use strict';
    console.log('Here');
    return 1;
});

2) We can call it via template:

app/code/Company/Module/view/adminhtml/templates/test.phtml

<script>
    require(["regionsForm"], function($form){
        //Your code here
    })
</script>