How do I setup VSCode to run and debug KeystoneJS application - visual-studio-code

I am trying to setup VSCode to run and debug my KeystoneJS application.
I currently run the application using npm run dev or yarn dev - in package.json, the dev script is set like this:
"scripts": {
"dev": "cross-env NODE_ENV=development DISABLE_LOGGING=true keystone dev"
},
If I try to run cross-env NODE_ENV=development DISABLE_LOGGING=true keystone dev from my prompt, I get the error, command not found. I would love to understand why this is not working...
I tried to setup my debug configuration in launch.json like this:
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/keystone",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceFolder}",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"PORT":"3030",
"NODE_ENV":"development",
"DISABLE_LOGGING":"true"
}
}
]
}
but it returns the error

this is how you can do this by changing npm script for dev
"dev": "cross-env PORT=4000 NODE_ENV=development NODE_OPTIONS=--inspect DISABLE_LOGGING=true keystone dev",
NODE_OPTIONS=--inspect or NODE_OPTIONS=--inspect-brk does the magic.
You must do this after cross-env as indicated above and not like below.
"dev": "NODE_OPTIONS=--inspect cross-env PORT=4000 NODE_ENV=development DISABLE_LOGGING=true keystone dev", (does not work)
Edit: 8 may
You can use following config in launch.json in vscode, (ideally the npm script should be called debug)
With NPM
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"dev"
],
"port": 9229,
"skipFiles": [
"<node_internals>/**"
]
}
]
}
with YARN
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"dev"
],
"port": 9229,
"skipFiles": [
"<node_internals>/**"
]
}
]
}
change port only if you change port in NODE_OPTIONS in package.json script

Related

Use preLaunchTasks in flutter launch config

I am trying to run a preLaunchTask to define environment vars. The problem is that the tasks runs in a separate terminal while the flutter launch runs in the Debug Console.
Is there a way to launch the tasks in the debug console?
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "LoadEnvVars",
"command": "pwsh.exe",
"args": [
"-ExecutionPolicy",
"Bypass",
"-File",
"${workspaceFolder}/load_env_vars.ps1"
],
},
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "makon_front (pwsh)",
"request": "launch",
"type": "dart",
"args": ["--dart-define",
"FIREBASE_WEB_CONFIG=${env:FIREBASE_CONFIG_WEB}",
"--dart-define",
"USE_FB_EMULATOR=${env:USE_FB_EMULATOR}"],
"preLaunchTask": "LoadEnvVars"
},
{
"name": "makon_front (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "makon_front (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
],
}
I have tested the actual string in dart pad, and it works fine. I have also ran const bool.hasEnvironment in the Debug Console, it returns a false. Clearly the env vars are not loading in the debug console before dart launch. How do I do that?

How to configure launch.json for NextJs and Typesctipt

I am trying to create a launch.json file for the following repo:
https://github.com/zakariamofaddel/shopify-nextjs-template
I have tried both the default VS Code node template and the NextJs launch file.
VS code default Node generated .vscode/launch.json file
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\server\\index.ts"
}
]
}
I am able to run yarn dev successfully and use console.log:
https://github.com/zakariamofaddel/shopify-nextjs-template/blob/38c700d8706818aa12d892b3f1193a969919e003/package.json#L9
I found the solution after adding the TypeScript Debugger extension in VS code.
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Localhost",
"type": "node",
"request": "launch",
"args": [
"${workspaceRoot}\\server\\server.ts"
],
"runtimeArgs": [
"-r",
"ts-node/register/transpile-only"
],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"NODE_ENV": "development"
}
}
]
}

How to attach debugger to yarn start command

I have the following command yarn start-package with package being the project for package
I have configured the following launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Package Debuger",
"preLaunchTask": "yarn start-package"
}
]
}
I have configured the following tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "yarn start-package",
"type": "npm",
"script": "start",
"path": "packages/package",
"problemMatcher": []
}
]
}
Now whenever i start debugger, it will never attach because the prelaunch task is starting the developing server and never finishes. How can i attach the debugger to the yarn start-package command correctly?
Note: I am using wsl for running the yarn command if it matters

Preload module in VSCode Launch debug

I'm trying to configure my VSCode launch.json for my node app. My node command for starting the app uses the -r flag to preload a module (dotenv/config). How do I configure that in my launch.json? I can't figure it out.
My run command:
node -r dotenv/config server.js
My launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "My Launch",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceRoot}\\myapp\\src\\server.js",
"env": {
"MY_CONFIG_PATH": ".env"
}
}
]
}
I am also struggling with adding the preLoadedModules in my launch.json, but if you are just wanting to load your environment file you can add "envFile":"${workspaceFolder}/.env" to your launch.json and your app should run.
Example below:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\dist\\index.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"envFile": "${workspaceFolder}/.env",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}

How to run a command in Visual Studio Code with launch.json

Is there a way to execute an ssh command when debugging a project with .vscode/launch.json?
For example: ssh -i xxxxx.
Or is it possible to create a command that you can run from the F1 command palette pop-up? Something like RunCustomCommandxx.
You can define a task in your tasks.json file and specify that as the preLaunchTask in your launch.json and the task will be executed before debugging begins.
Example:
In tasks.json:
For version 0.1.0:
{
"version": "0.1.0",
"tasks": [{
"taskName": "echotest",
"command": "echo", // Could be any other shell command
"args": ["test"],
"isShellCommand": true
}]
}
For version 2.0.0 (newer and recommended):
{
"version": "2.0.0",
"tasks": [{
"label": "echotest",
"command": "echo", // Could be any other shell command
"args": ["test"],
"type": "shell"
}]
}
In launch.json:
{
"configurations": [
{
// ...
"preLaunchTask": "echotest", // The name of the task defined above
// ...
}
]
}
Tasks documentation: https://code.visualstudio.com/docs/editor/tasks
Launch configuration: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
The format changed. Visual Studio Code can create the tasks.json file for you. Inside your launch.json file and inside any configurations object, just define preLaunchTask to force auto-creation of the tasks.json template file:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "launch program",
"skipFiles": [ "<node_internals>/**"],
"preLaunchTask": "myShellCommand",
"program": "${workspaceFolder}/test.js"
}
]
}
If you do not have file tasks.json configured:
launch the debugger. Visual Studio Code will inform you: "could not find the task
myShellCommand"
select Configure Task → Create tasks.json file
from template → Others
Your tasks.json template file will then be created for you. Add your command to command and the name you put in preLaunchTask to label:
{
"version": "2.0.0",
"tasks": [
{
"label": "myShellCommand",
"type": "shell",
"command": "echo goodfood"
}
]
}
For me, I just needed an environment variable, which is different. You don't want a task for this because (at least for me) it doesn't work when the launcher is run.
Thanks to here, I got it working like this, inside my launcher (launch.json) entry:
"environment": [{
"name": "ENVIRONMENT_VARIABLE_NAME",
"value": "${workspaceFolder}/lib" //Set to whatever value you want.
}],
My version of the configuration allows to just run a defined task and carried on (in my case, the task is to run the currently open Groovy file):
"configurations": [
{
"name": "Launch groovy",
"request": "launch",
"type": "coreclr",
"preLaunchTask": "groovy",
"program": "cmd.exe",
"args": ["/c"],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"internalConsoleOptions": "neverOpen"
}
]
And the task:
"tasks": [
{
"label": "groovy",
"type": "shell",
"command": "groovy ${file}"
}
]
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch app",
"program": "lib/main.dart",
"request": "launch",
"type": "dart"
},
{
"name": "Build an APK Release",
"command": "flutter build apk --release",
"request": "launch",
"type": "node-terminal"
},
{
"name": "Install an APK on a device",
"command": "flutter install",
"request": "launch",
"type": "node-terminal"
}
]
}
for flutter developers who is searching how to run flutter build commands.
in tasks.json add
"tasks": [
{
"type": "flutter",
"command": "flutter",
"args": [
"pub",
"run",
"build_runner",
"build",
"--delete-conflicting-outputs"
],
"problemMatcher": [
"$dart-build_runner"
],
"group": "build",
"label": "flutter: flutter pub run build_runner build --delete-conflicting-outputs"
},
]
then from vscode you can try "run task" it will show you flutter: flutter pub run build_runner build --delete-conflicting-outputs
this way you don't need to memorize and type to terminal source code generation/build commands