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

Having handler file anywhere but root fails #839

Open
Cmdv opened this issue May 21, 2021 · 3 comments
Open

Having handler file anywhere but root fails #839

Cmdv opened this issue May 21, 2021 · 3 comments

Comments

@Cmdv
Copy link

Cmdv commented May 21, 2021

This is a Bug Report

Description

I tend to have my handler files in a folder off root eg: /handlers/xx.js
When using this format with serverless-webpack would create an error:

Cannot find module  `/path/to/project/dist/service/handlers/handlerfile.js`

dist/ being my output location for my bundling.
handlers/ being the location of my handlers

But if I take my handlers out of the handlers/ and put them same level as both my serverless.yml & webpack.config.js. Then all is honky dory 🕺

Additional Data

  • Serverless-Webpack Version you're using: Latest
  • Webpack version you're using: Latest
  • Serverless Framework Version you're using: Latest
  • Operating System: MacOS
  • Stack Trace (if available):
@j0k3r
Copy link
Member

j0k3r commented Jun 10, 2021

Could you share a minimal config with serverless.yml & webpack.config.js ? So I can try to debug it and fix it. Thanks 🙏

@Cmdv
Copy link
Author

Cmdv commented Jun 10, 2021

@j0k3r yeah sure no problems

...
functions:
  screenshot:
    handler: handlers/screenshot.handler
    environment:
      SERVERLESS_STAGE: ${opt:stage}
...  
const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const slsw = require('serverless-webpack');

module.exports = {
  entry: {
      screenshot: "./handlers/screenshot.js"
  },
  mode: slsw.lib.webpack.isLocal ? "development" : "production",
  optimization: {
    // minimize: false
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.resolve(__dirname, "dist"),
    filename: '[name].js'
  },
  target: 'node',
  plugins: [
    new webpack.ProvidePlugin({'XMLHttpRequest': 'xhr2'})
  ],
  module: {
    rules: [
      {
        test: /\.purs$/,
        use: [
          {
            loader: "purs-loader",
            options: {
              spago: true,
            }
          }
        ]
      },
    ]
  },
  resolve: {
    modules: [ 'node_modules' ],
    extensions: [".js", ".purs"]
  },
  externals: [nodeExternals({
    allowlist: ['XMLHttpRequest']
  })]
};

but if I remove /handlers/ put the file inside the root it all works.

@j0k3r
Copy link
Member

j0k3r commented Jun 11, 2021

I've created a minimal repro too and I do not have errors.

service: test

provider:
  name: aws
  region: eu-west-1
  runtime: nodejs12.x
  stage: ${opt:stage, 'dev'}
  versionFunctions: false

plugins:
  - serverless-webpack

functions:
  hello:
    handler: handlers/handler.hello

custom:
  webpack:
    packager: yarn
    keepOutputDirectory: true
const path = require('path');
const slsw = require('serverless-webpack');

module.exports = {
  entry: {
    hello: "./handlers/handler.js"
  },
  mode: slsw.lib.webpack.isLocal ? "development" : "production",
  target: 'node',
  module: {},
  output: {
    libraryTarget: 'commonjs',
    path: path.resolve(__dirname, "dist"),
    filename: '[name].js'
  },
  resolve: {
    modules: [ 'node_modules' ],
    extensions: [".js"]
  },
};

The packaging is ok

$ serverless package
Serverless: Bundling with Webpack...
Time: 115ms
Built at: 2021-06-11 10:29:49
   Asset      Size  Chunks             Chunk Names
hello.js  1.05 KiB       0  [emitted]  hello
Entrypoint hello = hello.js
[0] ./handlers/handler.js 123 bytes {0} [built]
Serverless: Copying existing artifacts...
Serverless: Packaging service...
$

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

No branches or pull requests

2 participants