因此,在深入研究了 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)
那么为什么不直接使用导入字段呢? …