有什么方法可以限制nodejs中包内的导出吗?



NodeJS中是否有一种方法或模式可以仅在包的模块中共享函数,而不允许从另一个包共享它们?

例如,如果包 A 有 file1.js、file2.js 和 index.js. index.js则使用 file1 和 file2 中的函数。

包 B 使用包 A。似乎从 file1 和 file2 导出的所有模块也可用于包 B。是否可以将其限制为仅从包 A 的索引.js导出的那些?

简而言之,是否支持受保护的范围之类的东西?

正如AZ_所说:只导出你想要导出的内容。

例:

// file1
export const foo = () => { console.log("foo"); }
// file2
import {foo} from "./file1"
export const bar = () => { 
foo();
console.log("bar"); 
}
// index
import {bar} from "./bar"
// the package will only export bar
export { bar }   

最新更新