+ 1

Cannot import modules installed with NPM

I'm using ES6 import keyword, I have tried this with a various of different packages, but it just doesn't work, for example, I installed a package called moment, by typing npm install moment in my terminal, then I wrote the following code: import moment from 'moment'; let myDate = new Date(); let myCoolDate = moment(myDate).format('LL'); document.body.innerHTML = myCoolDate; This was written inside a scrip tage with the attribute type="module", this however gave me the following error: Uncaught TypeError: Failed to resolve module specifier "dat.gui". Relative references must start with either "/", "./", or "../". So I then tried writing ./moment, hoping that perhaps that may fixed that, but nope, it did not, instead I just got a new error message: GET http://127.0.0.1:56065/moment net::ERR_ABORTED 404 (Not Found) No file called moment exists in my project folder though, only the package.json file, the package-lock.json file, and the node_modules folder, so I don't really understand how this was supposed to work, the instructions say I should just write moment however, so that's what I did, I have never used any packages before so I'm a little confused and if anyone can help it would be really cool.

4th Mar 2021, 7:37 AM
Karak10
Karak10 - avatar
6 Antworten
+ 1
Karak10 Webpack is a javascript module bundler, it builds and bundle a js file from all the entry js files and modules, so that the bundled js file can be executed from html pages without problems.
7th Mar 2021, 1:31 PM
Calviղ
Calviղ - avatar
+ 3
Replace import moment from 'moment' With const moment = require('moment')
5th Mar 2021, 3:30 PM
Stuvan
Stuvan - avatar
0
To use ES6 import package, you should use webpack to bundle your codes.
7th Mar 2021, 5:23 AM
Calviղ
Calviղ - avatar
0
Stuvan I will try that
7th Mar 2021, 11:31 AM
Karak10
Karak10 - avatar
0
Calviղ what's that?
7th Mar 2021, 11:31 AM
Karak10
Karak10 - avatar
0
Calviղ Webpack seems a little complicated to use, can I use parcel instead?
7th Mar 2021, 4:08 PM
Karak10
Karak10 - avatar