jquery.lazy icon indicating copy to clipboard operation
jquery.lazy copied to clipboard

Node.js - Adding module.exports

Open alex-w0 opened this issue 6 years ago • 11 comments

Please use in your library the function module.exports to allow requiring the module with the es6 command "import"

http://stackabuse.com/how-to-use-module-exports-in-node-js/

alex-w0 avatar Dec 13 '17 15:12 alex-w0

Hello @DevelGitH,

I know how module.exports work. But as Lazy is a client-side script, what should I export? Could you give me an example what you want to be exported and how you use it? I'm open for any improvement!

dkern avatar Dec 13 '17 17:12 dkern

Hi @eisbehr- , thank´s for your answear. Yeah thats true, that you cannot export your library, when you have a client-side script. A solution would be to put your code into a object class or function and afterwards you can export the function with module.exports.

The problem is when i`m trying to import your lazy-load script, it throws an error at the line 43 of the file jquery.lazy.js ($.fn is not defined), because the variable window.$ from the main script couldn´t be passed to your script.

My Code is the following:

//Import jQuery Library
import $ from 'jquery';
window.$ = window.jQuery = $;

import './lib/jquery.lazy';
$("img.lazy").Lazy();

alex-w0 avatar Dec 13 '17 21:12 alex-w0

I will keep this open as an enhancement. But I need to think about the implementation.

dkern avatar Dec 15 '17 14:12 dkern

Hi, I'm using Zurb Foundation CLI, installed it via npm and tried:

import 'jquery-lazy';

copied file to lib folder and tried:

import import './lib/jquery.lazy';

these solutions doesn't work for me, I get this error:

Uncaught TypeError: Cannot read property 'fn' of undefined

How can I import library to my project?

massprogressive avatar Aug 16 '18 00:08 massprogressive

The error sounds like you didn't added jQuery to your project, because fn is only used register the plugin within jQuery.

dkern avatar Aug 17 '18 06:08 dkern

I've used the same import approach as if the first comment:

import $ from 'jquery';
window.$ = window.jQuery = $;

massprogressive avatar Aug 17 '18 08:08 massprogressive

Do you know that this is an issue and it is still open? Why do you think it will work for you, if it would not work for others?!

dkern avatar Aug 17 '18 08:08 dkern

The only solution that I found was pasting import statements on top of the jquery.lazy.js file:

import $ from 'jquery';
window.$ = window.jQuery = $;

/*!
 * jQuery & Zepto Lazy - v1.7.9
 * http://jquery.eisbehr.de/lazy/
 *
 * Copyright 2012 - 2018, Daniel 'Eisbehr' Kern
 *
 * Dual licensed under the MIT and GPL-2.0 licenses:
 * http://www.opensource.org/licenses/mit-license.php

Now it works for me and hope will work for others.

massprogressive avatar Aug 19 '18 08:08 massprogressive

Yeah but that solution wouldn't be nice. The jquery library will now be imported twice, when the developer is using the library in his project. Have a look at other libraries, they aren't importing the required libs in their project.

alex-w0 avatar Aug 19 '18 09:08 alex-w0

Yes, I know that problem. In the new version of Lazy the import problem is solved and there is no dependency on jQuery anymore. But I can't actually tell when the release will be ready. I think I will publish the first draft within the next time.

dkern avatar Aug 20 '18 06:08 dkern

Unfortunately this is still an issue. Any news on the new version?

eistruhe avatar Feb 28 '19 16:02 eistruhe