gpt4 book ai didi

node.js - 我有以下 node.js 代码,这些代码在多个 .js 文件中重复 -

转载 作者:太空宇宙 更新时间:2023-11-03 22:07:33 26 4
gpt4 key购买 nike

const c1 = require('package1')
const c2 = require('package2')
const c3 = require('package3')
const c4 = require('package4')
const c5 = require('package5')
const c6 = require('package6')
const iodata;

function foo()
{
let secretpath = "../abc/def/file.txt"
let filedata = await readCredentialsFromFile(secretPath);
iodata = filedata[0]; // iodata initialized to be used in other files.
return filedata;
}

如何重构它,使我只需在每个文件中编写一个 require 语句?

最佳答案

你的意思是这样吗?

commonDeps.js

exports.c1 = require("package1")
exports.c2 = require("package2")
exports.c3 = require("package3")

a.js

const cc = require("./commonDeps");
const c1 = cc.c1;

b.js

const c2 = require("./commonDeps").c2;

c.js

import {c3} from "./commonDeps"

关于node.js - 我有以下 node.js 代码,这些代码在多个 .js 文件中重复 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51389288/

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