You frappe.require() a script symlinked to assets/app_name/js/... folder of the site in question, then use the imported function in the provided callback. The callback is executed in the scope, containing the imported function and you can assign it to a variable you define:
Hi. I know this is an ancient topic at this stage, but I thought you might appreciate my input as it will simplify your life a lot when it comes to doing custom scripts.
I had been facing an issue with importing my own libraries of functions. I had found the documentation for frappe.require() but it said that you need to put your code that makes use of the called assets inside the callback so I put my entire script inside a frappe.require() callback, however that created loading issues. Eventually I found your thread, and your example works so thank you very much. The optimisation that I have found for it is that you do not need to set functions to the functions that you are after in your script you simply call the frappe.require() and all functions listed in your assets will become available, so in your case:
I’ve been trying to import a node module but its not working Can't import external JS Library in Frappe App, I’ve tried frappe.require('/assets/myapp/node_modules/@xeokit/xeokit-sdk/dist/xeokit-sdk.min.es.js'); but that gives
VM788:1 Uncaught SyntaxError: Unexpected token 'export'
at Object.eval (dom.js:33)
at Object.js (assets.js:162)
at Object.eval_assets (assets.js:89)
at assets.js:76
at Object.callback (assets.js:127)
at Object.callback [as success_callback] (request.js:78)
at 200 (request.js:122)
at Object.<anonymous> (request.js:247)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
Hi @satyajit_ghana. Try removing your export statements. Export is not needed in frappe.require() and if I remember correctly, it does interfere with the import if the keyword is found in your script.
As an example, if you had a script, stored in your app’s public/js directory, with a function that prints a greeting message, and you wanted to call it in your document each time you refresh the page:
example.js
function greet_user() {
frappe.msgprint("Hello " + frappe.session.user_fullname)
}
So what i basically want to do is import a node_module (xeokit) in a js file, but i’m not able to.
I don’t want to modify the node module package (xeokit) because every time i deploy i have to do that, and every time there’s a npm i i have to do that.
Ah, I see your problem now. Unfortunately, I’ve never worked with NPM modules, nevermind using them in Frappe. However, I did find a tutorial for using them in Frappe, have you seen this: Tutorial: Adding node modules to Frappe?
$ node rollup/build.js --app myapp
Development mode
✔ Built js/moment-bundle.min.js
✔ Built js/libs.min.js
Building myapp assets...
SyntaxError: Unexpected token (16:416) in /workspace/development/mileone-bench/apps/myapp/node_modules/@xeokit/xeokit-sdk/dist/xeokit-sdk.min.es.js
That’s quite strange as I use the arrow format in my own code without any issues. Well done for getting it to work! Out of interest, is your frappe up to date?