简体   繁体   English

Typescript - 如何让 package 用户导入 dist 文件夹作为 package 名称?

[英]Typescript - How to make package users import dist folder as the package name?

I'm trying to write a package using typescript, this package contains some declarations that should be imported and used by user.我正在尝试使用 typescript 编写 package,此 package 包含一些应由用户导入和使用的声明。 The problem is I have a src and a dist folder generated from src .问题是我有一个src和一个从src生成的dist文件夹。 When I publish the package I can not import it as @scope/package-name instead of that I should import the definitions from @scope/package-name/dist .当我发布 package 时,我无法将其导入为@scope/package-name而不是应该从@scope/package-name/dist导入定义。 How can I import from the dist folder using the name of the package?如何使用 package 的名称从 dist 文件夹导入? @scope/package-name

You should point to the dist folder in your package.json file.您应该指向 package.json 文件中的 dist 文件夹。

Main 主要的

The main field is a module ID that is the primary entry point to your program.主要字段是模块 ID,它是程序的主要入口点。 That is, if your package is named foo, and a user installs it, and then does import * from "foo" , then your main module's exports object will be returned.也就是说,如果您的 package 被命名为 foo,并且用户安装了它,然后import * from "foo" ,那么您的主模块的导出 object 将被返回。

This should be a module relative to the root of your package folder.这应该是相对于 package 文件夹的根目录的模块。

For most modules, it makes the most sense to have a main script and often not much else.对于大多数模块来说,拥有一个主脚本是最有意义的,而其他的通常不多。

If main is not set it defaults to index.js in the packages root folder.如果 main 未设置,则默认为包根文件夹中的 index.js。

In your case, include this in your package.json:在您的情况下,请将其包含在您的 package.json 中:

"main": "dist/index.js",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM