I have a simple web page made up of vanilla html, css and js files.
I'd like to be able to use the import { moment } from "moment" syntax in my js files but when I add npm to my proejct, install moment wiht npm install and use that syntax when I run my app using live-server I get an error:
Uncaught TypeError: Failed to resolve module specifier "moment". Relative references must start with either "/", "./", or "../".
Yes, it's possible. Browsers support native ES6 modules for more than 6 years. Here is an example configuration for Express, but it should work with all serious web servers.
index.html:app.js:index.js:package.json:I've used an import map to allow
import moment from 'moment';without relative paths. You have to configure your web server to point to the Moment JavaScript code.If the web server doesn't support a rewrite (as I used for Moment), you have to use the full path in the import map.
Here you can find out more about native ES6 modules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules