简体   繁体   English

切换到mjs文件后用import替换require

[英]Replace require with import after switching to mjs file

I switched from js file to mjs file, reading that this is the standard now, but i'm facing a noob problem, i used to have this code in mongoose.js file我从 js 文件切换到 mjs 文件,读到这是现在的标准,但我面临一个菜鸟问题,我曾经在 mongoose.js 文件中有这个代码

const mongoose = require('mongoose')

mongoose.connect('mongodb://127.0.0.1:27017/task-manager-api',{
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false
})

And in the entry point for my app i simply used:在我的应用程序的入口点中,我简单地使用了:

require('./mongoose.js')

And all worked great, but now, with mjs file i need to use import instead of require, and i wish to understand how to do this properly, can someone help me with this one?一切都很好,但是现在,对于 mjs 文件,我需要使用 import 而不是 require,我希望了解如何正确执行此操作,有人可以帮我解决这个问题吗?

You can import the mongoose configuration file using import as follows.您可以使用 import 导入 mongoose 配置文件,如下所示。 import './mongoose.js';导入'./mongoose.js';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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