javascriptnode.jsbbc-microbit

SyntaxError: Identifier 'pkg' has already been declared - JavaScript and Node.js


Wrote this code that uses serialport and spotify-web-helper to use the BBC Micro:Bit as a music remote, after some debugging I got stuck here and now I don't know how to fix this.

Here is the whole code:


import { SerialPort } from 'serialport';

import pkg from 'serialport';
const { SerialPortParser } = pkg;

import pkg from 'spotify-web-helper';
const { SpotifyWebHelper } = pkg;
const spotify = SpotifyWebHelper({'port': 4381});

const port = new SerialPort({
  path: 'C:\Users\Splat\Downloads\SpotBit\spotbit',
  baudRate: 115200,
  autoOpen: false
})
const parser=new Readline();
port.pipe(parser);

spotify.player.on('ready', () => {
    console.log("Spotify è pronto!")
    port.open(() => {
        console.log("Port aperto");
        parser.on('data', (data) => {
            console.log('Content ricevuto: ' + data.toString());
            processData(data);
        });
    })

    function processData(data) {
        if (data.indexOf('PLAY') == 0) {
            // Handle PLAY received
            spotify.player.pause(true);
        } else if (data.indexOf('PAUSE') == 0) {
            // Handle PAUSE received
            spotify.player.pause(false);
        }
    }
});

Tried using

var

without great results.

What could cause the error?

Please consider that I am still a newbie in developing.


Solution

  • You can't have two import pkg statements. Skip the pkg variable and just use destructuring to declare the names you want:

    import { SerialPortParser } from 'serialport';
    import { SpotifyWebHelper } from 'spotify-web-helper';