Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Cannot find module 'cors'

When running my app (just a node backend), I get `Cannot find module 'cors'` error.
The cors package is listed in my package.json dependencies, the npm install step seems to run fine too.
And yet it can't seem to find the cors module.

this is my app.yml
```

runtime: nodejs20
env: standard

handlers:
- url: /.*
script: auto

```
I've tried finding a way to see what's in the modules with no success. Iv'e tried different node version, and re-running npm install on build / start, but nothing works.

I've deployed other apps on app engine (full stack apps) and never had this error

0 1 643
1 REPLY 1

Hi @Lightwaves

I would like to confirm if your app is running behind Identity Aware Proxy (IAP)? If it is, it might be  throwing errors because IAP has CORS disabled by default. You may refer to this document on how to enable CORS when using App Engine with IAP.

On the other hand, the "Cannot find module 'cors'" error usually happens when the cors module isn't being recognized, even though it's in your package.json. Since you've already checked that the npm install runs without errors, there are a few things we can try:

  1. Check if node_modules is included: When deploying to Google Cloud, sometimes the node_modules folder doesn't get uploaded. You can check if the folder is in your deployment directory or if it's excluded by .gitignore or other ignore settings. Make sure that you run npm install in the environment you are deploying from and not just locally. You can rebuild the node_modules folder before deploying by running the following command: 
    rm -rf node_modules
    npm install
    gcloud app deploy
    
  2. Verify if the cors package is correctly installed: Double-check the package.json file for the proper version of cors in the dependencies section. You can try running the following command to confirm that the cors package is in your node_modules locally:
    npm ls cors
    Once you confirm if the Cors package is in your node_modules. You can try to install/reinstall by running the code:
    npm uninstall cors
    npm install cors
    
  3. Check Deployment Configuration: It looks like you're using the correct settings for nodejs20, but try adding an env_variables section in case any environment variables are missing. The script: auto option should allow App Engine to automatically detect and run your app, but ensure that your main file (usually server.js or index.js) is in the root of the project and is correctly specified.
    runtime: nodejs20
    env: standard
    
    env_variables:
     NODE_ENV: 'production'
    
    handlers:
    - url: /.*
      script: auto
    

After doing this, try redeploying to App Engine and see if that clears things up.  If the above solution doesn't resolve the issue, you can contact Google Cloud Support to further look into your case. I can't say exactly a specific time frame, but they'll do their best to address the issue. Let me know if it helped, thanks!