【问题标题】:Any way to restrict export within a package in nodejs?有什么方法可以限制 nodejs 包中的导出?
【发布时间】:2020-05-18 11:07:58
【问题描述】:

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

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

包B使用包A。好像从file1和file2导出的所有模块都对包B可用。是否只能限制从包A的index.js导出的模块?

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

【问题讨论】:

  • 仅从PackageAindex.js 导出所需的内容。

标签: node.js npm package


【解决方案1】:

正如 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 }   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    相关资源
    最近更新 更多