VS Code Extension - How to set NODE_EXTRA_CA_CERTS? - node.js

I am creating an extension for VS Code. In that, before connecting to database, I need to set the env variable NODE_EXTRA_CA_CERTS to path to certificate file. Normally, If I am running any javascript code(myFile.js) that connects to my database, I run following command in terminal -
export NODE_EXTRA_CA_CERTS='path-to-certificate-file'
node myFile.js
Now, my extension has code that connects to my database, but how do i set the env variable NODE_EXTRA_CA_CERTS before running the extension ?
I tried following ways but it didn't work -
Used process.env.NODE_EXTRA_CA_CERTS='path-to-certificate-file' inside extension code.
Set this env variable in launch.json
"configurations": [
{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}",
"osx": {
"env": {"NODE_EXTRA_CA_CERTS":"path-to-certificate-file"}
}
}
I added this line in my .zshrc file
export NODE_EXTRA_CA_CERTS='path-to-certificate-file' .
How env variables are set for VS Code Extension? Is there any reason or am i doing anything wrong which is why it isn't working ?

Related

VS Code + Node Launch + Run Time Args

Currently attempting to setup VS Code to run a node app, however, unable to get it to work correctly. I can run it however manually without an issue. With the goal once it's running to be able to attach to the node process for breakpoint debugging:
Manual operation that works:
navigate to the directory: /Users/me/source/app_modules/main-api/
run nvm use 12 to be using v12.22.1
run node -r esm dev-server.js
Runs correctly, and loads the esm package module so imports work correctly.
When running with launch.json:
{
"version": "0.2.0",
"configurations": [
{
"envFile": "${workspaceFolder}/app_modules/main-api/.env.local",
"type": "node",
"request": "launch",
"name": "Launch API",
"cwd": "${workspaceFolder}/app_modules/main-api/",
"runtimeVersion": "12",
"runtimeArgs": ["-r esm"],
"program": "${workspaceFolder}/app_modules/main-api/dev-server.js",
}
]
}
Running with this results in: /Users/me/.nvm/versions/node/v12.22.12/bin/node: bad option: -r esm
It's not finding the node_modules even though I request it to use the main-api directory as the main working directory.

Unable to change environmental variables in node.js in Visual Studio Code

I'm new here and I'm new with this programming language, so maybe my question is something very obvious.
I am trying to set an environmental variable in Node.js in Visual Studio Code (for example NODE_ENV to production). I've tried setting it using an .env file and using the dotenv package. This does not seem to work, and when I try to print the variable to the console it prints 'undefined'. This is the part that I have included in the .env file:
NODE_ENV = production
This is a part of the js file that I try to run:
const path = require('path')
require('dotenv').config()
console.log(process.env.NODE_ENV)
When I run this 'undefined' gets printed to the console. I also tried passing the place of the file as an argument with the config for dotenv, like this. That did not work.
I even tried to set te environmental variables directly via launch.json like this:
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${file}",
"env": {
"NODE_ENV" : "production",
}
}
]
}
That also did not work.
Why is this? Why isn't Visual Studio Code able to change the environmental variables?
Thanks in advance.
According to the docs of dotenv there should be no space between the variable name and its value.
The syntax is: VARNAME=VALUE
So your .env file should be
NODE_ENV=production

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()

Node Jest- Config paths must be specified

I am running Jest test cases into a reactjs project. I am trying to debug my jest test cases in VS code. The tests run ok on command line. But when I launch debugger in VS code I see error.
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Tests",
"program": "${workspaceRoot}/xxx/xxx/node_modules/jest/bin/jest",
"args": [
"-i"
],
"internalConsoleOptions": "openOnSessionStart",
"outFiles": [
"${workspaceRoot}/xxx/xxx/**/*"
]
}
]
}
Error
Debugger listening on ws://127.0.0.1:31182/2bf113f1-002f-49ed-ad91-5510affd172a
Debugger attached.
Error: Could not find a config file based on provided values:
path: "/Users/xxx/xxx/xxx-ui"
cwd: "/Users/xxx/xxx/xxx-ui"
Configh paths must be specified by either a direct path to a config
file, or a path to a directory. If directory is given, Jest will try to
traverse directory tree up, until it finds either "jest.config.js" or
"package.json".
Just specify -c option in your launch config:
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"{$PathToNPMRoot}/node_modules/jest/bin/jest.js",
"--runInBand",
"-c", "{$PathToConfig}/jest.config.js",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
},
You will have to replace {$PathToNPMRoot} and {$PathToConfig}
Source
I'll tell you what did the trick for me.
As documentation says, in case that your code is not in the root of your project folder you should create a new config file to specify where jest is located in z .vscode folder, for example:
.vscode/settings.json
{
"jest.pathToJest": "functions/node_modules/.bin/jest"
}
(You should replace "functions" to your own folder name, in case you have your code on your project's root you won't need to do the previous step)
After doing that I was still getting the same error as you even though I configured correctly the path to jest and my jest configuration was specified correctly on my package.json, then I moved my jest configuration to jest.config.js file on the root of my project and that was all, jest was now up and running after I used the command Jest: Start Runner.
//jest.config.js on root dir
module.exports = { verbose: true };
I hope you can get it working
cheers!
I never created a Jest config file. Instead, I just set the working directory in the VSCode launch.json file:
How to configure vs code working directory in the 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": [
// https://jestjs.io/docs/troubleshooting
{
"name": "Debug Jest Tests for Modules",
// https://stackoverflow.com/questions/47540627/
// how-to-configure-vs-code-working-directory-in-the-launch-json
"cwd": "${workspaceRoot}/pathToMyJestModuleTestDir",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
// https://stackoverflow.com/questions/60372790/
// node-v13-jest-es6-native-support-for-modules-without-babel-or-esm
"--experimental-vm-modules",
"${workspaceRoot}/pathToMyJestModuleTestDir/node_modules/jest/bin/jest.js",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
Values are for illustrative purposes only. They do not reflect what I use on my development machines.
The main thing to note here is the following line:
"cwd": "${workspaceRoot}/pathToMyJestModuleTestDir",
That line is explained in the SO Thread mentioned at the top of this answer.
I am new to Jest, as so I had this problem several times! and came back here several times, no solution! nothing worked. No matter how stupid it sounds, the problem was lacking the package.json, you cannot run without it. Maybe it is somewhere on the documentation, I am not very patient to keep reading documentation! Thus, test the obvious: if you created your package.json!!!
Just type in the following and you will be good to go.
npm init

Can I run/debug Heroku Node.js app locally with VSCode?

TL; DR
On Microsoft VSCode v1.6.1, how to:
Properly set runtime executable?
Properly pass Heroku arguments?
Run Heroku Node.js app?
Debug Heroku Node.js app?
Details
I have created a Heroku Node.js application, which is launched using the CLI command:
heroku local web
and successfully starts at port 5000.
I am trying to debug it using Microsoft Visual Studio Code, using the following launch.json configuration:
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/app.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": "/usr/local/bin/heroku",
"runtimeArgs": [
"local web",
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": false,
"outFiles": []
}
But VSCode is automagically passing --debug-brk argument to heroku, causing the error:
/usr/local/bin/heroku --debug-brk=23080 'local web' app.js
! `--debug-brk=23080` is not a heroku command.
! See `heroku help` for a list of available commands.
VSCode also does not find heroku command without its full path (seems like it is not loading PATH environment variable).
Any ideas about how to setup the editor?
The following solution works for me:
1) In your procfile add the parameter --debug to the node process
web: node --debug server.js
By default the debugger listens in the port 5858
2) Once you have the node process running, open VSCode and add the following configuration to your launch.json file
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858
}
3) Finally, click the play button in VSCode with the option "Attach to Process" and it should debug your process.
The following solution worked for me.
In my package.json "scripts", I added:
"debug": "node --inspect-brk server.js"
Then, in launch.json I added an envFile entry to the default "Launch via NPM" configuration, which now looks looks like this:
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"debug"
],
"port": 9229,
"envFile": "${workspaceFolder}/.env"
}
The above solution enables the VSCode debugger to run my server via an npm script, and my server runs with the env vars set in my .gitignore'd .env file, just like in the "regular" Heroku node.js workflow.
I struggled with this as for some reason the solution propsed didn't work for me. However, an alternate solution did so I thought I would share.
From the default debugging options in VS Code choose
Attach by Process ID
When you start the debugger with this configuration it should list available processes to attach to and one should be simply be server.js. This requires manually attaching each time, and if the other automatic attachment works for you that may be better, but this is still a workable solution.

Resources