简体   繁体   English

在我的 npm 包的 package.json 中我需要什么才能将其安装到另一个项目?

[英]What do I need in the package.json of my npm package, to install it to another project?

I'm trying to make a small-ish react component that can be used in other projects.我正在尝试制作一个可以在其他项目中使用的小型反应组件。 This is what I have in my package.json so far:到目前为止,这是我的 package.json 中的内容:


{
  "name": "my-package",
  "version": "0.1.1",
  "private": true,
  "main": "./dist/lib.umd.js",
  "module": "./dist/lib.module.js",
  "source": "src/components/MyPackage/index.js",
  "exports": {
    ".": "./dist/lib.js",
    "./styles.css": "./dist/lib.css"
  },
  "dependencies": {
    "classnames": "^2.3.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  },
  "devDependencies": {
    "microbundle": "^0.15.0",
    "react-scripts": "5.0.1"
  },
  "peerDependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "microbundle --jsx React.createElement --jsxFragment React.Fragment --jsxImportSource react",
    "watch": "microbundle watch --jsx React.createElement --jsxFragment React.Fragment --jsxImportSource react"
  },
  "eslintConfig": {
    "extends": [
      "react-app"
    ]
  },
  "files": [
    "dist"
  ],
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

The exports fields, module and source are for microbundle to know what and where to bundle. exports字段、 modulesource是为了让 microbundle 知道要捆绑什么以及在哪里捆绑。

This repo is in a private bitbucket repository, and I've installed it like this: npm i git+ssh://git@bitbucket.org:myuser/mypackage.git , but it doesn't let me import the package.这个 repo 在一个私有的 bitbucket 存储库中,我已经像这样安装它: npm i git+ssh://git@bitbucket.org:myuser/mypackage.git ,但它不允许我导入包。 When looking in my node_modules/mypackage , I only see the package.json, and the README, but no js files.在查看我的node_modules/mypackage时,我只看到 package.json 和 README,但没有看到 js 文件。

The weird thing is this package works when I import it locally using npm link .奇怪的是,当我使用npm link在本地导入它时,这个包可以工作。

  "publishConfig": {
      "registry": "https://registry.npmjs.org"
  }

More details can be foundhere更多细节可以在这里找到

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

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