amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Error when running Amplify 8.x from VSCode debugger

Open josep11 opened this issue 4 years ago • 34 comments

Before opening, please confirm:

  • [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • [X] I have searched for duplicate or closed issues.
  • [X] I have read the guide for submitting bug reports.
  • [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • [X] I have removed any sensitive information from my code snippets and submission.

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/[email protected]

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/[email protected]
  2. amplify configure

GraphQL schema(s)

# Put schemas below this line


Log output

# Put your logs below this line


Additional information

No response

josep11 avatar Apr 25 '22 11:04 josep11

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

josefaidt avatar Apr 25 '22 13:04 josefaidt

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)

josep11 avatar Apr 26 '22 09:04 josep11

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 avatar Apr 26 '22 17:04 josefaidt

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

josefaidt avatar Apr 26 '22 18:04 josefaidt

Same issue is happned on my env :(

two-pack avatar May 11 '22 12:05 two-pack

Hey @two-pack can you confirm the following:

  • Node version
  • how Amplify CLI was installed (npm, curl, etc)
  • does Amplify CLI work outside of VSCode's integrated terminal?

josefaidt avatar May 11 '22 15:05 josefaidt

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

two-pack avatar May 12 '22 12:05 two-pack

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?

josefaidt avatar May 13 '22 18:05 josefaidt

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.

two-pack avatar May 13 '22 21:05 two-pack

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

josefaidt avatar May 13 '22 21:05 josefaidt

Yes I installed with following command for the reproduced.

sudo npm install -g @aws-amplify/[email protected] --unsafe-perm=true

two-pack avatar May 13 '22 21:05 two-pack

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

josefaidt avatar May 13 '22 21:05 josefaidt

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

two-pack avatar May 13 '22 22:05 two-pack

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

BeaveArony avatar May 16 '22 21:05 BeaveArony

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?

josefaidt avatar May 17 '22 13:05 josefaidt

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.

two-pack avatar May 17 '22 13:05 two-pack

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?

josefaidt avatar May 17 '22 14:05 josefaidt

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.

two-pack avatar May 17 '22 14:05 two-pack

@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?

josefaidt avatar May 17 '22 14:05 josefaidt

@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.

two-pack avatar May 17 '22 15:05 two-pack

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:

  • typical debug launch.json throws NODE_OPTIONS issue
    Users/josef/.amplify/bin/amplify: --inspect-publish-uid= is not allowed in NODE_OPTIONS
    
  • copying the command that the debugger outputted to the terminal and removing the option noted in the error above we encounter the error originally described here
{
  "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 🙂

josefaidt avatar May 17 '22 17:05 josefaidt

+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.

StefanVolkmer-WW avatar May 27 '22 09:05 StefanVolkmer-WW

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

sankranti2018 avatar Jun 03 '22 23:06 sankranti2018

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

hukpo avatar Jun 19 '22 09:06 hukpo

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: @.***>

sankranti2018 avatar Jun 19 '22 19:06 sankranti2018

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=

bmw46033 avatar Jun 20 '22 00:06 bmw46033

@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.

StefanVolkmer-WW avatar Jun 29 '22 09:06 StefanVolkmer-WW

@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.

BeaveArony avatar Jun 29 '22 11:06 BeaveArony

@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.

StefanVolkmer-WW avatar Jul 01 '22 08:07 StefanVolkmer-WW

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!

johnwylie70 avatar Aug 12 '22 22:08 johnwylie70