gpt4 book ai didi

node.js - 有什么简单的方法可以轻松解决不在您的node_modules/文件夹中的 "require"模块?

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:47 25 4
gpt4 key购买 nike

我有一些 javascript 文件,它们导出我的应用程序中使用的一些常用函数。我希望能够通过编写 var example = require('example') 而不是 var example = require('../local_modules/example')

来导入这些文件

如何在不将其添加到 npm 注册表的情况下完成此操作?他们必须保持隐私。

最佳答案

一种方法是创建路径映射。例如

var pathmapping = {
example: '../local_modules/example'
...other pathes...
}

然后通过以下方式访问它们

require(pathmapping[example])

关于node.js - 有什么简单的方法可以轻松解决不在您的node_modules/文件夹中的 "require"模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43193795/

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