我有多项服务
root/
services/
subscriptions/
users/
shared/
httpUtils.js
database.js
node_modules/
package.json
当我使用serverless-offline在本地运行无服务器时,导入工作不会出现问题:
const _ = import('lodash') // module_modules
const database = require('../../shared/database')
However, when I deploy the application won't start because of
error: cannot find module lodash
error: cannot find module ../../shared/database
每个服务是否需要在服务中具有自己的node_modules依赖项?如果我可以从根目录和共享文件夹中的单个node_modules加载依赖项,那就太好了。
对于具有多个服务的项目,要达到此目的,标准做法是什么?
谢谢。
答案 0 :(得分:1)
建议为每个服务使用packages.json,而不要尝试使用跨多个服务共享的单个服务。您可以获取有关package.json和无服务器框架here的使用的其他详细信息。
我尝试了多种技术来共享单个node_modules目录,但是解决方案非常脆弱,随着我引入更多的依赖关系并开始对服务进行版本控制,最终会出问题。
我确实尝试在所有服务之间共享尽可能多的共享资源,但已确定共享单个node_modules目录是不值得的。