0

I am trying to use the json2csv javascript package in my Salesforce Web Component, but I get the error: can't find variable 'require'.

I've uploaded the json2csv package as a static resource and am loading it via:

import JSON2CSV from '@salesforce/resourceUrl/JSON2CSV';

export default class MyComponent extends LightningElement {
    /**
     * Variables
     */

    json2csvInitialized = false;

    renderedCallback() {
        if (this.json2csvInitialized) {
            return;
        }
        this.json2csvInitialized = true;

        Promise.all([
            loadScript(this, JSON2CSV)
        ])
        .then(() => {
            var csvString = this.testJson2Csv(json);
            console.log(csvString);
        })
    }

    testJson2Csv() {
        const Json2csvParser = require('json2csv').Parser;
        const fields = ['carModel', 'price', 'items.name', 'items.color', 'items.items.position', 'items.items.color'];
        const myCars = [
            {
              "carModel": "BMW",
              "price": 15000,
              "items": [
                {
                  "name": "airbag",
                  "color": "white"
                }, {
                  "name": "dashboard",
                  "color": "black"
                }
              ]
            }, {
              "carModel": "Porsche",
              "price": 30000,
              "items": [
                {
                  "name": "airbag",
                  "items": [
                    {
                      "position": "left",
                      "color": "white"
                    }, {
                      "position": "right",
                      "color": "gray"
                    }
                  ]
                }, {
                  "name": "dashboard",
                  "items": [
                    {
                      "position": "left",
                      "color": "gray"
                    }, {
                      "position": "right",
                      "color": "black"
                    }
                  ]
                }
              ]
            }
        ];
        const json2csvParser = new Json2csvParser({ fields, unwind: ['items', 'items.items'], unwindBlank: true });
        const csv = json2csvParser.parse(myCars);
        return csv;
    }
}

What am I missing? How else am I suppose to use the parser?

  • You don't seem to be loading the JS from the resource. loadScript(this, JSON2CSV) -- isn't this missing the path for the JS within your static resource? – Jayant Das Mar 7 at 21:38
  • You'll need to import requirejs in order for this library to work. – sfdcfox Mar 7 at 21:39
  • @JayantDas I don't believe so since it's just the resource file. The file is not contained in a zip or anything. – BlondeSwan Mar 7 at 21:41
  • @sfdcfox do you know if I have to upload my own static resource for that, or is requirejs default in the salesforce environment? – BlondeSwan Mar 7 at 21:43
  • @BlondeSwan Yes, you'd need to import requirejs. Alternatively, if you're trying to use the NPM, you can try using browserify to bundle require/json2csv/etc all in a single resource. – sfdcfox Mar 7 at 21:45
0

Turns out you just need to replace

const Json2csvParser = require('json2csv').Parser;

with

const Json2csvParser = json2csv.Parser;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.