"npm link"和"npm i <repo>"后无法解析模块("main"和"types"在package.json中是正确的)

After "npm link" and "npm i <repo>" cannot resolve module ("main" and "types" are correctly in package.json)

我的package.json文件

{
  "name": "ts-logger",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "install": "tsc"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/vkrenta/ts-logger.git"
  },
   "devDependencies": {
    "typescript": "^4.0.2",
    "@types/node": "^14.6.4"
  },
  "dependencies": {}
   ...
}

我的tsconfig.json

{
  "compilerOptions": {
     ...
    "outDir": "./dist",
    "target": "es5",
     ...
    "rootDir": "./"
  },
  "exclude": ["node_modules", "dist"]
}

我尝试将其推送到 github 和 运行 npm link

在我创建了另一个 npm 项目之后,然后在 require('ts-logger')import {Logger} from 'ts-logger'node . 我收到 npm 无法解析模块的日志

问题出在 package.json - 我只是交换了 devDependenciesdependencies,因为 typescript 是从 [= 编译代码的依赖项28=] 到 dist 文件夹。

"dependencies": {
    "typescript": "^4.0.2",
    "@types/node": "^14.6.4"
  },
  "devDependencies": {}

UPD

我还用 "target":"es5" 在包中启动了我的 tsconfig.json,但是用 "target":"es2015" 启动了新项目。所以这意味着你的包目标必须比你项目中的目标更新(或相同)(最好是"target":"ESNext")。