Issue passing Node arguments to PM2 - node.js

I want to start a process using PM2, but it doesn't seem to pick up node arguments relative to the current working directory.
Running this from /path/to/dir/ works:
pm2 start dist/main.js --node-args="-r ./tsconfig-paths-bootstrap.js" -- -c config.json
whereas using this ecosystem file:
{
"apps": [
{
"name": "server",
"script": "dist/main.js",
"instances": 2,
"exec_mode": "cluster",
"cwd": "/path/to/dir/",
"args": [
"-c",
"config.json"
],
"node_args": [
"-r",
"./tsconfig-paths-bootstrap.js"
],
"watch": false
}
]
}
and running:
pm2 start server
gives me the following error:
Error: Cannot find module './tsconfig-paths-bootstrap.js'
How can this be fixed?

Try this:
"node_args": ["-r ./tsconfig-paths-bootstrap.js"]
Check this
And make sure your file is in same directory else you need to give full p ath to file.

Related

How can I call arm-none-eabi-gdb with -x parameter

I want to call arm-none-eabi-gdb with -x parameter when launch debugger after installing cortex-debug. How can I do in launch.json or setting.json, thanks for your help.
You may try adding the following task to our tasks.json the following:
{
"label": "flash",
"type": "shell",
"command": "arm-none-eabi-gdb",
"args": [
"-x=\"${workspaceFolder}/.vscode/flash.gdb\"",
],
"group": "build",
"problemMatcher": [],
"dependsOn":["build"]
},
(of course this assume you have another task called "build" and that your "flash" task depends on it. this is just an example of how to add the -x argument)

How do I run a shell script prior to launch.json in vs code for cucumber-js

So I have a shell script that holds environment variables env.sh:
echo "Running Environment Variables Script"
#########################################################
# Run-time Options
# set to 'dev', 'qa', or 'stg'
export NODE_ENV='qa'
# Running Mode
export HEADLESS='truef'
# Set Debug mode 'true' or 'false'`enter code here`
export EPAY_GUI_AUTOMATION_TEST_DEBUG_MODE='true'
#########################################################
# yadda
# yadda
# yadda
In VS Code, I also am using the following .vscode/tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "loadEnvironmentVariables",
"type": "shell",
"command": ". ${workspaceFolder}/env.sh",
"isBackground": true,
"presentation": {
"reveal": "silent",
"focus": false
}
}
]
}
In VS Code, I also am using the following .vscode/launch.json:
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "foo",
"preLaunchTask": "loadEnvironmentVariables",
"program": "${workspaceFolder}/node_modules/.bin/cucumber-js",
"args": [
"${workspaceFolder}/features/01_MAT_ManualWorkflow_QBO.feature"
]
},
I use the same shell script env.sh in my package.json. This way, I have one set of environment variables I need to manage.
I when I run this in debug mode, the env file is run (I see "Running Environment Variables Script" in the terminal output). But then I get a pop-up saying "The specified task can not be tracked."
I can't seem to get past this issue to be able to run the cucumber-js.
I'm running this on MAcOs.
UPDATE 1
I tried just referring to the npm script, by having the launch.json:
"configurations": [
{
"type": "node",
"request": "launch",
"name": "mat",
"runtimeExecutable": "npm",
"args": [ "mat" ]
},
and where "mat" refers to the package.son:
"mat": ". ./env.sh; node ./node_modules/.bin/cucumber-js --tags #MAT
but that did do jack-squat for me :(

Passing arguments to a node.js package.json bin command or debugging a bin command?

I would like to launch a bin command in debug mode.
But I can't seem to write --inspect-brk=3000 as an argument as it only wants to accept a filename and npm link checks if file exists.
"bin": {
"mycommand": "index.js --inspect-brk=3000",
}
ENOENT: no such file or directory
Any ideas?
I might just make a debug.js that launches the index.js passing the arg and piping the stdin/stdout.
debug.js
#!/usr/bin/env node
// A wrapper to allow starting the index.js in debug mode port is 3000
const { spawn } = require('child_process')
var child = spawn('node', ['--inspect-brk=3000', 'index.js'], { stdio: 'inherit' })
launch.json
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "",
"runtimeExecutable": "${workspaceFolder}/../Tools/someprogramthatrunsmine",
"runtimeArgs": [
"mycommand-debug",
"someinputs"
],
"cwd": "${workspaceFolder}",
//"console": "integratedTerminal",
"port": 3000
}
]
package.json bin command
"bin": {
"mycommand-debug": "debug.js"
},
Run npm link to link the command for local testing.
Clicking the Debug button in Vscode can now breakpoint my index.js code when it is run by a different runtime executable.

Typescript build task in VSCode on Windows 10 with Windows Subsystem for Linux

My VSCode settings (workspace settings in my case) are setup to use bash as the default terminal:
{
"terminal.integrated.shell.windows": "C:\\WINDOWS\\Sysnative\\bash.exe"
}
I need this to be able to debug my app.
My tasks.json looks like that:
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "./tsconfig.json",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
So when I try to build my project / run the build task (i.e. Ctrl + B), I get the following error:
> Executing task: tsc -p "c:\PATH_TO_MY_PROJECT\tsconfig.json" <
error TS5058: The specified path does not exist: 'c:\PATH_TO_MY_PROJECT\tsconfig.json'.
The terminal process terminated with exit code: 1
If I disable bash in my settings an use the default Windows terminal, the build works fine.
I remember it working few VSCode updates ago, but it stopped working in the latest VSCode versions. Not sure how that's related.
I tried to fix this for a while, but finally gave up on the built-in Typescript task and instead just used a custom task:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Typescript watch",
"type": "shell",
"command": "tsc --watch",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": [
"$tsc"
]
}
]
}

Using "preLaunchTasks" and Naming a Task in Visual Studio Code

According to the documentation, it is possible to launch a program before debugging:
To launch a task before the start of each debug session, set the preLaunchTask to the name of one of the tasks specified in tasks.json.
I've not seen example syntax of a "named" task, but the schema documentation reveals a property called taskName. I tried using that to link my launch.json preLaunchTasks to the task, but it didn't work. When I launched my program, Visual Studio Code reported this error:
Could not find a unique task 'launch-core'. Make sure the task exists and that it has a unique name.
My custom "named" task looked something like this:
{
"taskName": "launch-core",
"version": "0.1.0",
"command": "C:\\utils\\mystuff.exe",
// The command is a shell script
"isShellCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
}
I then tried changing the property name from taskName to just name, based on this link. That also didn't work.
Intellisense gives no suggestions of how to name a task.
Does anybody know how to uniquely name a task in the tasks.json file? What is the syntax? What is the property name?
Ultimately I'd like to execute two or three node.js processes before my own node.js app is launched. For example, I'd like to have the following three apps launched before my app is launched into the debugger:
sh -c 'cd ./manager/ && node manager.js'
sh -c 'cd ./adapter/ && node adapter.js'
sh -c 'cd ./core/ && node core.js'
If I'm working on a Windows box, my task might look something like this:
{
"taskName": "core-launch",
"version": "0.1.0",
// The command is tsc. Assumes that tsc has been installed using npm install -g typescript
"command": "start",
// The command is a shell script
"isShellCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
// args is the HelloWorld program to compile.
"args": [
"ACD-Manager",
"/B",
"/D",
"./manager/",
"node",
"manager.js"
]
}
The above task using using the cmd start capability. I'm not sure yet how to make several node tasks launch instead of one, but I can't even get one task to launch because of this task-naming issue.
How do I name a task in the tasks.json file?
FWIW, I'm using VS Code 1.20.1 and here's how I got my preLaunchTask to work:
In launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
...
"preLaunchTask": "npm: build",
}
]
}
In my package.json:
{
...
"scripts": {
"build": "tsc"
...
}
}
So, if it's still relevant, or if someone finds this thread with the same problem, I've just figured it out how it works:
In the tasks.json, you need to create a 'tasks' array - code hint will help you with that - which holds an array of objects. Inside an object, you can have the 'taskName' key-value pair.
Example:
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"args": ["run-script", "webpack"],
"showOutput": "always",
"tasks": [
{
"taskName": "runwebpack",
"suppressTaskName": true
}
]
}
In my case, I had to run the npm run-script webpack command before running my project.
In the launch.json file, the "preLaunchTask": "runwebpack" will work now.
Note: the suppressTaskName is true in my example. Omitting it, or setting it to false will result in VS Code appending the taskName after the command.
A more general approach would be something like this:
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"args": ["run-script"],
"showOutput": "always",
"tasks": [
{ "taskName": "webpack" }
]
}
With the latter example, you can extend the tasks array with other scripts to be run also.
Hint for my usage: npm run-script fetches what to do from the package.json file's scripts object.
Edit: this works with VS Code 1.3.1
For version 2.0.0 configuration you now use label instead of taskName.
package.json:
...
"scripts": {
"tsc": "tsc",
...
}
...
launch.json (My source is in the src directory and tsc compiles to the dist directory):
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"preLaunchTask": "Compile",
"name": "Launch Program",
"program": "${workspaceFolder}/src/index.ts",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"protocol": "inspector",
"sourceMaps": true
}
]
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"type": "npm",
"script": "tsc",
"problemMatcher": []
}
]
}
For vscode 1.36.1 (1.36.1):
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build:tsc",
"type": "npm",
"script": "build:tsc"
},
{
"label": "clean",
"type": "npm",
"script": "clean"
},
{
"label": "build",
"dependsOrder": "sequence",
"dependsOn": ["clean", "build:tsc"]
}
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/dist/main.js",
"preLaunchTask": "build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"console": "integratedTerminal"
}
]
}
Before running node ${workspaceFolder}/dist/main.js, the preLaunchTask will run build task firstly which includes two subtasks: clean and build. It will run clean task firstly, then run build task.
You can specify the label of a task to preLaunchTask option.
The question title is:
"Using “preLaunchTasks” and Naming a Task in Visual Studio Code
I needed to define preLaunchTask***s***.
You can config multiple tasks using the dependsOn property described here
For example, a compound task in your tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Client Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/client"
}
},
{
"label": "Server Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/server"
}
},
{
"label": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}
You can find more information about naming tasks here.
I've only really seen the taskName used in relation to Gulp; I'm sure there are others but nothing that I have much insight into. Perhaps this can get you off to a start with what you already have?
Run a pre-launch task in VSCODE

Resources