How to import moment using js

Question:

Repl link/Link to where the bug appears:

Screenshots, links, or other helpful context:
hi im trying to install moment to use with js and html, but when i install it with the package manager I dont know how to import it, and when I use the shell ‘npm install moment’, i try to use 'import {moment} from ‘moment’, but it doesent work

Hi, @DylanSantwani1, and welcome to the Replit Ask community! It’d help if you gave us the link to your repl so we can better understand the problem.

3 Likes

https://sphericalupsetactivecontent.dylansantwani1.repl.co thanks

1 Like

https://sphericalupsetactivecontent.dylansantwani1.repl.co/__repl
^ for those who need it.

2 Likes

@DylanSantwani1 you seem to be trying to use stuff for Node.js in a HTML/CSS/JS repl. I’d recommend trying the Node.js template, and letting us know how it goes.

1 Like

im trying to use a js script to acess the html, there is no node.js

npm is used for Node, which is why I made the assumption that I did.

1 Like

Oh, I see the problem. It appears that you’re on an HTML, CSS, JS repl which does not have the npm command (native to nodejs applications). If you want to use the moment module, either:

To use the CDN, link a script tag to moment.js before your script:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-CryKbMe7sjSCDPl18jtJI5DR5jtkUWxPXWaLCst6QjH8wxDexfRJic2WRmRXmstr2Y8SxDDWuBO6CQC6IE4KTA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="script.js"></script>

And you shouldn’t need the import keyword with this implementation.

Like @Firepup650 said, you should also suggest using a node app.

Hope this helps! :upside_down_face:

is there a way that I could use the built in packages thing thats in repl

Not for a HTML/CSS/JS Repl. I’d (again) recommend you try a Node.js Repl.

what is a node.js repl?

Node.js is an implementation of javascript for non-website apps. It’s used for backend applications and apps (like VSC).

1 Like

is there any way I can import a package without node.js? it doesent sound like thats what I should use for my project, as it is a webiste app?

also could I do the cdn in the js script?

Yes, check my message where I explained you can use a CDN instead of a package.

1 Like

No… if you mean to import it inside the script…

2 Likes

so theres no way to use the cdn or whatever inside the json script? then how do I use the library?

You import it inside the HTML file.

1 Like

but then how do i use it inside the json file?

All of the variables would be in the global namespace. Which means there’s no need for the import keyword.

2 Likes