const os = require('os');if (os.type() == 'Windows_NT') {//windows平台}if (os.type() == 'Darwin') {//mac}if (os.type() == 'Linux') {//Linux平台}
window和linux已验证通过,mac待验证
通过当前系统(win,linux)配置文件路径
utils/index.js:
const os = require("os");const path = require("path");// 通过当前系统(win,linux)配置文件路径let setPathByOs = function (fileName = "") { var configPath = ""; if (os.type() == "Windows_NT") { configPath = path.resolve(process.cwd(), fileName); } if (os.type() == "Linux") { configPath = path.resolve(process.execPath, "../", fileName); } return configPath;};module.exports = { setPathByOs,};
app.js中使用:
相对根目录而言
let { setPathByOs } = require("./utils/index.js");const configPath = setPathByOs("./config_node.json");
来源地址:https://blog.csdn.net/qq_40323256/article/details/129800021