简体   繁体   中英

How can I use functions and libraries from Node.js to JavaScript?

I'm trying to use functions from Node.js to JavaScript but I don't know how to make the connection between them

I'm trying to use the next library in JavaScript

const mongoose = require('mongoose')

But the browser doesn't support "require"

Can any one helps me?

You can't directly use Node.js packages inside a Javascript file without a node environment. But there is a cool way of using Node.js packages inside your Javascript file.

Visit Skypack.dev and use any npm package using the CDN.

For example, mongoose - https://www.skypack.dev/view/mongoose

Write your javascript code like this,

<script type="module">
  import mongoose from 'https://cdn.skypack.dev/mongoose';
</script>

Note: Make sure you add type="module" attribute to your <script> tag.

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM