gpt4 book ai didi

node.js - 如何配置 __dirname 指向 Node 中的项目目录?

转载 作者:行者123 更新时间:2023-12-03 22:16:42 24 4
gpt4 key购买 nike

我正在向我的 Node 项目添加一个 Sequelize ,它添加了以下 index.js 文件作为设置的一部分,但是当我运行该项目时,__dirname 解析为我的 C: 目录而不是项目根文件夹
C:\工作区\我的应用程序。如何配置它以便它解析到我的项目根目录?

index.js 文件

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

包.json
{
"name": "my-app-model",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"format": "prettier --write \"src/**/*.{js,jsx,ts,tsx}\"",
"start": "next start",
"type-check": "tsc"
},
"dependencies": {
"@material-ui/core": "^4.8.3",
"@material-ui/lab": "4.0.0-alpha.39",
"mysql": "^2.18.1",
"mysql2": "^2.1.0",
"next": "9.1.7",
"prettier": "^1.19.1",
"react": "16.12.0",
"react-dom": "16.12.0",
"sequelize": "^5.21.7",
"sequelize-cli": "^5.5.1"
},
"devDependencies": {
"@types/node": "^13.1.6",
"@types/react": "^16.9.17",
"@types/react-dom": "^16.9.4",
"typescript": "^3.7.4"
}
}

最佳答案

我相信直接的解决方案是用以下内容替换 __dirname:

const modelsDir = path.resolve(process.cwd(), 'path/to/models')
fs.readdirSync(modelsDir)
.filter(....
__dirname 以我们期望的方式工作是 next.js 上的 known issue :

I expected __dirname to be the absolute path of the file in which getStaticProps is being executed, but instead it is incorrectly resolving as /.


这是重新表述为功能请求的问题: https://github.com/vercel/next.js/issues/8251
关于这如何为我体现的一点背景,以防这有助于某人找到此解决方案:
我使用的是 mac,但我遇到了非常相似的问题。我正在使用 sequelize 设置 next.js 应用程序。在我运行 sequelize-cli 生成你上面引用的 models/index.js 后,我第一次看到这个错误:
Error: Cannot find module '/Applications'
Require stack:
- /Users/username/src/mariner-next/node_modules/sequelize/lib/sequelize.js
- /Users/username/src/mariner-next/node_modules/sequelize/index.js
- /Users/username/src/mariner-next/.next/server/static/development/pages/api/graphql.js
- /Users/username/src/mariner-next/node_modules/next/dist/next-server/server/next-server.js
- /Users/username/src/mariner-next/node_modules/next/dist/server/next.js
- /Users/username/src/mariner-next/node_modules/next/dist/server/lib/start-server.js
- /Users/username/src/mariner-next/node_modules/next/dist/cli/next-dev.js
- /Users/username/src/mariner-next/node_modules/next/dist/bin/next
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:981:15)
at Function.Module._load (internal/modules/cjs/loader.js:863:27)
at Module.require (internal/modules/cjs/loader.js:1043:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Sequelize.import (/Users/username/src/mariner-next/node_modules/sequelize/lib/sequelize.js:481:62)
at eval (webpack-internal:///./tnr/lib/models/index.js:60:36)
at Array.forEach (<anonymous>)
at Module.eval (webpack-internal:///./tnr/lib/models/index.js:58:4)
经过一番摸索,我意识到 __dirname 设置为 "/",因此 fs.readdrSync(__dirname) 返回的是我的根目录列表。

关于node.js - 如何配置 __dirname 指向 Node 中的项目目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61432426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com