square / connect-api-examples

Code samples demonstrating the functionality of the Square Connect API
https://developer.squareup.com/docs/sample-apps
389 stars 792 forks source link

'NODE_ENV' is not recognized as an internal or external command, operable program or batch file #283

Open oshihirii opened 2 years ago

oshihirii commented 2 years ago

Windows 10
Node v14.18.1

I've cloned connect-api-examples with:

gh repo clone square/connect-api-examples

I'm following the instructions here: for connect-examples/v2/node_subscription:

https://github.com/square/connect-api-examples/tree/master/connect-examples/v2/node_subscription

I'm in the following location:

/connect-api-examples/connect-examples/v2/node_subscription

I've created a .env file with this content:

SQUARE_ACCESS_TOKEN=token-is-here  

and run:

npm install

and get error:

$ npm test

> square-subscription-api-sample-app@1.0.0 test D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription
> NODE_ENV=sandbox nodemon -e js,pug ./bin/www

'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! Test failed.  See above for more details.

Perhaps it has something to do with issues discussed here?

https://stackoverflow.com/q/11928013

Also, FYI, there is no config.json file in the root directory of this example, as referred to here:

https://github.com/square/connect-api-examples/tree/master/connect-examples/v2/node_subscription#project-organization

oshihirii commented 2 years ago

Update:

I don't know if this is the correct thing to do or not.

In package.json I added SET before NODE_ENV and && after the environment setting.

I.e. I replaced this:

  "scripts": {
    "start": "NODE_ENV=production nodemon -e js,pug ./bin/www",
    "test": "NODE_ENV=sandbox nodemon -e js,pug ./bin/www",
    "seed": "NODE_ENV=sandbox node ./bin/script/seed-data.js generate",
    "clear": "NODE_ENV=sandbox node ./bin/script/seed-data.js clear",
    "help": "NODE_ENV=sandbox node ./bin/script/seed-data.js --help"
  }

with this:

  "scripts": {
    "start": "SET NODE_ENV=production&& nodemon -e js,pug ./bin/www",
    "test": "SET NODE_ENV=sandbox&& nodemon -e js,pug ./bin/www",
    "seed": "SET NODE_ENV=sandbox&& node ./bin/script/seed-data.js generate",
    "clear": "SET NODE_ENV=sandbox&& node ./bin/script/seed-data.js clear",
    "help": "SET NODE_ENV=sandbox&& node ./bin/script/seed-data.js --help"
  }

And then run:

npm test    

And nodemon starts:

[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,pug
[nodemon] starting `node ./bin/www`
Listening on http://localhost:3000

When I go to page in browser, I get error:

No valid customer retreived, this example only works with customers that have email information.
Error: No valid customer retreived, this example only works with customers that have email information.
    at D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\routes\index.js:56:13
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

I looked at step 6 in the README which says:

[Optional] We provide a script you can use to quickly populate your sandbox store's subscription plans and customers with card on file. Run the script, wait for 30 seconds and refresh the page:

And ran:

$ npm run seed

Which returned:

> square-subscription-api-sample-app@1.0.0 seed D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription
> SET NODE_ENV=sandbox&& node ./bin/script/seed-data.js generate

Create customers failed:  ApiError: Response status code was not ok: 400.
    at D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\lib\http\requestBuilder.js:360:23
    at DefaultRequestBuilder.<anonymous> (D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\lib\http\requestBuilder.js:172:50)
    at step (D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\node_modules\tslib\tslib.js:143:27)
    at Object.next (D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\node_modules\tslib\tslib.js:124:57)
    at fulfilled (D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\node_modules\tslib\tslib.js:114:62)
    at processTicksAndRejections (internal/process/task_queues.js:95:5) {
  request: {
    method: 'POST',
    url: 'https://connect.squareupsandbox.com/v2/customers',
    headers: {
      'user-agent': 'Square-TypeScript-SDK/15.0.0',
      'content-type': 'application/json',
      authorization: 'Bearer EAAAEFFl-2suL2vQ6iXhKJnnym_oSz50iD2-R7ygX_YypdnxeY3hQPhtLHTfoqR8',
      'Square-Version': '2021-10-20',
      accept: 'application/json'
    },
    body: {
      type: 'text',
      content: '{"idempotency_key":"8b18cec1-1f9a-4d1f-b61e-a18fcbffdafb","given_name":"John","family_name":"Doe","email_address":"johndoe@square-example.com"}'
    }
  },
  statusCode: 400,
  headers: {
    date: 'Sat, 13 Nov 2021 08:01:55 GMT',
    'frame-options': 'DENY',
    'x-frame-options': 'DENY',
    'x-content-type-options': 'nosniff',
    'x-xss-protection': '1; mode=block',
    'content-type': 'application/json',
    'content-length': '123',
    'strict-transport-security': 'max-age=631152000; includeSubDomains; preload',
    connection: 'close'
  },
  body: '{"errors": [{"code": "BAD_REQUEST","detail": "Expected \\"{\\" (line 1, character 1)","category": "INVALID_REQUEST_ERROR"}]}\n',
  result: [Object: null prototype] { errors: [ [Object: null prototype] ] },
  errors: [
    [Object: null prototype] {
      code: 'BAD_REQUEST',
      detail: 'Expected "{" (line 1, character 1)',
      category: 'INVALID_REQUEST_ERROR'
    }
  ]
}
Updating catalog items failed:  ApiError: Response status code was not ok: 400.
    at D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\lib\http\requestBuilder.js:360:23
    at DefaultRequestBuilder.<anonymous> (D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\lib\http\requestBuilder.js:172:50)
    at step (D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\node_modules\tslib\tslib.js:143:27)
    at Object.next (D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\node_modules\tslib\tslib.js:124:57)
    at fulfilled (D:\REPOS\connect-api-examples\connect-examples\v2\node_subscription\node_modules\@apimatic\core\node_modules\tslib\tslib.js:114:62)
    at processTicksAndRejections (internal/process/task_queues.js:95:5) {
  request: {
    method: 'POST',
    url: 'https://connect.squareupsandbox.com/v2/catalog/batch-upsert',
    headers: {
      'user-agent': 'Square-TypeScript-SDK/15.0.0',
      'content-type': 'application/json',
      authorization: 'Bearer EAAAEFFl-2suL2vQ6iXhKJnnym_oSz50iD2-R7ygX_YypdnxeY3hQPhtLHTfoqR8',
      'Square-Version': '2021-10-20',
      accept: 'application/json'
    },
    body: {
      type: 'text',
      content: '{"idempotency_key":"db0cd490-4f27-46b2-b464-543a088c7c21","batches":[{"objects":[{"type":"SUBSCRIPTION_PLAN","id":"#Multiphase Gym Membership","subscription_plan_data":{"name":"Gym Membership","phases":[{"cadence":"MONTHLY","periods":1,"recurring_price_money":{"amount":100,"currency":"AUD"}},{"cadence":"WEEKLY","recurring_price_money":{"amount":1500,"currency":"AUD"}}]}},{"type":"SUBSCRIPTION_PLAN","id":"#Spa Service Addon","subscription_plan_data":{"name":"Spa Service Addon","phases":[{"cadence":"WEEKLY","periods":2,"recurring_price_money":{"amount":0,"currency":"AUD"}},{"cadence":"MONTHLY","recurring_price_money":{"amount":8000,"currency":"AUD"}}]}},{"type":"SUBSCRIPTION_PLAN","id":"#Laundry Service Addon","subscription_plan_data":{"name":"Laundry Service Addon","phases":[{"cadence":"WEEKLY","recurring_price_money":{"amount":1000,"currency":"AUD"}}]}}]}]}'
    }
  },
  statusCode: 400,
  headers: {
    'content-type': 'application/json',
    'square-version': '2021-10-20',
    vary: 'Origin, Accept-Encoding',
    'x-content-type-options': 'nosniff',
    'x-download-options': 'noopen',
    'x-frame-options': 'SAMEORIGIN',
    'x-permitted-cross-domain-policies': 'none',
    'x-speleo-traceid': 'KFWVWBEgMgXhR',
    'x-xss-protection': '1; mode=block',
    date: 'Sat, 13 Nov 2021 08:01:58 GMT',
    'content-length': '174',
    'strict-transport-security': 'max-age=631152000; includeSubDomains; preload',
    connection: 'close'
  },
  body: '{"errors":[{"category":"INVALID_REQUEST_ERROR","code":"BAD_REQUEST","detail":"Value is expected to be map[string]interface {}, but received string (line 1, character 991)"}]}',
  result: [Object: null prototype] { errors: [ [Object: null prototype] ] },
  errors: [
    [Object: null prototype] {
      category: 'INVALID_REQUEST_ERROR',
      code: 'BAD_REQUEST',
      detail: 'Value is expected to be map[string]interface {}, but received string (line 1, character 991)'
    }
  ]
}
emmac3 commented 2 years ago

Hi @oshihirii,

This is a known issue that has been reported before but we are unable to reproduce it on our end. I'd like to investigate further. Would you kindly provide me with the terminal that you are running these commands in?

oshihirii commented 2 years ago

i am using Windows Terminal (Preview) and Git Bash is my default profile (from Git).

may I ask which issue you are referring to?

this issue started with 1) the NODE_ENV issue, but then there were two other issues described: 2) the app doesn’t load (error in browser) 3) npm run seed doesn’t work (error in terminal).

oshihirii commented 2 years ago

Update:

If I manually add clients to the sandbox environment, then the app loads when running:

npm test

gym_members_app_loads

However, when I click on one of the customer names, I get:

gym_members_app_loads_error

Error message in chrome dev tools is:

GET http://localhost:3000/management/some-code-here/some-code-here 500 (Internal Server Error)
kharithomas commented 8 months ago

This is a very old thread, but I had this issue when starting.

Simple solution is to remove the test script inside package.json and pass the NODE_ENV as a param to the env.json file instead like this:

package.json:

"scripts": {
    "start": "nodemon -e js,pug ./bin/www",
    "test": "echo 'No tests found'",
    "seed": "node ./bin/script/seed-catalog.js generate",
    "clear": "node ./bin/script/seed-catalog.js clear",
    "help": "node ./bin/script/seed-catalog.js --help"
  },

env.json:

NODE_ENV=sandbox
SQ_APPLICATION_ID=your-application-id
SQ_ACCESS_TOKEN=your-access-token

When you're ready to run in production you just change to NODE_ENV=production.