How can I use javascript functions defined in a di...
# suitescript
a
How can I use javascript functions defined in a different file in Suitescript? Can I use relative file path references?
s
I use relative path references all the time
a
Ah nice. Can I put it all in its own folder in the Suitescript folder?
s
Yes - in fact as a best practice I root all my SuiteScript 2.x files under a single root folder (and subfolders if needed). In my case I name the folder
SS2
and put all SuiteScript 2.x stuff there, to distinguish clearly from any old SuiteScript 1.0 that might live elsewhere
I import from relative paths exclusively, except for the
N/*
modules from NetSuite
a
And this is all from on top of the define?
s
I use TypeScript so write my code using modern JS
export
import
syntax (i.e. I don't hand write a
define()
function at all)
s
Same here, but here’s a sample of the JS
Copy code
define(["../folderName/functionLibrary", "N/log", "N/runtime", "N/xml"], function (functionLibrary, log, runtime, xml) {
...
}
s
this has the benefit of it being impossible for me to mess up the define dependencies and the variables that get assigned to said dependencies 🙂
a
@Steven how would I use my own external functions?
@Steven Ah I see what you mean.
s
Yeah I write the functions in a library (note I also write in Typescript so I export them) then I can import the library and call something like
functionLibrary.functionOne()
I’d definitely recommend using the HITC typescript library and uploader. It’s free and well maintained (responsive to github issues as well). Probably the most useful tool for quick development
a
I don't know typescript unfortunately.
s
Never too late to learn 🙂 It’s just javascript with rules really
👍 1
💯 1
a
Maybe after my company completes their implementation and I have time to breathe.
s
That's fair haha
m
You can use the typings in regular JS as well
One day I'll get around submitting a pull request to update the readme with instructions
c
One thing to note - if the JS file is in the same dir, it looks like you have to prepend ./ before the filename in the define statement
e.g.
Copy code
define['MessageDispatcher']
throws an error (if you're creating a script record).
however
Copy code
define['./MessageDispatcher']
works fine.