Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Error: Can't resolve 'source-map-support/register' in 'libs/response-lib.js' #45

Open
chrisanderss0n opened this issue Jul 25, 2019 · 17 comments
Labels

Comments

@chrisanderss0n
Copy link

Following the instructions to upgrade to serverless-bundle I get the following error when trying to deploy with serverless deploy

ERROR in /project/libs/response-lib.js
Module not found: Error: Can't resolve 'source-map-support/register' in '/project/libs'
 @ /project/libs/response-lib.js 1:0-37
 @ /project/serverless-project/createCard.js
Unhandled rejection Error: Webpack compilation error, see above
    at _.forEach.compileStats (/project/serverless-project/node_modules/serverless-webpack/lib/compile.js:38:19)
    at arrayEach (/project/serverless-project/node_modules/lodash/lodash.js:516:11)
    at Function.forEach (/project/serverless-project/node_modules/lodash/lodash.js:9342:14)
    at BbPromise.fromCallback.then.stats (/project/serverless-project/node_modules/serverless-webpack/lib/compile.js:31:11)
    at tryCatcher (/project/serverless-project/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/project/serverless-project/node_modules/bluebird/js/release/promise.js:517:31)
    at Promise._settlePromise (/project/serverless-project/node_modules/bluebird/js/release/promise.js:574:18)
    at Promise._settlePromise0 (/project/serverless-project/node_modules/bluebird/js/release/promise.js:619:10)
    at Promise._settlePromises (/project/serverless-project/node_modules/bluebird/js/release/promise.js:699:18)
    at Promise._fulfill (/project/serverless-project/node_modules/bluebird/js/release/promise.js:643:18)
    at /project/serverless-project/node_modules/bluebird/js/release/nodeback.js:42:21
    at finalCallback (/project/serverless-project/node_modules/webpack/lib/MultiCompiler.js:247:12)
    at runWithDependencies.err (/project/serverless-project/node_modules/webpack/lib/MultiCompiler.js:270:6)
    at done (/project/serverless-project/node_modules/neo-async/async.js:2931:13)
    at runCompilers (/project/serverless-project/node_modules/webpack/lib/MultiCompiler.js:174:48)
    at err (/project/serverless-project/node_modules/webpack/lib/MultiCompiler.js:181:7)
    at compiler.run (/project/serverless-project/node_modules/webpack/lib/MultiCompiler.js:263:7)
    at finalCallback (/project/serverless-project/node_modules/webpack/lib/Compiler.js:253:39)
    at hooks.done.callAsync.err (/project/serverless-project/node_modules/webpack/lib/Compiler.js:269:13)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/project/serverless-project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
    at AsyncSeriesHook.lazyCompileHook (/project/serverless-project/node_modules/tapable/lib/Hook.js:154:20)
    at onCompiled (/project/serverless-project/node_modules/webpack/lib/Compiler.js:267:21)
    at hooks.afterCompile.callAsync.err (/project/serverless-project/node_modules/webpack/lib/Compiler.js:664:15)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/project/serverless-project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/project/serverless-project/node_modules/tapable/lib/Hook.js:154:20)
    at compilation.seal.err (/project/serverless-project/node_modules/webpack/lib/Compiler.js:661:31)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/project/serverless-project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/project/serverless-project/node_modules/tapable/lib/Hook.js:154:20)
    at hooks.optimizeAssets.callAsync.err (/project/serverless-project/node_modules/webpack/lib/Compilation.js:1403:35)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/project/serverless-project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/project/serverless-project/node_modules/tapable/lib/Hook.js:154:20)
    at hooks.optimizeChunkAssets.callAsync.err (/project/serverless-project/node_modules/webpack/lib/Compilation.js:1394:32)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/project/serverless-project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/project/serverless-project/node_modules/tapable/lib/Hook.js:154:20)
    at hooks.additionalAssets.callAsync.err (/project/serverless-project/node_modules/webpack/lib/Compilation.js:1389:36)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/project/serverless-project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/project/serverless-project/node_modules/tapable/lib/Hook.js:154:20)
    at hooks.optimizeTree.callAsync.err (/project/serverless-project/node_modules/webpack/lib/Compilation.js:1385:32)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/project/serverless-project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/project/serverless-project/node_modules/tapable/lib/Hook.js:154:20)
    at Compilation.seal (/project/serverless-project/node_modules/webpack/lib/Compilation.js:1322:27)
    at compilation.finish.err (/project/serverless-project/node_modules/webpack/lib/Compiler.js:658:18)
    at hooks.finishModules.callAsync.err (/project/serverless-project/node_modules/webpack/lib/Compilation.js:1245:4)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/project/serverless-project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
    at AsyncSeriesHook.lazyCompileHook (/project/serverless-project/node_modules/tapable/lib/Hook.js:154:20)
    at Compilation.finish (/project/serverless-project/node_modules/webpack/lib/Compilation.js:1237:28)
    at hooks.make.callAsync.err (/project/serverless-project/node_modules/webpack/lib/Compiler.js:655:17)
    at _err0 (eval at create (/project/serverless-project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at _addModuleChain (/project/serverless-project/node_modules/webpack/lib/Compilation.js:1169:12)
    at processModuleDependencies.err (/project/serverless-project/node_modules/webpack/lib/Compilation.js:1081:9)
    at processTicksAndRejections (internal/process/next_tick.js:74:9)
@jayair
Copy link
Member

jayair commented Jul 25, 2019

Do you have a repo I can reproduce this with? Or if I can get the package.json and Lambda function code. I'd like to see what it is complaining about.

@jayair jayair added the bug label Jul 25, 2019
@chrisanderss0n
Copy link
Author

package.json

{
  "name": "week-trello",
  "version": "1.1.0",
  "description": "A Node.js starter for the Serverless Framework with async/await and unit test support",
  "main": "handler.js",
  "scripts": {
    "test": "serverless-bundle test",
    "deploy": "serverless deploy"
  },
  "author": "",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/AnomalyInnovations/serverless-nodejs-starter.git"
  },
  "devDependencies": {
    "aws-sdk": "^2.401.0",
    "serverless-offline": "^3.25.6",
    "serverless-bundle": "^1.2.2"
  },
  "dependencies": {
    "http": "0.0.0",
    "oauth": "^0.9.15",
    "request": "^2.88.0",
    "request-promise": "^4.2.2",
    "url": "^0.11.0",
    "uuid": "^3.3.2"
  }
}

@chrisanderss0n
Copy link
Author

chrisanderss0n commented Jul 25, 2019

lambda code createCard.js

import requestlib from "request-promise";
import { success, failure } from "../libs/response-lib";

export async function main(event, context) {
  const data = JSON.parse(event.body);

  const options = {
    method: "POST",
    uri: `https://api.trello.com/1/cards`,
    form: {
      name: data.name,
      idList: data.idList,
      idLabels: data.idLabel,
      pos: data.position,
      key: process.env.API_KEY,
      token: process.env.SECRET
    }
  };

  try {
    let result = await requestlib(options);
    return success(result);
  } catch (e) {
    console.log(e);
    return failure({ status: false });
  }
}

@jayair
Copy link
Member

jayair commented Jul 26, 2019

Thanks for that. I'll give this a try and let you know.

@jayair
Copy link
Member

jayair commented Jul 27, 2019

Hmm I gave it a try and couldn't repro it. But I used the response-lib.js from here https://github.com/AnomalyInnovations/serverless-stack-demo-api/blob/master/libs/response-lib.js.

Maybe yours is different? Can you post that here? And your serverless.yml as well.

Also just to make sure, did you delete your package-lock.json before doing an npm install when migrating to the plugin?

@chrisanderss0n
Copy link
Author

I did delete my package-lock.json before doing npm install

my response-lib.js is the same as yours

export function success(body) {
  return buildResponse(200, body);
}

export function failure(body) {
  return buildResponse(500, body);
}

function buildResponse(statusCode, body) {
  return {
    statusCode: statusCode,
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Credentials": true
    },
    body: JSON.stringify(body)
  };
}

Here is serverless.yml

# NOTE: update this with your service name
service: week-trello

# Use the serverless-webpack plugin to transpile ES6
plugins:
  - serverless-bundle
  - serverless-offline

package:
  individually: true

provider:
  name: aws
  runtime: nodejs8.10
  stage: prod
  region: us-east-1
  # To load environment variables externally
  # rename env.example to env.yml and uncomment
  # the following line. Also, make sure to not
  # commit your env.yml.
  #
  environment: ${file(env.yml):${self:provider.stage}}

  # 'iamRoleStatements' defines the permission policy for the Lambda function.
  # In this case Lambda functions are granted with permissions to access DynamoDB.
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:us-east-1:*:*"

functions:
  createCard:
    handler: createCard.main
    events:
      - http:
          path: trello/card
          method: post
          cors: true
          authorizer: aws_iam
  finishCard:
    handler: finishCard.main
    events:
      - http:
          path: trello/finishCard
          method: post
          cors: true
          authorizer: aws_iam
  getBoards:
    handler: getBoards.main
    events:
      - http:
          path: trello/boards
          method: post
          cors: true
          authorizer: aws_iam
  setupWeek:
    handler: setupWeek.main
    events:
      - http:
          path: trello/setupWeek
          method: post
          cors: true
          authorizer: aws_iam

# Create our resources with separate CloudFormation templates
resources:
  # API Gateway Errors
  - ${file(../resources/api-gateway-errors.yml)}

@chrisanderss0n
Copy link
Author

@jayair is there anything else I can provide to help with this?

@jayair
Copy link
Member

jayair commented Aug 3, 2019

@chrisanderss0n Are the paths correct here? In your createCard.js:

import { success, failure } from "../libs/response-lib";

But your createCard.js is in your project root right? Shouldn't this be the path?

import { success, failure } from "./libs/response-lib";

@chrisanderss0n
Copy link
Author

This is how my project is setup:

Screen Shot 2019-08-05 at 11 34 51 AM

I have multiple services each with their own serverless.yml, but they all share the libs folder.

@chrisanderss0n
Copy link
Author

@jayair if you could provide me with a place to begin looking I can see if I can figure this out and submit a PR.

@jayair
Copy link
Member

jayair commented Sep 2, 2019

@chrisanderss0n I had tried your setup and got it to work with import { success, failure } from "./libs/response-lib"; instead of the path you were using, import { success, failure } from "../libs/response-lib";.

The best way to check this would be to create a simple repo that replicates the error. Would that be possible?

@vinyoliver
Copy link

vinyoliver commented Oct 4, 2019

@jayair I'm facing the same problem :(

@chrisanderss0n have you figured this out?

Below is my folder structure. My serverless project is the user folder. One level above (outside the user project) I've got the lib folder with some code I'd like to share with my serverless projects.

./api
   lib/someCode.js
   user/api/handler.js

This is how my handler is importing the code.

import { failure, success } from "../../lib/responses";

export const main = async (event, context) => {
  //code....

@jayair
Copy link
Member

jayair commented Oct 11, 2019

@vinyoliver I know in @chrisanderss0n's case I found the path was set incorrectly. I'm not sure what the issue is for your case. Can you make a repo or post the steps to recreate it?

@jayair jayair added question and removed bug labels Oct 11, 2019
@vinyoliver
Copy link

vinyoliver commented Oct 22, 2019

@jayair sorry for the late response. I got back to this issue today. I've created a new repo using the same structure as my project. I still get the same error; Maybe I'm missing something

Repo Example: https://github.com/vinyoliver/error-serverless

Lambda location: https://github.com/vinyoliver/error-serverless/tree/master/api/person/services
Shared code: https://github.com/vinyoliver/error-serverless/tree/master/lib

Tks in advance!

@vinyoliver
Copy link

vinyoliver commented Oct 22, 2019

I've finally figured out! After comparing my project to this project example I've noticed the only difference between them was that my root package.json was missing the serverless-bundle dependency.

After adding serverless-bundle dependency into my root package.json everything worked as expected!

I guess would be helpful to mention this on the README. The project example I got from this post:
https://seed.run/blog/how-to-structure-a-real-world-monorepo-serverless-app unfortunately, do not mention that this dependency is required in the root package.json to make it work.

@jayair
Copy link
Member

jayair commented Oct 26, 2019

Ah I see. Thanks for pointing it out.

@Z11
Copy link

Z11 commented Nov 5, 2020

As @vinyoliver mentioned, the solution is to install serverless as a dev dependency into your project
"yarn add --dev serverless"

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

No branches or pull requests

4 participants