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.

adding npm libary to dependencies

See original GitHub issue

I used the simple-json-datasource Plugin and added the mqtt library github.com/mqttjs/MQTT.js like so:

I ran npm install mqtt --save in the simple-json-datasource folder, so it would be in the package.json as a dependency.

in the datasource.js I added:

import mqtt from "mqtt";

afterwards I ran the grunt task which build the src files to the dist folder.

I also ran npm install mqtt --save in the grafana folder and ran the grunt task there also, but I always get the following error when I try to add my simple-json-datasource Plugin:

Plugin Error Error loading http://localhost:3000/public/mqtt as “mqtt” from http://localhost:3000/public/plugins/grafana-simple-json-datasource/datasource.js

I cant figure out how to add a external library to the datasource Plugin and get it running, do I miss out something?

I would be very thankful for some help or advice on how to use npm libraries which arent included in grafana by default. I couldnt find any information regarding that matter.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
aksakallicommented, Apr 12, 2017

@LaPush I came across the same issue with including additional dependency. I used this experimental plugin as boilerplate to tackle this issue: https://github.com/NatelEnergy/grafana-plotly-panel/

  1. You need to create a folder: src/external/
  2. Add the compiled single file dist versions of your dependency under this folder like src/external/mqtt.js. (Actually even Grafana project has vendors in git repository https://github.com/grafana/grafana/tree/master/public/vendor)
  3. In build task, you need to copy the files under your external folder, so your Gruntfile.js should be like:
module.exports = (grunt) => {
  require('load-grunt-tasks')(grunt);

  grunt.loadNpmTasks('grunt-execute');
  grunt.loadNpmTasks('grunt-contrib-clean');

  grunt.initConfig({

    clean: ['dist'],

    copy: {
      src_to_dist: {
        cwd: 'src',
        expand: true,
        src: ['**/*', '!**/*.js', '!**/*.scss', '!img/**/*'],
        dest: 'dist'
      },
      pluginDef: {
        expand: true,
        src: ['README.md'],
        dest: 'dist',
      },
      externals: {
        cwd: 'src',
        expand: true,
        src: ['**/external/*'],
        dest: 'dist'
      },
      img_to_dist: {
        cwd: 'src',
        expand: true,
        src: ['img/**/*'],
        dest: 'dist'
      }
    },

    watch: {
      rebuild_all: {
        files: ['src/**/*'],
        tasks: ['default'],
        options: {spawn: false}
      },
    },

    babel: {
      options: {
        sourceMap: true,
        presets: ['es2015'],
        plugins: ['transform-es2015-modules-systemjs', 'transform-es2015-for-of'],
      },
      dist: {
        files: [{
          cwd: 'src',
          expand: true,
          src: ['*.js'],
          dest: 'dist',
          ext: '.js'
        }]
      },
    },

  });

  grunt.registerTask('default', ['clean', 'copy:src_to_dist', 'copy:pluginDef', 'copy:img_to_dist', 'copy:externals', 'babel']);
};
  1. Now you can import the external library: import * as mqtt from './external/mqtt';
1reaction
bergquistcommented, Jun 20, 2016

When running npm install you will download that package into node_modules. Which is not available for grafana. To use npm packages you need to first download them and then copy them from node_modules to the rootfolder/dist folder.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding dependencies to a package.json file - npm Docs
To add dependencies and devDependencies to a package.json file from the command line, you can install them in the root directory of your...
Read more >
NPM doesn't install module dependencies - Stack Overflow
So, I read it carefully and found that the problem was that I didn't specify my repo, description, and valid name in my...
Read more >
An Absolute Beginner's Guide to Using npm - NodeSource
Having dependencies in your project's package.json allows the project to install the versions of the modules it depends on. By running an ...
Read more >
Installing Dependencies with npm - LearnHowToProgram.com
Installing an npm Package · npm will add the package to a directory named node_modules . If the directory doesn't exist yet, npm...
Read more >
npm install supports local packages and dependencies
The above install command adds some-local-package to your package.json 's dependencies. The local package definition will then include the file: ...
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