aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.83k stars 822 forks source link

Error when running Amplify 8.x from VSCode debugger #10284

Open josep11 opened 2 years ago

josep11 commented 2 years ago

Before opening, please confirm:

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

v16.14.0

Amplify CLI Version

8.0.2

What operating system are you using?

Mac

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

Didn't get there yet

Amplify Categories

Not applicable

Amplify Commands

configure

Describe the bug

I just installed the cli with: npm i -g @aws-amplify/cli from VSCode terminal. It was not working and I also tried to downgrade from 8.0.2 to 8.0.0: npm install -g @aws-amplify/cli@8.0.0

With both times it would throw the same error when running any amplify command:

internal/modules/package_json_reader.js:4 const internalModuleReadJSON = function (f) { return require('fs').internalModuleReadJSON(f); }; ^

TypeError: require(...).internalModuleReadJSON is not a function at internalModuleReadJSON (internal/modules/package_json_reader.js:4:68) at Object.read (internal/modules/package_json_reader.js:21:34) at readPackage (internal/modules/cjs/loader.js:264:36) at readPackageScope (internal/modules/cjs/loader.js:297:19) at trySelf (internal/modules/cjs/loader.js:412:40) at Function.Module._resolveFilename (internal/modules/cjs/loader.js:879:24) at Function.Module._load (internal/modules/cjs/loader.js:746:27) at Module.require (internal/modules/cjs/loader.js:974:19) at Module._preloadModules (internal/modules/cjs/loader.js:1244:12) at loadPreloadModules (internal/bootstrap/pre_execution.js:480:5)

When I run amplify from the Mac terminal it works. It's weird because sam and aws work just fine from both terminals.

Expected behavior

to work

Reproduction steps

  1. npm install -g @aws-amplify/cli@8.0.0
  2. amplify configure

GraphQL schema(s)

```graphql # Put schemas below this line ```

Log output

``` # Put your logs below this line ```

Additional information

No response

josefaidt commented 2 years ago

Hey @josep11 :wave: thanks for raising this! Is the VSCode terminal using a different shell perhaps? Or a different Node/npm version?

josep11 commented 2 years ago

Thanks for the reply @josefaidt, the result is the same for both terminals.

➜ amplify-tutorial git:(main) node -v v16.14.0 ➜ amplify-tutorial git:(main) echo $SHELL /bin/zsh ➜ amplify-tutorial git:(main)

josefaidt commented 2 years ago

Hey @josep11 thanks for the clarification! To clarify further can you confirm both terminals are pointing to the same Node.js installation? Are you able to run these commands outside of the project in both terminals?

josefaidt commented 2 years ago

NOTE: potentially related https://github.com/vercel/pkg/issues/1209

two-pack commented 2 years ago

Same issue is happned on my env :(

josefaidt commented 2 years ago

Hey @two-pack can you confirm the following:

two-pack commented 2 years ago

Sorry, following command is succeed on my environment.

$ amplify configure

Another command which debugs lambda from vscode occurs the same error, but I'll re-check my env again. My environment is WSL2 Ubuntu 20.04 on Windows 10 Pro Version: 21H1 Build: 19043.1645.

$ node -v
v14.18.3
$ echo $SHELL
/bin/bash
$ amplify -v
8.0.0
josefaidt commented 2 years ago

Hey @two-pack thank you for clarifying. Unfortunately we have not been able to reproduce this issue in VSCode's integrated terminal. Do you see only WSL Bash in the integrated terminal causing issues? Would you be open to hopping on a quick call?

two-pack commented 2 years ago

I reproduced same error with the following command in WSL Bash and VSCode' integrated terminal. NOTE: debugfunc is a name of amplify function (Lambda).

$ /usr/bin/env 'NODE_OPTIONS=--require /home/foo/.vscode-server/bin/da15b6fd3ef856477bf6f4fb29ba1b7af717770d/extensions/ms-vscode.js-debug/src/bootloader.bundle.js' /usr/bin/node ./../../../../usr/bin/amplify mock function debugfunc --event ts-src/event.json --timeout 0 
internal/modules/package_json_reader.js:4
const internalModuleReadJSON = function (f) { return require('fs').internalModuleReadJSON(f); };
                                                                   ^

TypeError: require(...).internalModuleReadJSON is not a function
    at internalModuleReadJSON (internal/modules/package_json_reader.js:4:68)
    at Object.read (internal/modules/package_json_reader.js:21:34)
    at readPackage (internal/modules/cjs/loader.js:264:36)
    at readPackageScope (internal/modules/cjs/loader.js:297:19)
    at trySelf (internal/modules/cjs/loader.js:412:40)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:879:24)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at Module._preloadModules (internal/modules/cjs/loader.js:1244:12)
    at loadPreloadModules (internal/bootstrap/pre_execution.js:480:5)

Above command is for debugging of amplify functions with following setting. But some environment options are removed because rejected other errors.

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "debugfunc",
      "program": "/usr/bin/amplify",      
      "preLaunchTask":"webpack",
      "args": [
        "mock",
        "function",
        "debugfunc",
        "--event",
        "ts-src/event.json",
        "--timeout",
        "0"
      ],
      "console": "integratedTerminal",
      "sourceMaps": true,
      "smartStep": true,
      "outFiles": ["${workspaceFolder}/amplify/backend/function/debugfunc/src/**/*.js"],
      "sourceMapPathOverrides": {
        "webpack:///./*": "${workspaceFolder}/amplify/backend/function/debugfunc/*"
      }
    },
  ]
}

The original command is following.

$  cd /home/foo/dev/test-debug-func ; /usr/bin/env 'NODE_OPTIONS=--require /home/foo/.vscode-server/bin/da15b6fd3ef856477bf6f4fb29ba1b7af717770d/extensions/ms-vscode.js-debug/src/bootloader.bundle.js --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.850-3.sock","deferredMode":false,"waitForDebugger":"","execPath":"/usr/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","mandatePortTracking":true,"fileCallback":"/tmp/node-debug-callback-4771749cf913d1dd"}' /usr/bin/node ./../../../../usr/bin/amplify mock function debugfunc --event ts-src/event.json --timeout 0 
Debugger attached.
/home/foo/.amplify/bin/amplify: --inspect-publish-uid= is not allowed in NODE_OPTIONS
Waiting for the debugger to disconnect...

I success the command If I removed NODE_OPTIONS --require. And I alse success with simple js code (which only console.log) instead of amplify.

$ /usr/bin/env /usr/bin/node ./../../../../usr/bin/amplify mock function debugfunc --event ts-src/event.json --timeout 0
Ensuring latest function changes are built...
Starting execution...
<snip>
Finished execution.
$ echo 'console.log("test");' > test.js
$ /usr/bin/env 'NODE_OPTIONS=--require /home/foo/.vscode-server/bin/da15b6fd3ef856477bf6f4fb29ba1b7af717770d/extensions/ms-vscode.js-debug/src/bootloader.bundle.js' /usr/bin/node test.js
test

So I think this issue is caused a combination of amplify and VSCode JavaScript Debugger in my case. And it is probably different from the original issue case.

VSCode versions are

Version: 1.67.1 (system setup)
Commit: da15b6fd3ef856477bf6f4fb29ba1b7af717770d
Date: 2022-05-06T12:37:03.389Z
Electron: 17.4.1
Chromium: 98.0.4758.141
Node.js: 16.13.0
V8: 9.8.177.13-electron.0
OS: Windows_NT x64 10.0.22000

And the version of JavaScript Debugger extension is v1.67.2.

Would you be open to hopping on a quick call?

It's ok if I'm free, but my english is poor especially speaking and listening.

josefaidt commented 2 years ago

Hey @two-pack this line stands out to me "program": "/usr/bin/amplify",, was amplify installed with sudo by chance?

two-pack commented 2 years ago

Yes I installed with following command for the reproduced.

sudo npm install -g @aws-amplify/cli@8.0.0 --unsafe-perm=true
josefaidt commented 2 years ago

Can you try installing without sudo? If you have troubles installing the CLI without sudo perhaps this doc will share some additional insight: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

two-pack commented 2 years ago

Nothing changes after reinstalling amplify cli without sudo. I used n.

BeaveArony commented 2 years ago

I had the same problem since 8.0.0.

I was able to fix it by uninstalling the Extension "Debugger for Chrome". I also disabled and then re-enabled the "Javascript Debugger" and the "JavaScript Debugger Companion Extension", but I'm not sure if this was needed.

After deleting the Build-In Terminal and opening a new one I could finally run amplify -v without this error

josefaidt commented 2 years ago

Hey @BeaveArony thank you for chiming in here and that brings up a good point, @two-pack do you also experience this after disabling all VSCode extensions?

two-pack commented 2 years ago

Hey @BeaveArony thank you for chiming in here and that brings up a good point, @two-pack do you also experience this after disabling all VSCode extensions?

I tried, but nothing changed.

josefaidt commented 2 years ago

Thanks for clarifying @two-pack , to clarify further are we executing commands such as amplify mock function myfunction directly or using the absolute path to the binary? Does this appear to affect normal command executions or only under the context of VSCode debugging?

two-pack commented 2 years ago

I use like amplify mock function myfunction. And only with VSCode debugging, this command line has NODE_OPTIONS environment variables with require and it caused this error I think.

josefaidt commented 2 years ago

@two-pack can you share the updated launch.json for VSCode debugging (after reinstalling without sudo)? Do you also experience this when running npx amplify mock function myfunction? Was Node also installed with sudo?

two-pack commented 2 years ago

@josefaidt, I created new amplify project with all default configure and added Hello world lambda. And I pushed to this repository (include launch.json). https://github.com/two-pack/amplify-lambda-debug-with-vscode

I use n package system and installed amplify cli with out sudo. Following logs are running debug on VSCode.

$  /usr/bin/env 'NODE_OPTIONS=--require /home/two-pack/.vscode-server/bin/da15b6fd3ef856477bf6f4fb29ba1b7af717770d/extensions/ms-vscode.js-debug/src/bootloader.bundle.js --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.2102-1.sock","deferredMode":false,"waitForDebugger":"","execPath":"/usr/local/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","mandatePortTracking":true,"fileCallback":"/tmp/node-debug-callback-c3cd1e02980488e7"}' /usr/local/bin/node ./../../../../usr/local/bin/amplify mock function debugfunc --event src/event.json --timeout 0 
Debugger attached.
/home/two-pack/.amplify/bin/amplify: --inspect-publish-uid= is not allowed in NODE_OPTIONS
Waiting for the debugger to disconnect...
$ /usr/bin/env 'NODE_OPTIONS=--require /home/two-pack/.vscode-server/bin/da15b6fd3ef856477bf6f4fb29ba1b7af717770d/extensions/ms-vscode.js-debug/src/bootloader.bundle.js' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.2102-1.sock","deferredMode":false,"waitForDebugger":"","execPath":"/usr/local/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","mandatePortTracking":true,"fileCallback":"/tmp/node-debug-callback-c3cd1e02980488e7"}' /usr/local/bin/node ./../../../../usr/local/bin/amplify mock function debugfunc --event src/event.json --timeout 0 
internal/modules/package_json_reader.js:4
const internalModuleReadJSON = function (f) { return require('fs').internalModuleReadJSON(f); };
                                                                   ^

TypeError: require(...).internalModuleReadJSON is not a function
    at internalModuleReadJSON (internal/modules/package_json_reader.js:4:68)
    at Object.read (internal/modules/package_json_reader.js:21:34)
    at readPackage (internal/modules/cjs/loader.js:264:36)
    at readPackageScope (internal/modules/cjs/loader.js:297:19)
    at trySelf (internal/modules/cjs/loader.js:412:40)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:879:24)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at Module._preloadModules (internal/modules/cjs/loader.js:1244:12)
    at loadPreloadModules (internal/bootstrap/pre_execution.js:480:5)
$ /usr/local/bin/amplify mock function debugfunc --event src/event.json --timeout 0
Ensuring latest function changes are built...
Starting execution...
EVENT: {"key1":"value1","key2":"value2","key3":"value3"}
Result:
{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}
Finished execution.
$ amplify mock function debugfunc --event src/event.json --timeout 0
Ensuring latest function changes are built...
Starting execution...
EVENT: {"key1":"value1","key2":"value2","key3":"value3"}
Result:
{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}
Finished execution.
$ which amplify 
/usr/local/bin/amplify
$ node -v
v16.13.0
$ amplify -v
8.0.0
$ /home/two-pack/.amplify/bin/amplify -v
8.0.0
  1. I ran the debug from VSCode, I got NODE_OPTIONS error.
  2. I removed --inspect-publish-uid option from NODE_OPTIONS, so I got this issue error.
  3. I ran local mocking with absolute path of amplify cli. It's ok.
  4. Only amplify was also ok.
josefaidt commented 2 years ago

Hey @two-pack thank you for clarifying that piece! I was not trying to reproduce with the VSCode debugger initially but now I am seeing both issues you have noticed:

{
  "configurations": [
    {
      "name": "Mock Amplify function",
      "request": "launch",
      "type": "node",
      "runtimeExecutable": "amplify",
      "runtimeArgs": [
        "mock",
        "function",
        "myfunction",
        "--event",
        "src/event.json"
      ],
      "console": "integratedTerminal"
    }
  ]
}

Given this issue started to occur with 8.x and has broken existing workflows I will mark this as a bug for the team to evaluate further 🙂

Stefan-WW commented 2 years ago

+1: This is a big issue, it also prevents the possibility to debug "amplify build". When you use Overrides and Custom Resources it's annoying without a debug possibility.

sankranti2018 commented 2 years ago

Any workarounds? Other than running from a command prompt. Thanks.

hukpo commented 2 years ago

Disable JavaScript Debugger Companion Disable JavaScript Debugger Enable them again Kill terminal It works

sankranti2018 commented 2 years ago

Yes. This worked for me. Thank you!


From: Pavlo Huk @.> Sent: Sunday, June 19, 2022 9:14 AM To: aws-amplify/amplify-cli @.> Cc: sankranti2018 @.>; Comment @.> Subject: Re: [aws-amplify/amplify-cli] Error when running Amplify 8.x from VSCode debugger (Issue #10284)

Disable JavaScript Debugger Companion Ext Disable JavaScript Debugger Enable them again Kill terminal It works

— Reply to this email directly, view it on GitHubhttps://github.com/aws-amplify/amplify-cli/issues/10284#issuecomment-1159663745, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABMQT3JCNSJ7F43LUELSHVDVP3QHXANCNFSM5UIGX6AA. You are receiving this because you commented.Message ID: @.***>

bmw46033 commented 2 years ago

The quick temp fix is to just set NODE_OPTIONS to nothing. Everytime I start up VS Code, I have to do this again, though. So, not a permanent solution.

In a terminal command window: Windows: SET NODE_OPTIONS=

Stefan-WW commented 2 years ago

@hukpo :

You mean this: image image image

End then enable it again? i tried it multiple times, but no difference.

My NODE_OPTIONS is set to: --require "c:/Program Files/Microsoft VS Code/resources/app/extensions/ms-vscode.js-debug/src/bootloader.bundle.js" --inspect-publish-uid=http

With this setting i get: "amplify.exe: --inspect-publish-uid= is not allowed in NODE_OPTIONS"

if i remove the "inspect-publish-uid=http" from NODE_OPTIONS i get the .internalModuleReadJSON error

image

@bmw46033 : Setting NODE_OPTIONS to empty leads in my case that i doesn't have a connection to the vscode debugger.

BeaveArony commented 2 years ago

@StefanVolkmer-WW

Maybe on top of killing the terminal try to restart vscode and keep the extensions disabled until it works?

In my case once I had a built-in terminal where I could execute amplify -v I could reenable these extensions and create further terminals without any problems.

Stefan-WW commented 2 years ago

@BeaveArony : Unfortunately no success. i also tried to restart vscode after disabling it. Only to make sure that we work with the same terminal. Which kind of vscode terminal do you use? the problem occurs when i use "javascript debug terminal". when i use git bash it works.

But i want to use "javascript debug terminal" in order to debug amplify custom resources or amplify plugins.

Do you use "javascript debug terminal" too?

In the meantime i found a workaround. I install the amplify-cli in the same way as when i want to contributing it. then you have a "amplify-dev" command. Here i can use the javascript debug terminal. But i hope i can avoid this workaround in the near future.

johnwylie70 commented 2 years ago

None of the above worked for M1 based Mac, BUT

Quick workaround fix, run this in the VS Code terminal:

Mac/Linux: export NODE_OPTIONS=''

Windows (as per bmw46033 suggestion): SET NODE_OPTIONS=''

This will probably break debugging integration etc but I rarely use that except when necessary

This will need to be run each time VSCode and a terminal session is started

for speed on Mac/linux create a shell script in the root of the project called vscode.sh:

#!/bin/zsh export NODE_OPTIONS=''

then make the script executable chmod +x vscode.sh

Then when vscode first starts, open the terminal panel and run: ./vscode.sh

Then Amplify CLI will work!

jkk4999 commented 2 years ago

Disabling the Javascript debugger before installing Amplify worked for me.

sankranti2018 commented 2 years ago

Me too. This works.. amplify -v I could reenable these extensions and create further terminals without any problems.


From: Michael Gustmann @.> Sent: Wednesday, June 29, 2022 11:12 AM To: aws-amplify/amplify-cli @.> Cc: sankranti2018 @.>; Comment @.> Subject: Re: [aws-amplify/amplify-cli] Error when running Amplify 8.x from VSCode debugger (Issue #10284)

@StefanVolkmer-WWhttps://github.com/StefanVolkmer-WW

Maybe on top of killing the terminal try to restart vscode and keep the extensions disabled until it works?

In my case once I had a built-in terminal where I could execute amplify -v I could reenable these extensions and create further terminals without any problems.

— Reply to this email directly, view it on GitHubhttps://github.com/aws-amplify/amplify-cli/issues/10284#issuecomment-1169851824, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABMQT3KACYKFLYZKXQT4GZDVRQVRPANCNFSM5UIGX6AA. You are receiving this because you commented.Message ID: @.***>

niv-goldenberg commented 2 years ago

This works for me: Turning off the built-in debugging support for vscode which is shown around the bottom left of the vscode window as described below will solve the issue: https://code.visualstudio.com/docs/nodejs/nodejs-debugging

When you need to debug JS, just set it again - this is an issue only when running Amplify commands.

tomwang57 commented 2 months ago

The NODE_OPTIONS is added by the default javascript debugger in VSCode, disabling it fix this issue:

image