使用本地模块,使用本地依赖 [英] Use a local module, using local dependency
问题描述
我正在尝试包含一个本地库,所以我遵循了 2 个教程:如何创建库, 如何使用本地库.所以,我有一个很好的示例库(它有 package.json、index.ts 等),我尝试将它包含在我的主项目中(npm link
工作正常,我可以看到一个符号链接我的库),但我不能从我的主项目中引用它.
I'm trying to include a local library, so i follow theese 2 tutorials: how to create library, how to consume local library. So, i have a nice sample library (it has package.json, index.ts, etc), i try to include it in my main project (npm link
works fine, i can see a symlink to my lib), but i cant refer it from my main project.
import { HelloWorld } from "my-test-lib"; // "Cant find module"
另外,我尝试通过 npm install ../libs/my-test-lib
安装它,但结果是一样的:找不到模块".关于如何将其包含到我的项目中的任何建议?
Also, i tried to install it via npm install ../libs/my-test-lib
, but results are the same: "Cant find module". Any suggestions on how to include it to my project?
PS:我更喜欢 npm install
之类的东西,这样我就可以将此更改提交到存储库(与 npm 链接
相反).
PS: i'd prefer something like npm install
, so i'd be able to commit this change to the repository (as opposing to npm link
).
编辑(为什么不是重复的):在如何将本地模块指定为 npm 包依赖项 不清楚该走哪条路: "bar":"file:../foo/bar"
和由于上述原因,npm link
对我不起作用.
EDIT (why its not a duplicate): in how to specify local modules as npm package dependencies its not clear which way to go: both "bar":"file:../foo/bar"
and npm link
are not working for me for the reason mentioned above.
好的,我已经在 package.json 中尝试了 "preinstall": "npm ln my-test-lib ../libs/my-test-lib"
.然后import { HelloWorld } from "my-test-lib";
.仍然是同样的错误找不到模块".会不会是包裹本身有问题?
Okay, i've tried "preinstall": "npm ln my-test-lib ../libs/my-test-lib"
in the package.json. And then import { HelloWorld } from "my-test-lib";
. Still the same error "Cannot find module". Can it be something wrong with the package itself?
所以是的,这个问题的重点是弄清楚为什么
So yes, the main point of this question is to figure out why
// package.json
"dependencies": {
"my-test-lib": "file:../lib/my-test-lib"
}
// ts file
import { HelloWorld } from "my-test-lib";
不工作.想知道,我该如何调试.
is not working. Wondering, how can i debug that.
推荐答案
import defaultExport from './local-file'
或
import { namedExport } from './local-file'
这篇关于使用本地模块,使用本地依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!