Mongoose/MongoDB 在使用 NodeJS 时抛出“错误 [ERR_REQUIRE_ESM]:不支持 ES 模块 /node_modules/mongoose/index.js 的 require()”

问题描述 投票:0回答:1

我正在尝试创建一个网站,通过 mongoDB 存储不同机器的状态值,但每当我尝试运行 index.js 文件时,它都会抛出

Error [ERR_REQUIRE_ESM]: require() of ES Module file:///C:/Users/mimic/OneDrive/Documents/GitHub/TAMU_Laundry/node_modules/mongoose/index.js not supported.
,尽管 package.json 中没有
"type": "module"
。在另一篇论坛帖子中提到 node-fetch 仅与 ES 兼容,但我尝试对此进行故障排除,但没有任何效果,因此不确定这是否是原因。这是我第一次进行 Web 开发,也是第一次使用 mongoDB,因此我们将不胜感激!

主包.json:

{
  "name": "tamu_laundry",
  "version": "1.0.0",
  "description": "",
  "main": "node index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.1.1",
    "express": "^4.21.0",
    "jsonwebtoken": "^9.0.2",
    "mongoose": "^8.7.0",
    "node-fetch-native": "^1.6.4",
    "node-schedule": "^2.1.1",
    "twilio": "^5.3.2"
  },
  "devDependencies": {
    "nodemon": "^3.1.7"
  }
}

主index.js:


const express = require('express');
const connectDB = require('./config/db');
const authRoutes = require('./routes/auth');
const claimRoutes = require('./routes/claims');
const seedMachines = require('./config/seed');

const app = express();
const PORT = 3000;

connectDB();
app.use(express.json());

app.use('/auth', authRoutes);
app.use('/api', claimRoutes);

app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});
seedMachines()

猫鼬包.json

{
  "name": "mongoose",
  "description": "Mongoose MongoDB ODM",
  "version": "8.7.0",
  "author": "Guillermo Rauch <[email protected]>",
  "keywords": [
    "mongodb",
    "document",
    "model",
    "schema",
    "database",
    "odm",
    "data",
    "datastore",
    "query",
    "nosql",
    "orm",
    "db"
  ],
  "license": "MIT",
  "dependencies": {
    "bson": "^6.7.0",
    "kareem": "2.6.3",
    "mongodb": "6.9.0",
    "mpath": "0.9.0",
    "mquery": "5.0.0",
    "ms": "2.1.3",
    "sift": "17.1.3"
  },
  "devDependencies": {
    "@babel/core": "7.24.7",
    "@babel/preset-env": "7.25.4",
    "@typescript-eslint/eslint-plugin": "^8.4.0",
    "@typescript-eslint/parser": "^8.4.0",
    "acquit": "1.3.0",
    "acquit-ignore": "0.2.1",
    "acquit-require": "0.1.1",
    "assert-browserify": "2.0.0",
    "axios": "1.1.3",
    "babel-loader": "8.2.5",
    "broken-link-checker": "^0.7.8",
    "buffer": "^5.6.0",
    "cheerio": "1.0.0",
    "crypto-browserify": "3.12.0",
    "dotenv": "16.4.5",
    "dox": "1.0.0",
    "eslint": "8.57.0",
    "eslint-plugin-markdown": "^5.0.0",
    "eslint-plugin-mocha-no-only": "1.2.0",
    "express": "^4.19.2",
    "fs-extra": "~11.2.0",
    "highlight.js": "11.10.0",
    "lodash.isequal": "4.5.0",
    "lodash.isequalwith": "4.4.0",
    "markdownlint-cli2": "^0.13.0",
    "marked": "14.1.0",
    "mkdirp": "^3.0.1",
    "mocha": "10.7.3",
    "moment": "2.30.1",
    "mongodb-memory-server": "10.0.0",
    "ncp": "^2.0.0",
    "nyc": "15.1.0",
    "pug": "3.0.3",
    "q": "1.5.1",
    "sinon": "18.0.0",
    "stream-browserify": "3.0.0",
    "tsd": "0.31.1",
    "typescript": "5.5.4",
    "uuid": "10.0.0",
    "webpack": "5.94.0"
  },
  "directories": {
    "lib": "./lib/mongoose"
  },
  "scripts": {
    "docs:clean": "npm run docs:clean:stable",
    "docs:clean:stable": "rimraf index.html && rimraf -rf ./docs/*.html  && rimraf -rf ./docs/api && rimraf -rf ./docs/tutorials/*.html && rimraf -rf ./docs/typescript/*.html && rimraf -rf ./docs/*.html && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
    "docs:clean:5x": "rimraf index.html && rimraf -rf ./docs/5.x && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
    "docs:clean:6x": "rimraf index.html && rimraf -rf ./docs/6.x && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
    "docs:copy:tmp": "mkdirp ./tmp/docs/css && mkdirp ./tmp/docs/js && mkdirp ./tmp/docs/images && mkdirp ./tmp/docs/tutorials && mkdirp ./tmp/docs/typescript && mkdirp ./tmp/docs/api && ncp ./docs/css ./tmp/docs/css --filter=.css$ && ncp ./docs/js ./tmp/docs/js --filter=.js$ && ncp ./docs/images ./tmp/docs/images && ncp ./docs/tutorials ./tmp/docs/tutorials && ncp ./docs/typescript ./tmp/docs/typescript && ncp ./docs/api ./tmp/docs/api && cp index.html ./tmp && cp docs/*.html ./tmp/docs/",
    "docs:copy:tmp:5x": "rimraf ./docs/5.x && ncp ./tmp ./docs/5.x",
    "docs:copy:tmp:6x": "rimraf ./docs/6.x && ncp ./tmp ./docs/6.x",
    "docs:generate": "node ./scripts/website.js",
    "docs:generate:sponsorData": "node ./scripts/loadSponsorData.js",
    "docs:test": "npm run docs:generate",
    "docs:view": "node ./scripts/static.js",
    "docs:prepare:publish:stable": "git checkout gh-pages && git merge master && npm run docs:generate",
    "docs:prepare:publish:5x": "git checkout 5.x && git merge 5.x && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && git checkout gh-pages && npm run docs:copy:tmp:5x",
    "docs:prepare:publish:6x": "git checkout 6.x && git merge 6.x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && mv ./docs/6.x ./tmp && git checkout gh-pages && npm run docs:copy:tmp:6x",
    "docs:prepare:publish:7x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rimraf ./docs/7.x && mv ./tmp ./docs/7.x",
    "docs:check-links": "blc http://127.0.0.1:8089 -ro",
    "lint": "eslint .",
    "lint-js": "eslint . --ext .js --ext .cjs",
    "lint-ts": "eslint . --ext .ts",
    "lint-md": "markdownlint-cli2 \"**/*.md\"",
    "build-browser": "(rm ./dist/* || true) && node ./scripts/build-browser.js",
    "prepublishOnly": "npm run build-browser",
    "release": "git pull && git push origin master --tags && npm publish",
    "release-5x": "git pull origin 5.x && git push origin 5.x && git push origin 5.x --tags && npm publish --tag 5x",
    "release-6x": "git pull origin 6.x && git push origin 6.x && git push origin 6.x --tags && npm publish --tag 6x",
    "mongo": "node ./tools/repl.js",
    "publish-7x": "npm publish --tag 7x",
    "test": "mocha --exit ./test/*.test.js",
    "test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys --allow-write ./test/deno.js",
    "test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
    "test-tsd": "node ./test/types/check-types-filename && tsd",
    "tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
    "test-coverage": "nyc --reporter=html --reporter=text npm test",
    "ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check"
  },
  "main": "./index.js",
  "types": "./types/index.d.ts",
  "engines": {
    "node": ">=16.20.1"
  },
  "bugs": {
    "url": "https://github.com/Automattic/mongoose/issues/new"
  },
  "repository": {
    "type": "git",
    "url": "git://github.com/Automattic/mongoose.git"
  },
  "homepage": "https://mongoosejs.com",
  "browser": "./dist/browser.umd.js",
  "config": {
    "mongodbMemoryServer": {
      "disablePostinstall": true
    }
  },
  "funding": {
    "type": "opencollective",
    "url": "https://opencollective.com/mongoose"
  },
  "tsd": {
    "directory": "test/types",
    "compilerOptions": {
      "esModuleInterop": false,
      "strict": true,
      "allowSyntheticDefaultImports": true,
      "strictPropertyInitialization": false,
      "noImplicitAny": false,
      "strictNullChecks": true,
      "module": "commonjs",
      "target": "ES2017"
    }
  }
}

猫鼬index.js:

/**
 * Export lib/mongoose
 *
 */

'use strict';

const mongoose = require('./lib/');

module.exports = mongoose;
module.exports.default = mongoose;
module.exports.mongoose = mongoose;

// Re-export for ESM support
module.exports.cast = mongoose.cast;
module.exports.STATES = mongoose.STATES;
module.exports.setDriver = mongoose.setDriver;
module.exports.set = mongoose.set;
module.exports.get = mongoose.get;
module.exports.createConnection = mongoose.createConnection;
module.exports.connect = mongoose.connect;
module.exports.disconnect = mongoose.disconnect;
module.exports.startSession = mongoose.startSession;
module.exports.pluralize = mongoose.pluralize;
module.exports.model = mongoose.model;
module.exports.deleteModel = mongoose.deleteModel;
module.exports.modelNames = mongoose.modelNames;
module.exports.plugin = mongoose.plugin;
module.exports.connections = mongoose.connections;
module.exports.version = mongoose.version;
module.exports.Aggregate = mongoose.Aggregate;
module.exports.Mongoose = mongoose.Mongoose;
module.exports.Schema = mongoose.Schema;
module.exports.SchemaType = mongoose.SchemaType;
module.exports.SchemaTypes = mongoose.SchemaTypes;
module.exports.VirtualType = mongoose.VirtualType;
module.exports.Types = mongoose.Types;
module.exports.Query = mongoose.Query;
module.exports.Model = mongoose.Model;
module.exports.Document = mongoose.Document;
module.exports.ObjectId = mongoose.ObjectId;
module.exports.isValidObjectId = mongoose.isValidObjectId;
module.exports.isObjectIdOrHexString = mongoose.isObjectIdOrHexString;
module.exports.syncIndexes = mongoose.syncIndexes;
module.exports.Decimal128 = mongoose.Decimal128;
module.exports.Mixed = mongoose.Mixed;
module.exports.Date = mongoose.Date;
module.exports.Number = mongoose.Number;
module.exports.Error = mongoose.Error;
module.exports.MongooseError = mongoose.MongooseError;
module.exports.now = mongoose.now;
module.exports.CastError = mongoose.CastError;
module.exports.SchemaTypeOptions = mongoose.SchemaTypeOptions;
module.exports.mongo = mongoose.mongo;
module.exports.mquery = mongoose.mquery;
module.exports.sanitizeFilter = mongoose.sanitizeFilter;
module.exports.trusted = mongoose.trusted;
module.exports.skipMiddlewareFunction = mongoose.skipMiddlewareFunction;
module.exports.overwriteMiddlewareResult = mongoose.overwriteMiddlewareResult;

// The following properties are not exported using ESM because `setDriver()` can mutate these
// module.exports.connection = mongoose.connection;
// module.exports.Collection = mongoose.Collection;
// module.exports.Connection = mongoose.Connection;

我尝试将 index.js 文件转换为 ES,但错误只是传递到另一个 .js 文件,抛出相同的错误。

javascript mongodb mongoose
1个回答
0
投票

我的一个文件不小心使用了ES格式,已解决!

© www.soinside.com 2019 - 2024. All rights reserved.