小编Chr*_*rdt的帖子

节点 package.json 导出 VS 导入字段

因此,在深入研究了 package.json 文件中声明的 Export 和 Imports 的差异之后,我想知道两者的最佳用例是什么?

例如以下字段:

"name": "node-api",
"exports": {
  ".": "./application.js",
  "./config/*": "./config/*.js",
  "./controllers": "./controllers/index.js",
  "./helpers/*": "./helpers/*.js",
  "./models": "./models/index.js",
  "./routes": "./routes/index.js"
},
"imports": {
  "#config/*": "./config/*.js",
  "#controllers": "./controllers/index.js",
  "#helpers/*": "./helpers/*.js",
  "#models": "./models/index.js",
  "#routes": "./routes/index.js"
}
Run Code Online (Sandbox Code Playgroud)

然后以下每项及其在主 JS 文件中的输出:

import routes from './routes/index.js'; // works
import routes from './routes'; // error - ERR_UNSUPPORTED_DIR_IMPORT
import routes from 'node-api/routes'; // works (with the package name)
import routes from '#routes'; // works (without the package name but need the #)
Run Code Online (Sandbox Code Playgroud)

那么为什么不直接使用导入字段呢? …

javascript node.js package.json

7
推荐指数
1
解决办法
7216
查看次数

标签 统计

javascript ×1

node.js ×1

package.json ×1