@jsenv/execution
v6.1.0
Published
[![github package](https://img.shields.io/github/package-json/v/jsenv/jsenv-execution.svg?label=package&logo=github)](https://github.com/jsenv/jsenv-execution/packages) [![ci status](https://github.com/jsenv/jsenv-execution/workflows/ci/badge.svg)](https:
Downloads
420
Readme
jsenv-execution
Introduction
@jsenv/execution
is capable to execute a javaScript file on a browser or Node.js.
It has the following exports
execute
How to use
Using a basic project setup we'll see how to use execute
to create script capable to execute file inside chromium or node.js.
Basic project setup
Create basic project file structure
— see ./docs/basic-project
Install dependencies
npm install
Browser execution with chromium
node ./execute-chromium.js index.js
browser
will be logged in your terminal.
Node execution
node ./execute-node.js index.js
node
will be logged in your terminal.
Use execute
to debug file withing vscode
What if you could debug inside node.js the file currently opened in vscode?
- Add a launch configuration in
basic-project/.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "jsenv-node",
"type": "node",
"request": "launch",
"protocol": "inspector",
"program": "${workspaceFolder}/execute-node.js",
"args": ["${relativeFile}"],
"autoAttachChildProcesses": true,
"sourceMaps": true,
"sourceMapPathOverrides": {
"/*": "${workspaceFolder}/*"
},
"smartStep": true,
"skipFiles": ["node_modules/**", "<node_internals>/**/*.js"]
}
]
}
- Start a debugging session using
jsenv node
I made a video of the debugging session inside vscode. The gif below was generated from that video.
Sometimes vscode fails to auto attach child process debugging session. According to my experience it happens mostly on windows. When it happens you must manually attach it.
To do that you can add an other configuration in your launch.json
.
{
"name": "jsenv-node-attach-child",
"type": "node",
"request": "attach",
"port": 3456,
"smartStep": true,
"sourceMaps": true,
"sourceMapPathOverrides": {
"/*": "${workspaceFolder}/*"
},
"skipFiles": ["node_modules/**", "<node_internals>/**/*.js"]
}
Using this configuration also means your child process debug session listens at 3456
. You must update the execute-node script to force 3456
port like this:
const { execute } = require("@jsenv/execution")
const { launchNode } = require("@jsenv/node-launcher")
execute({
projectPath: __dirname,
launch: (options) => launchNode({ ...options, debugPort: 3456 }),
fileRelativePath: `/${process.argv[2]}`,
})
If you want to know more about execute
, there is a more detailed documentation on it.
— see execute
documentation