【问题标题】:module has no exports on my new npm package that I publish模块在我发布的新 npm 包上没有导出
【发布时间】:2022-02-13 02:49:50
【问题描述】:

我最近刚刚尝试创建新的 npm 包。在我的导入中,它说module has no exports。我已将组件导出到 dist 文件夹内的 index.js 中。 这是我的项目结构


- dist
  - components
    - MyComponent.tsx
  - index.js

package.json

编译前我的index.js

import MyComponent from "./components/MyComponent";

export {MyComponent}

我的package.json

{
  "name": "my-test-npm",
  "homepage": "https://github.com/ganinw13120/my-test-npm",
  "main": "dist/index.js",
  "module": "dist/index.js",
  "bin": "dist/index.js",
  "version": "0.1.13",
  "dependencies": {
    "@babel/polyfill": "^7.12.1",
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "@types/jest": "^26.0.24",
    "@types/node": "^12.20.37",
    "@types/react": "^17.0.37",
    "@types/react-dom": "^17.0.11",
    "@types/uuid": "^8.3.3",
    "bignumber.js": "^9.0.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "styled-components": "^5.3.3",
    "typescript": "^4.5.3",
    "uuid": "^8.3.2",
    "web-vitals": "^1.1.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "rm -rf dist && NODE_ENV=production babel src/lib --out-dir dist --copy-files",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/cli": "^7.16.8",
    "@babel/core": "^7.16.7",
    "@babel/preset-env": "^7.16.8",
    "@types/styled-components": "^5.1.17"
  },
  "files": [
    "dist",
    "package.json"
  ]
}

在我的错误中,它还说Field 'browser' doesn't contain a valid alias configuration 然后是dist 文件夹上的我的组件路径。似乎在 dist 文件夹中找不到我的组件,该文件夹已包含在内。

【问题讨论】:

  • 你解决了这个问题吗? @Gan Mongklakorn
  • 不,还在寻找。你有什么线索吗?

标签: reactjs typescript npm package.json


【解决方案1】:

我最近遇到了类似的问题。我检查了我的项目配置和你的配置,你可以尝试在npm包源代码中添加配置package.json

"type": "module",

我导入的公共 npm 包组件是这样创建的:

import { ResponseHandler } from "js-wheel/dist/index";

并像这样在 npm 包源 index.ts 中导出:

import {ResponseHandler} from "./net/rest/ResponseHandler";

export {
    ResponseHandler
}

我花了几天的时间来解决这个问题,终于成功了。

【讨论】:

    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 2018-09-26
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多