【问题标题】:After "npm link" and "npm i <repo>" cannot resolve module ("main" and "types" are correctly in package.json)在“npm link”和“npm i <repo>”之后无法解析模块(“main”和“types”在 package.json 中正确)
【发布时间】:2020-12-27 21:29:12
【问题描述】:

我的 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 无法解析模块的日志

【问题讨论】:

    标签: javascript node.js typescript npm npm-install


    【解决方案1】:

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

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

    更新

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-18
      • 2014-08-24
      • 2017-10-12
      • 1970-01-01
      • 2019-09-23
      • 2021-05-03
      • 2017-04-05
      • 2020-06-08
      相关资源
      最近更新 更多