[英]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
。
require('ts-logger')
or import {Logger} from 'ts-logger'
and node .
require('ts-logger')
或import {Logger} from 'ts-logger'
和node .
I get logs that npm cannot resolve module
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 - 我只是交换了
devDependencies
和dependencies
,因为打字稿是将代码从 git 编译到 dist 文件夹的依赖项。
"dependencies": {
"typescript": "^4.0.2",
"@types/node": "^14.6.4"
},
"devDependencies": {}
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.