Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Problem with Lerna mono-repo and local dependencies #257

Closed
TomiTakussaari opened this issue Oct 19, 2017 · 4 comments
Closed

Problem with Lerna mono-repo and local dependencies #257

TomiTakussaari opened this issue Oct 19, 2017 · 4 comments

Comments

@TomiTakussaari
Copy link

This is a Bug Report

Description

I have private mono-repo project using Lerna, with Serverless app being one package.

Serverless package depends on couple of other private packages, that are sym-linked to serverless-app/node-modules via Lerna bootstrap.

Because not all my dependencies are webpack compatible, I have to use

#serverless.yml
custom:
  webpackIncludeModules: true

#webpack.config.js
const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
    entry: './lambda.js',
    target: 'node',
    externals: [nodeExternals()],
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: __dirname,
                exclude: /node_modules/,
            }
        ]
    },
    output: {
        libraryTarget: 'commonjs',
        path: path.join(__dirname, '.webpack'),
        filename: 'lambda.js'
    },
};

Which leads to serverless-webpack issuing "npm install" command, which then fails because of my Lerna managed local packages are not found from NPM repository..

Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: babel-runtime@6.26.0, my-package@0.0.1, base64url@^2.0.0, url-safe-string@^1.1.0, memoizee@^0.4.11,cookie-parser@1.4.3
(cleaned this up a bit)

  Error --------------------------------------------------

  Command failed: npm install
npm ERR! code ETARGET
npm ERR! notarget No matching version found for my-package@0.0.1

Is there something that can be done ? Skip NPM install somehow ?

Additional Data

"serverless": "1.23.0",
"serverless-webpack": "3.1.2",
"webpack": "3.5.5",
"webpack-node-externals": "1.6.0"
@HyperBrain
Copy link
Member

Hi @TomiTakussaari ,

unfortunately the npm install cannot be skipped because it is needed to get the dependencies reported from webpack. We have to fetch them because otherwise the code would not be functional in the end.

What you might try to do is, to add the private modules to the node-external whitelist parameter and let them be bundled completely in your output. Then the dependency to them should vanish.

Some thoughts:
As I'm not very used to lerna, maybe there could be a possible feature implementation that could solve the problem.
(1) Add a configuration switch under webpackIncludeModules (maybe useLerna: true)
(2) Assemble the package.json for the needed dependencies
(3) Use lerna bootstrap in the package composite dependency folder to fetch and link all lerna dependencies
(4) Continue with npm install after that

However, I have to do some experiments first, to see if such an approach would be feasible.

@TomiTakussaari
Copy link
Author

Unfortunantely, those private modules depended on non-webpack compatible packages, so bundling them did not work.

I also tried this:

  "dependencies": {
    "my-module": "file:../my-module",
   }

That did not work because serverless-webpack copies package.json to .webpack subdir, rendering that relative path invalid.
It looks like that could be a bug ?

In the end, I ended up moving code from private modules to my-serverless-module, adding necessary package.json deps, and then depending on code from my-serverless-module on other packages, using import myStuff from "../my-serverless-module/my-stuff"

Not that pretty, but it seems to work..

@HyperBrain
Copy link
Member

@TomiTakussaari Happy to hear that you found a way that overcomes the problem.

"my-module": "file:../my-module",
It looks like that could be a bug ?

Indeed, when using file references, the reference should be changed by the plugin internally, to work from the dependencies folder, so that it finds the dependency. Good catch 🙌 .

@HyperBrain
Copy link
Member

Created a bug for the file reference issue. Closing this now.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants