Cannot import modules installed with NPM | Sololearn: Learn to code for FREE!


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 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.

3/4/2021 7:37:44 AM


6 Answers

New Answer


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.


Replace import moment from 'moment' With const moment = require('moment')


To use ES6 import package, you should use webpack to bundle your codes.


Stuvan I will try that


Calviղ what's that?


Calviղ Webpack seems a little complicated to use, can I use parcel instead?