无服务器框架 - 在多个服务之间共享代码

Serverless Framework - Share code between multiple services

我有多项服务

root/
    services/
        subscriptions/
        users/
    shared/
        httpUtils.js
        database.js
    node_modules/
    package.json

当我在本地使用无服务器离线 运行 无服务器时,我的导入工作没有问题:

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 加载依赖项,那就太好了。

为具有多个服务的项目完成此操作的标准做法是什么?

谢谢。

建议您为每个服务使用一个 packages.json,而不是尝试使用跨多个服务共享的单个服务。您可以获得有关使用 package.json 和无服务器框架 here.

的更多详细信息

我尝试过各种技术来共享单个 node_modules 目录,但解决方案很脆弱,并且随着我引入更多依赖项并开始对服务进行版本控制,最终会出现问题。

我确实尝试在我的服务中尽可能多地共享,但我确定不值得为共享单个 node_modules 目录付出努力。