recurly / recurly-client-node

node.js library for Recurly's V3 API
https://developers.recurly.com
MIT License
44 stars 10 forks source link

Serverless framework webpack scandir error? #121

Closed jatazoulja closed 4 years ago

jatazoulja commented 4 years ago

Using Severless Framework with webpack and babel just to import recurly: just use: import * as recurly from 'recurly

LOG ERROR: `Error: ENOENT: no such file or directory, scandir '/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/resources'",         "at Object.readdirSync (fs.js:865:3)",         "at Object.../../recurly/lib/recurly/resources.js (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/Users/{user.name}/Documents/POC/{project.name}/node_modules/recurly/lib/recurly/resources.js:6:1)",         "at webpack_require__ (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/webpack/bootstrap:19:1)",         "at Object.../../recurly/lib/recurly/BaseClient.js (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/Users/{user.name}/Documents/POC/{project.name}/node_modules/recurly/lib/recurly/BaseClient.js:11:19)",         "at webpack_require (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/webpack/bootstrap:19:1)",         "at Object.../../recurly/lib/recurly/Client.js (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/Users/{user.name}/Documents/POC/{project.name}/node_modules/recurly/lib/recurly/Client.js:10:20)",         "at __webpack_require (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/webpack/bootstrap:19:1)",         "at Object.../../recurly/lib/recurly.js (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/Users/{user.name}/Documents/POC/{project.name}/node_modules/recurly/lib/recurly.js:6:11)",         "at webpack_require (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/webpack/bootstrap:19:1)",         "at Module.../../../utils/recurly.js (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/Users/{user.name}/Documents/POC/{project.name}/utils/recurly.js:1:17)",         "at webpack_require (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/webpack/bootstrap:19:1)",         "at Module.../../../harp/plans.js (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/plans.js:102:72)",         "at __webpack_require__ (/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/webpack/bootstrap:19:1)",         "at /Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/webpack:/webpack/bootstrap:83:1",

bhelx commented 4 years ago

Hey @jatazoulja

I'm happy to help, but I'm not sure how to reproduce this. Could you help us by outlining a minimal reproducible example?

jatazoulja commented 4 years ago

package.json dependencies:

 ],
  "devDependencies": {
    "serverless-bundle": "^1.2.2",
    "serverless-offline": "^5.3.3"
  },
  "dependencies": {
    "cheerio": "^1.0.0-rc.3",
    "crawler": "^1.2.1",
    "moment": "^2.24.0",
    "pg": "^8.0.2",
    "qs": "^6.9.3",
    "recurly": "^3.8.0",
    "serverless": "^1.72.0",
    "uuidv4": "^6.1.0"
  },

serverless option:

package:
  individually: true

plugins:
  - serverless-bundle
  - serverless-offline

recurly.js:

const recurly = require("recurly"); //<--- error here

const {
  RECURLY_API_KEY,
} = process.env;
const client = new recurly.Client(RECURLY_API_KEY);

export default recurly;
bhelx commented 4 years ago

@jatazoulja how do i invoke this code? I'm not sure at all how serverless works. I tried using the serverless invoke command:

  Serverless Error ---------------------------------------

  "service" property is missing in serverless.yml
bhelx commented 4 years ago

@jatazoulja another question, those paths look like they still have template variables in them: '/Users/{user.name}/Documents/POC/{project.name}/.webpack/service/harp/resources'. Is that expected?

bhelx commented 4 years ago

@jatazoulja I still can't seem to repro. I do have a hint as to what may be going on though. Perhaps webpack doesn't like that we include those files dynamically (by walking the directory). I have a patch that uses a more explicit approach in this branch: https://github.com/recurly/recurly-client-node/tree/dynamic-to-manual-resource-import

The commit: https://github.com/recurly/recurly-client-node/commit/f48a437d4afa14acb369413dbd2cf0230b18f593

Would you mind trying that branch?

jatazoulja commented 4 years ago

Thanks @bgreenrecurly I will try to make a sample code base you can checkout to repro also will try that solution as well..

bhelx commented 4 years ago

@jatazoulja still having the problem? There's no rush to respond as it's not being reported elsewhere, just checking in on this issue. Let us know if we can help.

StuClift commented 4 years ago

This is caused by trying to load these resources at runtime from a folder that doesn't exist because you are bundling everything into a single file.

bhelx commented 4 years ago

@StuClift this is what I suspected, I've pushed a change in this branch to address that: https://github.com/recurly/recurly-client-node/tree/dynamic-to-manual-resource-import

Are you able to confirm that fixes the problem? We cannot confirm the fix without being able to reproduce.

StuClift commented 4 years ago

@bhelx I've tested your changes and can confirm that those files are now being statically resolved by the bundler and there is no runtime error for the missing directory. 👍

bhelx commented 4 years ago

@StuClift Great! thanks so much for taking the time to check. Opening a PR now.

bhelx commented 4 years ago

I changed this to a bug as it seems to me like it would be a bug for anyone using a bundler.

StuClift commented 4 years ago

@bhelx Thanks for the quick response, I have added a minimal repro. Just need npm i && npm run build && npm test You should then be able to apply your patch and get the correct result. recurly-node-bundle.zip

bhelx commented 4 years ago

Thanks, appreciate the help reproducing!

jatazoulja commented 4 years ago

sorry guys... I was busy with the project and resulted using node-recurly... will retest this lib again... thanks @StuClift