question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Import Parsley.js with es6 does not register plugin

See original GitHub issue

What kind of issue is this? (put ‘x’ between the square brackets)


Sadly this can be reproduced on codepen. But it is not hard to reproduce. When adding parsley natively using import (in supported browsers like Chrome or Safari) does not registers the plugin in the jQuery instance.

Take this main.js file:

import './libs/jquery/jquery-3.3.1.min.js';
import './libs/jquery/jquery-ui-1.12.1/jquery-ui.min.js';
import './libs/parsley-2.8.1/parsley.js'; 

It Throws the error:

Uncaught TypeError: Cannot read property ‘jQuery’ of undefined

while jQueryUI does work as expected.

The error comes fron somewhere around this at the beginning from parsley.js:

typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) :
  typeof define === 'function' && define.amd ? define(['jquery'], factory) :
  global.parsley = factory(global.$)

My workaround is adding a global verification

  global = (global) ? global : window;
 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) : typeof define === 'function' && define.amd ? define(['jquery'], factory) : global.parsley = factory(global.jQuery);

I do not dare to make a PR because I am not familiar with the Parsley source and I am not quite sure what “global” is suppose to be (Also I can no find the source for that part 😞 )

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

1reaction
denis-gorincommented, Apr 16, 2020

hi, I’m using jQuery from ajax.googleapis.com cdn and build my scripts with webpack. So I’ve solved this issue for me by adding jQuery in externals section in webpack config

    externals: {
        jquery: 'jQuery'
    },

and import parsley

import 'parsleyjs'
1reaction
distantecommented, Feb 8, 2018

Thanks @marcandre using ../../path/to/my/jquery.js it works.

Would be nice if we could use the dist version also with import like jquery-ui.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Import Parsley.js with es6 does not register plugin -
while jQueryUI does work as expected. The error comes fron somewhere around this at the beginning from parsley.js : typeof exports === 'object'...
Read more >
node.js - How do I set up parsely with nodejs? - Stack Overflow
I'm using Nodejs, ES6, Express, PUG. I'm having a hard time finding something that works. I think parsley would do it for me,...
Read more >
The ultimate documentation - Parsley
Parsley, the ultimate frontend javascript form validation library.
Read more >
Parsley - Find the help you need
Parsley, the ultimate frontend javascript form validation library. ... StackOverflow: need a hand, not sure how to do something? Parsley ships with a...
Read more >
How To Enable ES6 Imports in Node.JS | TimOnWeb
As of now, Node.js doesn't support ES6 imports yet. ... act as an entry-point for our node.js app and will contain babel registration...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found