【发布时间】:2021-06-01 09:34:18
【问题描述】:
我已经建立了一个示例项目来演示这个问题:
https://github.com/garethrbrown/node-share-code
在这个例子中有两个项目,example-api(一个迷你快递项目)和example-shared(一个类库),都使用Node JS / TypeScript .我希望 example-api 能够使用 example-shared 中的类以避免代码重复。遵循this 示例后,我在example-api 中引用了package.json 中的示例共享项目。
"dependencies": {
"example-shared": "file:..\\example-shared",
"express": "^4.17.1"
}
完成此操作并在运行 npm install 之后,VSCode 中的智能感知会从示例共享项目中看到 ApiClass 并协助导入。
然后我可以通过 NPM 使用构建命令 tsc --build 运行,它成功了。
我还可以看到在example-api node_modules目录下已经创建了sym链接。
但是,当我尝试使用 example-api 下的 npm start 脚本运行 example-api 项目时,出现如下错误:
Error: Cannot find module 'example-shared/apiClass'
Require stack:
...
code: 'MODULE_NOT_FOUND',
requireStack: [
...
]
我曾尝试从不同位置运行命令,例如 here 所述,但到目前为止没有运气。
我正在使用当前稳定版本的 Node (14+) 和 NPM (7+)。
我不想通过 NPM 或 git 存储库共享,因为我觉得这会减慢开发速度。
我做错了什么?
【问题讨论】:
标签: node.js typescript nodes node-modules