This topic contains 4 replies, has 0 voices, and was last updated by cblackburn 7 years, 11 months ago.

  • Author
    Posts
  • #1844

    darrenhillconsulting

    So,

    Trying to use my favorite library in SS 2.0, but not luck. Anyone else? Here’s my details:

    – lodash.js in SuiteScripts/DH

    – my define is as followings (my script file is also in SuiteScripts/DH)

    Code:
    define([“require”, “exports”, ‘N/search’, ‘N/ui/dialog’, ‘./lodash’], function (require, exports, search, dialog, _) {
    Lodash seems to deploy correctly, because if I change the path ./lodash, to anything else, it fails to load that library.

    The first instance I tried to use it … _.VERSION, console spits out an Cannot read property ‘VERSION’ of undefined

    So, why is it undefined?

    My buddy, John.O, suggested I use https://www.npmjs.com/package/lodash-amd, but that didn’t work either.

    Anyone else use lodash?
    This is a cached copy. Click here to see the original post.

  • #1845

    darrenhillconsulting

    Ok, so I figured it out (but don’t FULLY understand). Here’s the changes that made it work for me

    I added this line to my script

    Code:
    ///
    This produced the define line as follows

    Code:
    define([“require”, “exports”, “./lodash”, ‘N/search’, ‘N/ui/dialog’], function (require, exports, lodash, search, dialog) {
    And here’s the kickers, instead of using lodash.VERSION … I used _.VERSION, and it works.

  • #1846

    ironside

    That doesn’t make sense at all Darren, does it? Unless somehow that’s coercing lodash to behave a a global rather than a module, but I didn’t think that was possible?

    I originally used the amd-dependency-path directive (TypeScript) to make sure the emitted JS refers to the correct path where lodash is installed in the NS account. I coupled that with the typings by referencing index.d.ts in my tsconfig.js. Haven’t had any troubles with lodash working in that scenario.

    For scripts that needed both lodash and moment I had something like this:

    Code:
    ///
    ///
    Fast forward to today and I’m almost done switching over to TS 2.0.0 and do not use the amd-dependency-path directive, instead using the @typings namespace and importing lodash like so:

    Code:
    import * as _ from “lodash”
    No need for the typings tool, the compiler finds the lodash declaration automatically and generates a define statement like:

    Code:
    define([“require”, “exports”, ‘N/record’, ‘N/format’, ‘../EC_Logger’, “moment”, “lodash”], function (require, exports, record, format, LogManager, moment, _) {
    The last step I’m working on now is to resolve the paths to the actual lodash and moment implementation files via require.config.

    p.s. I use webstorm as my IDE.

  • #1847

    starlingMark

    Hi Guys,

    I know I’m a little late to this party, but I was trying to get lodash in as a module as well. I have my lodash in a subfolder called “modules”. Don’t know if NS has changed anything on their side, but all I did was:

    Code:
    define([‘N/currency’, ‘N/format’, ‘N/search’, ‘N/ui/serverWidget’, ‘./modules/lodash.min.js’]
    function(currency, format, search, serverWidget, lodash) {
    Now both lodash.VERSION and _.VERSION work.

  • #1848

    cblackburn

    The typical AMD pattern works for me with version 4.17.2

    Code:
    /**
    *
    * @NApiVersion 2.x
    * @NScriptType Suitelet
    * @NModuleScope SameAccount
    */
    define([‘/SuiteScripts/lib/lodash’],
    function (_)
    {
    function onRequest(context)
    {
    context.response.write(JSON.stringify(_.defaults({ ‘a’: 1 }, { ‘a’: 3, ‘b’: 2 })));
    }

    return {
    onRequest: onRequest
    }

    });

You must be logged in to reply to this topic.