V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sub166
V2EX  ›  程序员

如何正确使用 package.json 的 exports 进行路径映射?

  •  
  •   sub166 · 312 天前 · 900 次点击
    这是一个创建于 312 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在我的一个 npm 包a中,目录结构如下:

    dist
    ├── another.cjs
    ├── another.d.ts
    ├── another.mjs
    ├── index.cjs
    ├── index.d.ts
    ├── index.mjs
    └── types.d.ts
    

    apackage.json:

    {
      "name": "a",
      "version": "1.0.0",
      "main": "dist/index.cjs",
      "module": "dist/index.mjs",
      "types": "dist/index.d.ts",
      "exports": {
        ".": {
          "import": "./dist/index.mjs",
          "require": "./dist/index.cjs",
          "types": "./dist/index.d.ts"
        },
        "./another": {
          "import": "./dist/another.mjs",
          "require": "./dist/another.cjs",
          "types": "./dist/another.d.ts"
        },
        "./types": "./dist/types.d.ts"
      },
      "files": [
        "dist"
      ],
      ...
    }
    

    在另一个项目中通过

    import test from 'a/another'
    

    调用 a ,然后报错:

    Cannot find module 'a/another' or its corresponding type declarations.ts(2307)
    

    查询 chatgpt 无果,所以来请教各位大佬

    7 条回复    2023-06-28 09:24:19 +08:00
    zhuisui
        1
    zhuisui  
       312 天前
    写法是不是有问题,参照 https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing types 写错了吧,不确定能不能用。
    另外,是 IDE 报的错,还是 tsc ,还是什么程序?
    ts 版本支持不,要 4.7 吧,node 版本支持不?
    zbinlin
        2
    zbinlin  
       312 天前
    tsconfig.json 配置是怎样的
    sub166
        3
    sub166  
    OP
       312 天前
    @zhuisui 抱歉,信息没提供全。两个项目的 ts 都是 5.1.3 ,node16 ,报错的是 ide

    后面我查了 StackOverflow ,大部分的解决方案是把 tsconfig 的 moduleResolution 改成 NodeNext

    但是在使用其他的 npm 包的时候,moduleResolution 为 Node 时也不会报错
    sub166
        4
    sub166  
    OP
       312 天前
    @zbinlin 原本是 moduleResolution: Node ,改成 NodeNext 就没有问题了。但是使用其他包时不需要特地修改,很奇怪
    zhuisui
        5
    zhuisui  
       312 天前
    IDE 报错了难道 tsc 没报错?有时候可能是 IDE 的问题。
    另外,我倒是没用过 exports 的 typings ,我只用过 typesVersions
    至于,

    > moduleResolution: 'node16' or 'nodenext' for Node.js’ ECMAScript Module Support from TypeScript 4.7 onwards

    exports 是 node16 的特性,用 nodenext 也合理吧。
    zhuisui
        6
    zhuisui  
       312 天前   ❤️ 1
    之前我没从头到尾看这篇文档 https://www.typescriptlang.org/docs/handbook/esm-node.html
    这里面开头就提了,下面的特性都是为 node ECMAScript Module 做的支持,包括使用 moduResolution: NodeNext 和 import conditions 。
    不知道你说的别的 npm 包是什么情况,但这儿用了 mjs 显然应该用文档要求的使用方式。
    另外,文档要求把 types 放到首行,应该是有特殊目的的。
    SmiteChow
        7
    SmiteChow  
       311 天前
    为什么要加最后一句废话生成器,给我整笑了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   777 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:16 · PVG 04:16 · LAX 13:16 · JFK 16:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.