I’m trying to follow this SuiteAnswers: <https://n...
# suitescript
c
I’m trying to follow this SuiteAnswers: https://netsuite.custhelp.com/app/answers/detail/a_id/45054 but I can’t get the module to load correctly. Has anyone had success with this? I’m working on what’s shaping up to be a larger code base and I’d like to avoid loading larger libraries unless I actually need them.
b
use absolute paths
c
Will try that and report back.
b
if you require relative paths, then add the local require as a dependency Local vs Global require
c
Using absolute paths was the right answer. I’m still not sure if the return statement should be returning an instance of the module or a specific function.
b
do what you want
both work
c
I can get either to work at the level of the loader module, however I get
undefined
back from the
return new lib1();
statement.
i.e.
var lib = loader.newInstance('myType');
comes back
undefined
even though it’s clearly there in the loader method. I don’t get it…
b
what does your code look like
c
Copy code
define({
	newInstance: function (type) {
		switch (type) {
			case 'makeKeyFile':
				require(['/SuiteApps/com.sample.app/lib/create_key'],
					function (createKey) {
						return createKey; 
						// also tried return new createKey();
					});
				break;
			case 'lib2':
				require(['/lib2'], function (lib2) {
					return new lib2();
				});
				break;
			default:
				return null;
		}
	}
});
then, to call it from another file
var createKey = loader.newInstance('makeKeyFile');
b
huh
ive never seen an object used as an argument to a define function
that said, your function does not return anything for your 2 cases
nested return statements means you return from your inner function and return nothing in your outer function
in more normal amd environments, you would need to redesign this since the amd require is usually asynchronous
in suitescript, you can just set a variable in your outer function's closure scope and return that
c
Thanks for the context, that’s the part I was missing.
The NS environment is the hardest part of NS.
b
this was more of a javascript problem than a netsuite thing
c
true 🙂
So after lots of frustration I explored another route and tried this alternative and it seems to work: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html#dynamic-import-expressions.