WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.52k stars 4.21k forks source link

Unexpected editor error when importing lodash to block #6399

Closed tinboyko closed 6 years ago

tinboyko commented 6 years ago

Issue Overview

Steps to Reproduce (for bugs)

  1. Create block
  2. import { get } from 'lodash';
  3. The editor has encountered an unexpected error.

react-dom.min.b75d8b30.js:62 TypeError: this.activateMode is not a function at load-scripts.php?c=1&load[]=hoverIntent,common,admin-bar,svg-painter,heartbeat,wp-auth-check,backbone,wp-util,wp-backbone,media-models,wp-plupload,jquery-ui&load[]=-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-sortable,wp-mediaelement,media-views,postbox&ver=4.9.5:298

Chrome

roborourke commented 6 years ago

@TinBoyko it's because lodash is already in a global context so you have to import the sub script directly eg:

import get from 'lodash/get';

Just needs documenting I think.

keesiemeijer commented 6 years ago

related #4043

aduth commented 6 years ago
  1. Create block

Can you elaborate? Share some sample code?

aduth commented 6 years ago

In debugging this further, just as in #4043, this is nothing to do with Gutenberg itself, but rather the plugin's loading of Lodash causing Underscore to be overridden, therefore conflicting with usage in media scripts. You'll see similar breakage on the classic editor if you allow your scripts to be loaded there.

You should do one of the following:

robertsheacole commented 6 years ago

Just for anyone getting an error related to _.pluck is not being defined in WordPress. I have a plugin using Vue.js and inside of one of my templates I am importing lodash. This caused a collision and the post editor in the admin broke. You could not see the content in the view tab, but you could in the text tab. In face, those buttons didn't work because that's what WP core is using the pluck function on in core. When I removed lodash it worked and also when I imported the specific name spaced version, like import get from "lodash/get" it worked.

hybridwebdev commented 1 year ago

5 years later, still an issue. @roborourke solution was what fixed it for me.