javascript – How to use libraries with npm?


I’m new to javascript and would like to include moment.js (or any other library) in a project but I don’t know how to set up my project so that I can import from the library.
I’ve set up my html like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Using Libraries</title>
  </head>
  <body>
    <script src="script.js" type="module"></script>
  </body>
</html>

and my script:

import moment from "moment";

console.log(moment());

then I ran

npm install moment --save 

in the terminal and got my node_modules, package.json, package-lock.json. When I run my html I still get a CORS error.

What am I doing wrong?
How would I go about using moment.js (or any other library) without npm or any package manager?

In a tutorial I did with webpack running the command in terminal did the trick, but without webpack I have no clue how to import libraries.
Any suggestions, maybe a good tutorial on the topic?



Source link

Leave a Comment