braintree / braintree_node

Braintree Node.js library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
334 stars 104 forks source link

Azure Function Node.js - Cannot find module './braintree/test/... #174

Closed BarryEdwards closed 4 years ago

BarryEdwards commented 4 years ago

When I install npm module braintree on an Azure Function (Node.js 10 or 8) , I am getting this error on Azure function execution:

2020-08-31T03:42:48.274 [Error] Executed 'Functions.clientToken' (Failed, Id=c50ac3b5-249b-4d2f-bc2c-23582910528c, Duration=2ms)Result: FailureException: Worker was unable to load function clientToken: 'Error: Cannot find module './braintree/test/credit_card_defaults''Stack: Error: Cannot find module './braintree/test/credit_card_defaults'at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)at Function.Module._load (internal/modules/cjs/loader.js:562:25)at Module.require (internal/modules/cjs/loader.js:692:17)at require (internal/modules/cjs/helpers.js:25:18)at Object. (D:\home\site\wwwroot\node_modules\braintree\lib\braintree.js:32:26)at Module._compile (internal/modules/cjs/loader.js:778:30)at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)at Module.load (internal/modules/cjs/loader.js:653:32)at tryModuleLoad (internal/modules/cjs/loader.js:593:12)at Function.Module._load (internal/modules/cjs/loader.js:585:3)

Closer inspection shows that this directory is not being installed via 'npm install braintree' or 'npm -g install braintree'

./node_modules/braintree/lib/braintree/test/...

Note that this is NOT happening locally. That is, on my local dev environment. Rather, its happens on Azure App itself.

The Azure app is using this version on npm . D:\home\site\wwwroot\node_modules\braintree\lib\braintree>npm --version 6.13.4

My local environment is using this version:

npm --version 6.14.8

scannillo commented 4 years ago

Hi @BarryEdwards - looking at a similar issue reported on Microsoft's GitHub, can you check that your braintree dependency is listed in your package.json as a dependency and not a dev dependency?

BarryEdwards commented 4 years ago

Hi @scannillo Its definitely a dependancy. { "name": "xxxx-poc", "version": "1.0.0", "description": "", "scripts": { "start": "func start", "test": "echo \"No tests yet...\"" }, "dependencies": { "braintree": "^2.24.0", "cookie-parser": "^1.4.5", "http-errors": "^1.8.0", "morgan": "^1.10.0", "path": "^0.12.7" }, "devDependencies": {} }

scannillo commented 4 years ago

Were you able to try this having your Azure app and your local setup to use the same versions of npm and node?

BarryEdwards commented 4 years ago

Hi @scannillo Just done that. My local now runs same versions as the Azure App node -v v10.19.0 npm -v 6.13.4

I get the 'test' folder created ./node_modules/braintree/lib/braintree/test on my local, after removing the node_modules directory and installing (npm install).

So unlikely the node/npm version.

scannillo commented 4 years ago

Can you try removing the node_modules folder entirely and re-installing them in your Azure app?

Also - to confirm, is your Azure app using yarn by chance (want to rule this out)? There was a bug in older versions of yarn where yarn clean would remove the test directory.

BarryEdwards commented 4 years ago

Hi @scannillo Its using npm, I think yarn is not even deployed on the Azure app.

Regarding removing the node_modules directory, and re-installing. This has got the Functions working!

So good, but this is a work around. I think, the underlaying issue is still unknown (see below).

This is what I did:

  1. Set the Azure Function App configuration variable to WEBSITE_RUN_FROM_PACKAGE = 0 This makes the Azure App on-line 'editable'
  2. Using the Azure portal Function console: (I cannot recall if I stopped and restarted the Azure App..) ...wwwroot> rm -fr node_modules ...wwwroot> npm install ...wwwroot> ls -l node_modules/braintree/lib/braintree/test Total 12 ...

The Azure Function now works correctly.

  1. Redeploy the Azure App from vscode

The Azure Function STILL works correctly. Note, I have not tried re-setting WEBSITE_RUN_FROM_PACKAGE = 1

So there is something strange in the way the initial Azure deployment happened whereby the /test/ folder is not deployed.

scannillo commented 4 years ago

I'm glad that worked for you!

I'm curious though if the issue happening on every deploy, unless you include rm -fr node_modules, or if it was only happening on your initial Azure deploy. Would be nice to know which part of the deploy process (either something specific to Azure or your application) that is stripping the /test dir.

Either way - our team will look into renaming this directory, as this is the second time using the name test has led to unanticipated conflicts.

BarryEdwards commented 4 years ago

The issue happened on every deploy, until the 'rm -fr node_modules; npm install'. I think its something to do with the Azure deployment process. I did not mess with the "out-of-the-box' vscode Azure Function deployment scripting, so maybe not my application.

scannillo commented 4 years ago

Ok, thanks for the follow up. I am glad you have a workaround.

But we will keep you posted here when make a release with the renamed /test dir.

sestevens commented 4 years ago

@BarryEdwards We've updated the name of this directory to test_values, which should resolve this issue. Please let us know if you continue to see any problems.

This change was released in version 3.0.0.