简体   繁体   English

在“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)

My package.json file我的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": {}
   ...
}

My tsconfig.json我的tsconfig.json

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

I tried to push it to github and run npm link .我试图将它推送到 github 并运行npm link

After I created another npm project, then after require('ts-logger') or import {Logger} from 'ts-logger' and node . 在我创建了另一个 npm 项目之后,在require('ts-logger')import {Logger} from 'ts-logger'node . I get logs that npm cannot resolve module 我收到 npm 无法解析模块的日志

The problem was in package.json - I just swaped devDependencies and dependencies , because typescript is dependency that compiles code from git to dist folder.问题出在package.json - 我只是交换了devDependenciesdependencies ,因为打字稿是将代码从 git 编译到 dist 文件夹的依赖项。

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

UPD UPD

Also I initiated my tsconfig.json in package with "target":"es5" , but new project with "target":"es2015" .此外,我使用"target":"es5"启动了我的tsconfig.json包,但使用"target":"es2015"启动了新项目。 So it means that your package target must be newer (or same) than target in your project (the best is "target":"ESNext" ).因此,这意味着您的包目标必须比项目中的目标更新(或相同)(最好是"target":"ESNext" )。

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

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