0

I am struggling to try to import a class in Javascript (located in the leaflet-m2t.js file) is there an error keeps getting thrown m2t is not defined. The file is getting imported just fine, and the error is not in the file but in my code. I would appreciate some help in addressing this problem

require('./leaflet-m2t.js')
$scope.tileProvider = new m2t.tileProvider({
    map: map,
    tileUrl: '/m2t',
    defaultLayer: 'Dark',
    tileLayers: [],
    outlineLayers: ['Light', {
        name: 'Dark',
        style: {
            "color": "#737373",
            "weight": 1,
            "fillOpacity": 1,
            "fillColor": "#0d0d0d",
            'backgroundColor': '#242425',
            'clickable': false
        }

                }]
});

2 Answers 2

2
var m2t = require('./leaflet-m2t.js');

Unlike in Java you should define what you are importing, in this case the default export of leaflet. JavaScript doesn't magically know what you're referring to, this only works for global variables.

If leaflet uses es6 exports you will need to do:

var m2t = require('./leaflet-m2t.js').default;

Or if you have your app configured to handle es6 imports:

import m2t from './leaflet-m2t.js';
Sign up to request clarification or add additional context in comments.

8 Comments

now i am getting an error m2t.tileProvider is not a constructor
Add how you are exporting your class in leaflet-m2t.js
what do you mean by exporting this
I've added more info, incase leaflet-m2t.js uses es6 exports instead of module.exports. If you open that file you can see how it is exporting.
ok so when I use .default i get an error that states m2t is undefined and then i used the import statement the error I get is You may need an appropriate loader to handle this file type.
|
1

Ok so the solution to this problem happened to be is instead of importing the class via Javascript using the require statement, I had to import the JS Class file via an HTML tag <script src="./leaflet-m2t.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.