problem using jquery with npm

1

I have a problem calling jquery. In my project I have installed npm, with the package.json, there I have entered the dependency of jquery. I'm also calling the 'bundle.js' script from BUDO (node_modules). Question, in my main file 'index.js', I need to declare jquery calling it with a require (at least, that's what I did with the simple-peer package), the issue is that I do not know how to call it, how to define it so that it works for me , if you can help me, I thank you. I leave the code below my index.js

var getUserMedia = require('getusermedia')

getUserMedia({ video: true, audio: false }, function (err, stream) {
  if (err) return console.error(err)

  var JQ = require('jquery')
  var jq = new JQ()

  $('#yourMessage').val('hola')

  var Peer = require('simple-peer')
  var peer = new Peer({
    initiator: location.hash === '#init',
    trickle: false,
    stream: stream
  })

  peer.on('signal', function (data) {
    document.getElementById('yourId').value = JSON.stringify(data)
  })

  document.getElementById('connect').addEventListener('click', function () {
    var otherId = JSON.parse(document.getElementById('otherId').value)
    peer.signal(otherId)
  })

  document.getElementById('send').addEventListener('click', function () {
    var yourMessage = document.getElementById('yourMessage').value
    peer.send(yourMessage)
  })

  peer.on('data', function (data) {
    document.getElementById('messages').textContent += data + '\n'
  })

  peer.on('stream', function (stream) {
    var video = document.createElement('video')
    document.body.appendChild(video)

    video.src = window.URL.createObjectURL(stream)
    video.play()
  })
})

There I have $ ('# yourMessage'). val ('hello'), but it tells me that '$' is not a function

    
asked by Alejandro Viera 07.11.2017 в 22:42
source

1 answer

0

Change your require to $ = require('jquery'); and it's all.

If you are using webpack you can use the ProvidePlugin that automatically loads the modules without using import/require . This in webpack.config.js .

new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery'
})
    
answered by 07.11.2017 / 22:55
source