2

I am trying to build a backend based on the http://serverless.com framework & started following this repository for reference - https://github.com/AnomalyInnovations/serverless-stack-demo-api

Now, I have added the same config, resources files but wanted to organize my handler funtions. So, updated the directory structure to be something like - controllers/service/handler.js

Now, with this I changed my function definitions in serverless.yml to be like this -

functions:
  - ${file(routes/user/user.routes.definitions.yml)}

where the contents of user routes file are -

healthcheck:
    # Defines an HTTP API endpoint that calls the healthcheck function in handler.js
    # - path: url path is /health-check
    # - method: GET request
    handler: handler.healthcheck
    events:
      - http:
          path: health-check
          method: get
          cors: true

create:
  handler: controllers/service/handler.main
  events:
    - http:
        path: users
        method: post
        cors: true
        authorizer: aws_iam

get:
  # Defines an HTTP API endpoint that calls the main function in get.js
  # - path: url path is /users/{id}
  # - method: GET request
  handler: controllers/service/handler.main
  events:
    - http:
        path: users/{id}
        method: get
        cors: true
        authorizer: aws_iam

Now, I am getting the following error when trying serverless deploy -

ERROR in ./controllers/user/get.js
Module not found: Error: Can't resolve '../../../libs/dynamodb-lib' in 'E:\work\dittytv\backend\controllers\user'
 @ ./controllers/user/get.js 70:19-56

ERROR in ./controllers/user/list.js
Module not found: Error: Can't resolve '../../../libs/dynamodb-lib' in 'E:\work\dittytv\backend\controllers\user'
 @ ./controllers/user/list.js 69:19-56

ERROR in ./controllers/user/create.js
Module not found: Error: Can't resolve '../../../libs/dynamodb-lib' in 'E:\work\dittytv\backend\controllers\user'
 @ ./controllers/user/create.js 68:19-56

Now, my controller file starts from this -

import * as dynamoDbLib from "../../../libs/dynamodb-lib";
import { success, failure } from "../../../libs/response-lib";

& apart from this I have tried ./libs/ ./../../../libs but still the same error.

I am assuming this has to do something with the webpack. So here is the webpack config I am using -

const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");

module.exports = {
  entry: slsw.lib.entries,
  target: "node",
  // Generate sourcemaps for proper error messages
  devtool: 'source-map',
  // Since 'aws-sdk' is not compatible with webpack,
  // we exclude all node dependencies
  externals: [nodeExternals()],
  mode: slsw.lib.webpack.isLocal ? "development" : "production",
  optimization: {
    // We no not want to minimize our code.
    minimize: false
  },
  performance: {
    // Turn off size warnings for entry points
    hints: false
  },
  // Run babel on all .js files and skip those in node_modules
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: "babel-loader",
        include: __dirname,
        exclude: /node_modules/
      }
    ]
  }
};

Can anyone help identify / fix the issue here

Harshit Laddha
  • 2,044
  • 8
  • 34
  • 64

1 Answers1

0

You need to allow webpack config to resolve js files or you can add the .js extension after the import file name.

resolve:{
   extensions: [".ts", ".js", ".json"]
 }