Let's say I have a pure node.js file.js
as the following:
const fs = require('fs')
console.log("Hello World")
If I execute it inside Vim with :%terminal node
it works perfectly fine. I see the same results I'd see executing it from the command line with node file.js
.
My problem starts with node.js modules. On node.js, modules are .mjs
files. Let's say I have the following file.mjs
code:
import fs from 'fs'
console.log('Hello World')
The way node.js differentiates a module from a normal file is by its file extension, js
files are pure node.js and mjs
files are modules. Both of them can be executed from the command line as isolated scripts with the commands node file.js
and node file.mjs
. However, if I try to execute this mjs
file with :%terminal node
inside Vim it gives me the error :
SyntaxError: Cannot use import statement outside module
That's exactly the same error I'd receive if I had renamed file.mjs
to file.js
and tried to execute it with node file.js
. My assumption is that Vim sees it as a js
file even though its extension is mjs
...
When I run :echo &ft
inside Vim it prints javascript
for both mjs
and js
files. I'm not completely sure if what I want to do is possible. From the command line it works perfectly fine executing mjs
files, but if I try executing them from Vim with the :terminal
command, like :%terminal node
, it fails. Does anyone have any idea of a workaround for this issue? In a way that allows me to execute node.js modules from inside Vim?
:terminal node %
—the range version using the lines as input, while the argument version gives the file name to the node process:terminal node %
it executes themjs
file properly. However, I lose the ability to select lines and execute only them doing that. That was an important feature for me... So, apparently the problem is that Vim creates ajs
file with the selected lines when I use the range version. Then when it tries to execute it it won't be seen as a module. I'm not sure if I can change this behavior with the range version... I'm considering creating a function that creates a temporarymjs
file with the selected lines just for executing it with a second command.:%terminal node
runs node and passes each line in the buffer to the node REPL by giving the line to the process's input. You do have the module part right, but there's no extra files or anything, just stdin. You could create a temp file as you mentioned though.%terminal node
syntax. Does that mean that it'd be possible to continuously execute parts of the code inside the same terminal? So we can create something like a Jupyter notebook just using Vim?