是否可以从“main”指向的其他内容导入?
在安装到 node_modules 中的库中,我将 main 设置为
lib/index.js
所以(使用es2015导入 - 源是ts编译的js),我可以这样做
import { FunctionA, FunctionB } from 'MyTestLibrary';
这是可行的,因为这些函数是在 libs 下的 index.js 内部导出的。
我在导出 functionC 和 functionD 的目录中还有一个索引,结构在这里
/lib/otherdir/index.js
所以如果我像这样导入
import { FunctionC, FunctionD } from 'MyTestLibrary/otherdir';
我的 IDE 没有提示,但运行应用程序时我得到了
Cannot find module MyTestLibrary/otherdir
一切都按应有的方式导出。
最佳答案
您可以像这样直接访问该目录:
import { FunctionC, FunctionD } from 'MyTestLibrary/lib/otherdir'
关于node.js - node_modules 从 "main"(package.json) 中指定的目录导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50404994/