medikoo / serverless-plugin-reducer

Serverless plugin: Reduce Node.js lambda package so it contains only lambda dependencies
ISC License
14 stars 5 forks source link

`package.include` not supported as expected #7

Closed romaindutartre closed 2 years ago

romaindutartre commented 4 years ago

Hi,

I've come across what I think is a bug in this library. In my serverless project I'm using the object-mapper library and I'm getting an error when packaging the project using serverless package. I believe serverless-plugin-reducer should check whether the required modules are inside devDependencies in order not to . I could just add this tape library to the dependencies of my project and indeed it works but I'm not sure that's the way to go.

The error in question:

Error: Could not resolve "tape" module, required in "/node_modules/object-mapper/test/test.js"
    at /node_modules/serverless-plugin-reducer/node_modules/ncjsm/get-dependencies.js:33:12

Regards, Romain


Relevant parts of relevant files

serverless.yml

plugins:
  - serverless-plugin-reducer
  - serverless-offline

package:
  individually: true
  exclude:
    - node_modules/**
  include:
    - node_modules/object-mapper

package.json (from my project)

   "devDependencies": {
    "@commitlint/cli": "8.3.5",
    "@commitlint/config-conventional": "8.3.4",
    "eslint": "7.0.0",
    "eslint-config-airbnb-base": "14.1.0",
    "eslint-plugin-import": "2.20.2",
    "eslint-plugin-jest": "23.10.0",
    "husky": "4.2.5",
    "jest": "26.0.1",
    "serverless": "^1.67.0",
    "serverless-offline": "^5.12.0",
    "serverless-plugin-reducer": "3.2.3",
    "tape":"5.0.1"
  },
  "dependencies": {
    "@hapi/joi": "^17.1.1",
    "lodash": "4.17.15",
    "object-mapper": "6.2.0",
    "moment": "2.24.0"
  },

object-mapper/package.json

  "dependencies": {},
  "deprecated": false,
  "description": "Copy properties from one object to another.",
  "devDependencies": {
    "debug": "^2.2.0",
    "perf_hooks": "0.0.1",
    "tape": "^4.0.1"
  },

object-mapper/test/test.js

"use strict";

//const om = require('object-mapper')
const om = require('../')
  , test = require('tape')
  // , performance = require('perf_hooks').performance

test('SPLIT with complicated key', function (t) {
  var k = 'abc.def.ghi.j..k\\.l\\\\.m.'
  var expect = ['abc','def','ghi','j','','k.l\\\\','m','']
  var result = om.split(k, '.')
  t.deepEqual(result, expect);
  t.end();
});
medikoo commented 4 years ago

@romaindutartre thanks for report.

Problem is that by pointing node_modules/object-mapper in include section, you've indicated that you want all modules that are there. In this case reducer assumes that you also want node_modules/object-mapper/test/test.js module and hence crashes when not able to resolve it's dependencies.

I think you can easily fix it by putting node_modules/object-mapper/index.js file into include and not whole node_modules/object-mapper folder.

Other question is, why do you actually need to list object-mapper in include section. Wouldn't it be resolved automatically?

romaindutartre commented 4 years ago

The thing is by default we want to exclude every packages because we are using a shared AWS Layer to load all common packages inside our Lambda function but some library we have to manually add to the serverless package like object-mapper, that's why I put it in the include section. I tried removing it from the include section and packaging but the compressed zip doesn't include object-mapper in the node_modules, probably because of the exclude all policy.

Putting node_modules/object-mapper/index.js in the include section oddly only include this specific file in the zip file and doesn't seem to follow the module.exports = require('./src/object-mapper');.

medikoo commented 4 years ago

and doesn't seem to follow the module.exports = require('./src/object-mapper');.

Hmm.. I think the case is that reducer currently supports include instruction only at function level and not at global package level. That definitely should be improved

Workaround you may use, is to put include at function level instead for now.

MrAtheist commented 3 years ago

Having similar issue here as well... my function just needs aws-sdk and nothing else.

+ ./node_modules/.bin/sls deploy --stage dev
Serverless: Packaging service...

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

  Error: Could not resolve "tape" module, required in "./serverless/eb-restart-app/node_modules/isarray/test.js"
      at ./serverless/eb-restart-app/node_modules/serverless-plugin-reducer/node_modules/ncjsm/get-dependencies.js:33:12

#... and then sometimes it spits out another module...

 Error: Could not resolve "benchmark" module, required in "./serverless/eb-restart-app/node_modules/jmespath/perf.js"
      at ./serverless/eb-restart-app/node_modules/serverless-plugin-reducer/node_modules/ncjsm/get-dependencies.js:33:12

I also got this while trying to npm i, not exactly sure what this means...

npm WARN serverless-plugin-reducer@3.2.3 requires a peer of serverless@^1.21 but none is installed. You must install peer dependencies yourself.

Serverless.yml snippet:

package:
  individually: true
  excludeDevDependencies: false

custom:
  includeDependencies:
    enableCaching: true

plugins:
  - serverless-plugin-reducer
  - serverless-plugin-common-excludes
  - serverless-plugin-include-dependencies

functions:
  hello:
    handler: handler.hello
    name: eb-restart-app
    description: EB restart app
    runtime: nodejs12.x
    memorySize: "128"
    timeout: 300

# where handler.hello requires('aws-sdk') and thats it

For the time being and without digging in too much, im taking out serverless-plugin-reducer.

medikoo commented 3 years ago

I also got this while trying to npm i, not exactly sure what this means...

Plugin didn't delcare support for v2 of Serverless Framework. I've just fixed that (published with v3.2.4)

Concerning other errors, it doesn't seem to be related to issue in question (you do not have includes rules per function).

It looks a if a test files of some package (isarray) are required, and they require dev dependencies to be installed, which obviously are not.

medikoo commented 2 years ago

Fixed with https://github.com/medikoo/serverless-plugin-reducer/commit/ae68aada3cc826a32415f4f2227642c0ccba2af0 and released with v4.0.0