在使用 Yarn Plug'n'Play 升级到 Yarn 3.0 后(抛出 node_modules),我在启动 Node.js 时开始遇到文件名解析问题。
成功启动后server.js
,它会尝试解析项目中第一个导入的文件,称为util.js
,但路径解析失败,除非我明确添加.js
到文件名。我宁愿不必更新项目范围内的所有路径。
我可以让 Yarn 和 Node.js
自动解析文件扩展名吗?
纱线3.1.0
,节点17.0.1
。
yarn node server.js # Automatically uses Yarn's .pnp.cjs, right?
// server.js excerpt
import util from "/tools/util"; // <-- No worky
import util from "/tools/util.js"; // <-- ✅ Worky
错误信息:
/Users/me/repo/.pnp.cjs:22839
return Object.defineProperties(new Error(message), {
^
Error: Qualified path resolution failed - none of those files can be found on the disk.
Source path: /Users/me/repo/server/util
Not found: /Users/me/repo/server/util
at internalTools_makeError (/Users/me/repo/.pnp.cjs:22839:34)
at resolveUnqualified (/Users/me/repo/.pnp.cjs:24238:13)
at resolveRequest (/Users/me/repo/.pnp.cjs:24271:14)
at Object.resolveRequest (/Users/me/repo/.pnp.cjs:24343:26)
at resolve$1 (file:///Users/me/repo/.pnp.loader.mjs:205:25)
at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
at link (node:internal/modules/esm/module_job:75:36)