1

Im trying to export my jQuery file as a module using module.exports so it can be called using:

var saveLocation = require('saveLocation');

Have tried: module.exports = jQuery(function saveLocation(){...

How can I go about exporting this code.

N.B. I have removed some of my function code for simplicity of the issue.

Here is my code:

'use strict';

jQuery(function saveLocation(){

    var saveLocationContainer = jQuery('.save-container'),
    favoriteIcon = saveLocationContainer.find('.glyphicon'),
    favoriteLocationsListGroup = jQuery('.list-group');

    var hasFavoriteLocations = false;

    // The following function outputs the specific address info based on location

    function showLocationByAddress(e) {
        e.preventDefault();
    }
};
Web Tech
  • 173
  • 1
  • 3
  • 11

2 Answers2

0

var module = require('./relative/path/to/file');

You need to provide relative path to file with module you want to import, so you probably need something like:

var whatEver = require('./saveLocation');

assuming that saveLocation is file with exported function and is in the same directory as file in which you are trying to import module.

If you are using ES6 you can do something like this:

export default function(){...};

import someFunction from './path/to/module';

Kacper Wiszczuk
  • 1,809
  • 11
  • 14
0

module.exports pairs with import, not require. First export your function from the jQuery file.

var saveLocation;
(function($) {
   saveLocation = {
    ... [jquery]...
  };
})(jQuery);

module.exports = { saveLocation }

Then you can import it:

import { saveLocation } from './my-plugin-file';

If you want to use require instead, this thread might help: how to use require function in js

Erin
  • 5,315
  • 2
  • 20
  • 36