How to make lodash import in JSBin?

Viewed 3117

I need to show one code in JSBin with the lodash and cannot figure it out how to make a 'get' import. I've got "ReferenceError: require is not defined https://jsbin.com/hotobir/edit?js,console

// Normally that is working fine
import { get } from 'lodash'

//No import get from 'lodash/get'
//No const { get } = lodash

const obj = { myKey: 'myValue' }
console.log(get(obj, 'myKey', 'notThere'))

How to do this import?

3 Answers

Yeah, the Add Library option doesn't work the way I expected either.

It prepends the script tag to the html output, so if you don't make any changes it doesn't work.

Solution: just move the script tag into the head tag.

By default, the JSBin drop the script import in the wrong place. It shoul to be in the head of html.

const {get} = _
const obj = { myKey: 'myValue' }
console.log(get(obj, 'myKey', 'notThere'))

To add any library in jsbin place the cursor inside head of the html, add the lib, and make sure you add the language='javascript' in the <script tag. That way works for me at least.

Eg. From this:

  <script
    src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"> 
  </script>

To this:

  <script
    language='javascript'
    src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"> 
  </script>
Related