Open file with line numbers in editor from Node.js.
The main functionality is extracted from react-dev-utils with slight modifications so that it can be used as a standalone package. The original source code is licensed under MIT.
Also added column number support.
There are also a few other existing packages with the same purpose:
However, both expects env variables like EDITOR to be set in order to open files. This package infers the editor to open by checking current running processes before falling back to env variables.
On the other hand,react-dev-utils includes many other utilities and dependencies and is thus not suitable for standalone usage.
const launch = require('launch-editor')
launch(
// filename:line:column
// both line and column are optional
// `file://` URIs are also supported
'foo.js:12:34',
// try specific editor bin first (optional)
'code',
// callback if failed to launch (optional)
(fileName, errorMsg) => {
// log error if any
},
)An express/connect/webpack-dev-server compatible middleware is also available:
const launchMiddleware = require('launch-editor-middleware')
app.use('/__open-in-editor', launchMiddleware())The middleware factory function accepts the following arguments (all optional, the callback can be in any position as long as it's the last argument):
- A specific editor bin to try first. Defaults to inferring from running processes, then fallback to env variables like
EDITORandVISUAL. - The root directory of source files, in case the file path is relative. Defaults to
process.cwd(). - a callback when it fails to launch the editor.
To launch files, send requests to the server like the following:
/__open-in-editor?file=src/main.js:13:24
| Value | Editor | Linux | Windows | macOS |
|---|---|---|---|---|
appcode |
AppCode | ✓ | ||
atom |
Atom | ✓ | ✓ | ✓ |
atom-beta |
Atom Beta | ✓ | ||
brackets |
Brackets | ✓ | ✓ | ✓ |
clion |
Clion | ✓ | ✓ | |
code |
Visual Studio Code | ✓ | ✓ | ✓ |
code-insiders |
Visual Studio Code Insiders | ✓ | ✓ | ✓ |
codium |
VSCodium | ✓ | ✓ | ✓ |
cursor |
Cursor | ✓ | ✓ | ✓ |
emacs |
Emacs | ✓ | ||
idea |
IDEA | ✓ | ✓ | ✓ |
notepad++ |
Notepad++ | ✓ | ||
pycharm |
PyCharm | ✓ | ✓ | ✓ |
phpstorm |
PhpStorm | ✓ | ✓ | ✓ |
rider |
Rider | ✓ | ✓ | ✓ |
rubymine |
RubyMine | ✓ | ✓ | ✓ |
sublime |
Sublime Text | ✓ | ✓ | ✓ |
vim |
Vim | ✓ | ||
visualstudio |
Visual Studio | ✓ | ||
webstorm |
WebStorm | ✓ | ✓ | ✓ |
zed |
Zed | ✓ | ✓ | ✓ |
You can use the LAUNCH_EDITOR environment variable
LAUNCH_EDITOR=codiumLAUNCH_EDITOR=my-editor-launcher.sh# gets called with 3 args: filename, line, column
filename=$1
line=$2
column=$3
# call your editor with whatever args it expects
my-editor -l $line -c $column -f $filenameOn Windows, launch-editor refuses to open files whose resolved path is a UNC path, that is, a path that starts with \\, such as \\server\share\file.js.
When a UNC path points at a remote host, any filesystem operation launch-editor performs on it (such as the fs.existsSync check used to verify the file before launching the editor) causes Windows to connect to that host over SMB. An attacker who controls the requested file path could point it at a server they control and abuse this to leak the current user's NTLM credentials: Windows automatically attempts NTLM authentication against the remote host, sending the user's NTLM challenge-response, which the attacker can capture for offline cracking or relay to another service (an NTLM relay attack). Because the file path frequently originates from untrusted input (for example, the file query parameter handled by launch-editor-middleware), launch-editor rejects UNC paths up front so it never performs any filesystem operation on them.
When a UNC path is rejected, the error callback is invoked with:
UNC paths are not supported on Windows to avoid security issues.
If you need to edit a file on a network share, map the share to a drive letter first and pass that path (e.g. Z:\file.js) instead.