Magento – Unable to run custom js to admin backend

adminhtmljavascriptmagento-2.1

I have a module with the intention of modifying some elements on a page in the admin backend.

In my module/name/view/adminhtml/layout/default.xml file I have

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <head>
     <script src="module_name::js/priceChange.js"/>
   </head>
</page>

In module_name/view/adminhtml/web/js/priceChange.js

define([
"jquery",
], function(jQuery){
   "use strict";
   (function(e){ 
    console.log("Message");
    });
});

If I change define to require, it doesn't make a difference.
And in module_name/view/adminhtml/requirejs-config.js Is have:

var config = {
    map: {
        '*': {
            customAdmin: 'Cubed_AdminStyles/js/priceChange'
        }
    },
    deps: ["jquery"]
};

When I load any page in the backend I am seeing: Uncaught ReferenceError: define is not defined.

Looking at other questions about adding custom javascript, they call the javascript on a phtml page, I don't have that in my module. It will just be that javascript file. This question, makes it seem that you can add the js file without needing to call it on a phtml page, however, I am unable to get it to work.

Best Answer

change define to require fix your problem

Modify code a bit

// avoid re-define jquery object
require([
 "jquery",
], function(jQuery){
  "use strict";
  (function(e){ 
    console.log("Message");
  })();
});