AWS Toolkit + VSCode local testing - node.js

I'm trying to figure out how to use AWS toolkit for vscode. I go to the AWS extension and click Create New SAM Application, point to project directory and it creates a hello world function. Above it, it says Add Debug Configuration. I click that, choose nodejs 12.x and save the launch.json, but I don't get the run option. It still says Add Debug Configuration for some reason. How can I run my lambda functions locally in the console?
The launch.json file generates, but I can never run the code.
launch.json
{
"configurations": [
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "new test:app.lambdaHandler (nodejs12.x)",
"invokeTarget": {
"target": "code",
"projectRoot": "new test/hello-world",
"lambdaHandler": "app.lambdaHandler"
},
"lambda": {
"runtime": "nodejs12.x",
"payload": {},
"environmentVariables": {}
}
}
]
}
I also tried navigating to the hello-world directory in terminal and executing node app.js, but it doesn't return anything
What am I doing wrong? I appreciate the help!

Make sure you have SAM CLI install in local, here are the instructions for installation https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html
Then run the command sam local start-api.
You should be able to access the api at http://127.0.0.1:3000/hello
You can also do the same via vscode by selecting Run > Run without debugging (shortcut: ctrl + F5)

Related

In vscode using node.js, ctrl+F5 always asks for “select environment”. This didn't happen a few weeks ago

Whenever I press F5 or Ctrl+F5, vscode asks me to "select Environment". I have to choose Node.js every time. Somebody has given this solution:
Run > Add Configuration > select Environment. It works for that particular folder.
However, when I change folders the problem persists. How can I set up configurations globally?
The worst part is that this problem started appearing since 4-5 weeks. Before that vscode was automatically debugging & running my files on node.js
I found a workaround.
Installed this free extension code runner.
https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner
I had to set a custom shortcut key for enabling it to run since Ctrl+Alt+N didn't work for me.
The type key is missing in /.vscode\launch.json (Access it from the top left gear icon or open it in vscode), please see #Andy's answer:
{
"version": "0.2.0",
"configurations": [
{
"type": "node", // to avoid the environment Select
"request": "launch",
"name": "Debug File",
"program": "${file}"
}
]
}
If the type key is not there, Vscode asks you about the type.
There is actually an official solution for this and there is no need to install other extensions, as Visual Studio Code comes with support of Node.Js
The configuration file for running and debugging is in .vscode\launch.json, which can be found in the Run panel.↓
Then you can change the configuration file to the desired effect. If you want to run or debug the current file directly, the configuration is like this
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug File",
"program": "${file}"
}
]
}
it is also possible to run the index.js file in the current working directory.
"program": "${workspaceFolder}/src/index.js"
More information:
https://go.microsoft.com/fwlink/?linkid=830387
Debugging current file in VS Code

Azure Functions local HTTP trigger issue and solution

Just wanted to post the steps I took after running into the following error when trying to run an Azure Function HTTP Trigger locally in VS Code:
"[error] Incompatible Node.js version. The version you are using is v13.1.0,
but the runtime requires an LTS-covered major version. LTS-covered versions
have an even major version number (8.x, 10.x, etc.) as per
https://github.com/nodejs/Release#release-plan. For deployed code, change
WEBSITENODEDEFAULT_VERSION to '~10' in App Settings. Locally, install or
switch to a supported node version (make sure to quit and restart your code
editor to pick up the changes)."
(1.) install NVM. Instructions here: https://github.com/nvm-sh/nvm#installing-and-updating
(2.) install NVS. Instructions here: https://github.com/jasongin/nvs
(3.) in your VS code terminal, run:
nvs add 12.16.1
nvs use node/12.16.1/x64
(4.) edit your launch.json to the following:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Node Functions",
"type": "node",
"request": "attach",
"port": 5555,
"preLaunchTask": "func: host start"
}
],
"node": {
"runtimeVersion": "12.16.1"
}
}
(5.) in your VS code terminal, run:
func start

Set env var for Node JS when launching through VS Code

I have Node JS Azure function that when I run locally needs the NODE_TLS_REJECT_UNAUTHORIZED=0 environment variable needs setting for my code to work. This is because I am connecting to an emulated Cosmos DB running locally on my machine which require Node to allow for self signed certificates.
I only want this environment variable setting when I run locally as in production I will be using a real (i.e. non emulated) Cosmos DB instance. Rather than putting and #if debug in my code (or the equivalent for a Node Azure Function) I'd like my VS Code project to set the env var when it is launched.
I've trying following this answers advice but when VS Code launches the program the environment variable is not set as I get a runtime error in my code about self signed certificates not being authorised. My launch.json looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Node Functions",
"type": "node",
"request": "attach",
"port": 9229,
"preLaunchTask": "func: ",
},
],
"environment": [{
"name": "NODE_TLS_REJECT_UNAUTHORIZED",
"value": "0"
}],
}
If I set the env var directly in code using process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 everything is fine. I don't know how I can get VS Code to set this, or if it is even possible.
Please update
"environment": [{
"name": "NODE_TLS_REJECT_UNAUTHORIZED",
"value": "0"
}],
To Be
"env": {
"name": "NODE_TLS_REJECT_UNAUTHORIZED",
"value": "0"
},
Also please check the alternatives.
You can Update package.json scripts to add all environment variables on start like "runLocal": "NODE_TLS_REJECT_UNAUTHORIZED=0 ... your start code " or You can use third party lib called dotenv
this is how it works
01- Create .env file
02- Write all your environment variables key=val NODE_TLS_REJECT_UNAUTHORIZED=0
03- Update package.json scripts to add "runLocal": "NODE_ENV=dev ... your start code "
04- Check if node NODE_ENV is equal dev then load the dotenv
05- Import module in case of node env is local and call config function require('dotenv').config()

Debug web add-in for outlook in visual studio code

I was following the tutorial for creating an advanced Outlook add-in here (Git the Gist)
The above is a node.js-app which uses Yeoman and Microsoft Office Add-in Project Generator. To simply start the add-in, you just type 'npm start' in your root project folder.
But: I want to debug the add-in in Visual Studio Code.
VS Code automatically creates a launch.json file when you want to debug a project. This file looks like this:
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\bsconfig.json"
}
]
However, when I debug I get the following error:
Cannot launch program 'c:\ZTesting\outlooktest\bsconfig.json'; setting
the outFiles attribute might help.
So are these outFiles truly the problem and - if so - what should they be?
And - if not - what is the solution towards debugging a web add-in created with node.js, Yeoman and Microsoft Office Add-in Project Generator?
The bsconfig.json file looks like this:
{
"ui": {
"port": 3000
},
"server": {
"routes": {
"/node_modules": "node_modules"
}
},
"https": {
"key": "./certs/server.key",
"cert": "./certs/server.crt"
},
"watch": true,
"files": "*.*"
}
I did not find out how to debug the add-in above in Visual Studio Code. However it is possible to debug the add-in with the F12-developer-app from Windows 10.
You can find more information about the F12-app here:
Debug add ins using F12 Developer-app from Windows 10
I have the same problem today.
However, there is a tricky way to debug your outlook add-ins if WIN10 F12-app not able to open your html file.
you can use tbody.append to show any value when you debug your add-ins in outlook
var tbody = $('.prop-table');
tbody.append(makeTableRow("asyncResult: ", item));
function makeTableRow(name, value) {
return $("<tr><td><strong>" + name +
"</strong></td><td class=\"prop-val\"><code>" +
value + "</code></td></tr>");
}
I had this same problem. The root cause is that the app does not actually run on the server. npm start actually runs a separate program, called webpack. You can see this by opening package.json. There should be a line like the following:
"start": "webpack-dev-server --mode development --https --key ./certs/server.key --cert ./certs/server.crt --cacert ./certs/ca.crt --port 3000"
When you run npm start, it adds node_modules/.bin/ to the path before running that line. So this script is actually calling an executable called node_modules/.bin/webpack-dev-server. Webpack does not run any of the code in index.js. It "packs" the code, and broadcasts it over https to the client.
Since the app code only runs on the client, it can only be debugged on the client. The use of npm start makes it look like you're writing server code, but the add-in is actually running clientside.

visual studio code node.js debugger fails to attach on standard launch

I tried to use the simple node.js example visualstudio code provides, but unfortunately when node is being started from visual studio code (on OS X) the node gets started with some arbitrary debug-brk, but debugger attachment fails.
When I run the node app manually with --debug-brk = 5858 and then use the attach I can debug my app. Anyone faced the same problem?
P.S I got mono installed from the mono project page .pkg
Here is my launch.js:
{
"version": "0.1.0",
// List of configurations. Add new configurations or edit existing ones.
// ONLY "node" and "mono" are supported, change "type" to switch.
"configurations": [
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Launch app",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "./bin/www",
// Automatically stop program after launch.
"stopOnEntry": true,
// Command line arguments passed to the program.
"args": [],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": ".",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
// Environment variables passed to the program.
"env": { }
},
{
"name": "Attach",
"type": "node",
// TCP/IP address. Default is "localhost".
"address": "localhost",
// Port to attach to.
"port": 5858
}
]
}
Just ran into the same issue... Code couldn't find Node.
Change this line to point to your executable, for example:
"runtimeExecutable": "C:/Program Files/nodejs/node.exe",
Enjoy!
Might be the cluster thing. Try to turn off cluster in ./bin/www, and you will be able to debug it.

Resources