rishavs / vanillajs-spa

a simple SPA in vanilla js
343 stars 107 forks source link

Cannot import third party modules #1

Closed joshmillgate closed 5 years ago

joshmillgate commented 5 years ago

Hey, I'm playing with your SPA setup and whilst just trying to import modules through any of the JS pages I'm getting the following error:

Uncaught TypeError: Failed to resolve module specifier "uuid". Relative references must start with either "/", "./", or "../".

Can't find anything online about this and I'm new to JS so not sure if I'm overlooking something simple and fundamental.

rishavs commented 5 years ago

Can I take a look at your code? Maybe you can put up a gist/jsfiddle, if you dont have a github project?

From some other discussions like https://github.com/openlayers/openlayers/issues/7766, it seems to be an issue with trying to import something which isn't explicitly exported.

joshmillgate commented 5 years ago

I'm literally just cloning the repo and installing uuid via npm and then adding import uuid from 'uuid';

to the top of App.js

rishavs commented 5 years ago

One way is to simply add;

        <script src="http://wzrd.in/standalone/uuid%2Fv5@latest"></script>
        <script>
            console.log(uuidv5('http://example.com/hello', uuidv5.URL)); // -> v5 UUID
        </script> 

in your index.html

I think importing browser specific js modules should work, but they need to end in .js and have a module.exports in them. For example, I took https://raw.githubusercontent.com/pnegri/uuid-js/master/lib/uuid.js

and changed the last line to export default UUIDjs; and added an import in my app.js as; import uuid from './services/uuid.js'. This worked fine.

You may also consider a browser side bundler like browserify.

I don't think you should call any module from npm. You wouldn't be able to use it unless you are serving these files from a static npm server.

I haven't really spent much time on this kind of issue but I am sure more elegant ways of doing fixing it should be there.

rishavs commented 5 years ago

@joshmillgate I hope you have been unblocked. I am closing this issue for now.