当我使用require commonjs时,一切都正常工作
const { Schema, connect } = require("mongoose");
connect("mongodb://localhost:27017/usersdb", {
useNewUrlParser: true
});
但是当我使用导入es6时出现错误
import { connect } from "mongoose";
connect(
"mongodb://localhost:27017/usersdb",
{
useNewUrlParser: true
}
);
SyntaxError:所请求的模块>'file:/// C:/ Users / Rich%20Warrior / Desktop / mon0022 / node_modules / mongoose / index> .js'不提供 名为“ connect”的出口
如果你这样写
import * as mongoose from "mongoose";
mongoose.connect("mongodb://localhost:27017/usersdb", {
useNewUrlParser: true
});
TypeError:mongoose.connect不是函数
package.json
{
"name": "dur",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon -r esm src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"esm": "^3.2.25",
"express": "^4.17.1",
"i": "^0.3.6",
"mongoose": "^5.7.8",
"nodemon": "^1.19.4"
}
}
我想使用es6模块,是否可以在没有错误的情况下将es6模块与猫鼬一起使用?