Search code examples
node.jspaypalcors

CORS in NodeJS app that has type "module"


I have two apps in VSCode, a Python Flask app that submits a POST request to PayPal NodeJS app. Here is the PayPal documentation on the NodeJS app.

When I go to http://127.0.0.1:5000 the Flask web app submits a POST request to the PayPal NodeJS app at http://127.0.0.1:8888. The web browser console captures the following.

Cross-Origin Read Blocking (CORB) blocked cross-origin response https://api2.amplitude.com/2/httpapi with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.
POST http://127.0.0.1:8888/my-server/create-paypal-order net::ERR_CONNECTION_REFUSED

This Stack Overflow post has an answer that mentions adding CORS to NodeJS.

The PayPal NodeJS app is "type": "module" (not "type": "commonjs"). Most articles I'm finding on Google and Stack Overflow explain how to use CORS in a CommonJS NodeJS app. I installed CORS in VSCode using npm install cors and then added these lines to NodeJS server.js.

const cors = import("cors");
app.use(cors());

And I get the following.

TypeError: cors is not a function

I am not sure how to configure a NodeJS app that has "type": "module" so that POST requests are NOT blocked by CORS policy.


Solution

  • The correct import syntax for using the cors package in type module is import cors from 'cors';