Is there an obvious way to run this JavaScript snippet?
--- js code ---
require('moment')
console.log('--- start')
console.log('moment().format(): ', moment().format())
console.log('--- end')
--- output ---
[Running] node "c:\lab\temp-ackikjupmw.js"
--- start
C:\lab\temp-ackikjupmw.js:3
console.log('moment().format(): ', moment().format())
^
ReferenceError: moment is not defined
at Object.
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:160:18)
at node.js:449:3
[Done] exited with code=1 in 0.189 seconds
Hi @rana , do you have moment npm package installed in global or in C:\lab\node_modules?
Hi @formulahendry,
I love code-runner, very helpful. Hoping to use it more!
yes, the library is in 'C:\labnode_modules\moment'. all node modules present after install. not sure about configuring setting up js with vscode-code-runner. Would be great if there was a little more documentation for JavaScript; otherwise, javascript calls without module references run well. Is this a simple case of user not configuring the tool?
Here are some additional questions i would love to see answer in documentation with an example:
Thank you!
Rana
Hi @rana ,
For question 1, require('some-lib') is supported. Actually, running js with Code Runner has no difference with running your js file as node C:\lab\your-file.js in shell. Could you please try run node C:\lab\your-file.js in your shell (bash or CMD) to see the result?
For question 2, do you want to run js with es6 syntax?
You could install the babel-node: npm install -g babel-cli
And set code-runner.executorMap as below:
{
"code-runner.executorMap": {
"javascript": "babel-node"
}
}
@formulahendry @rana and anyone else who happens along to here, here's how to run your ES6/7 code without having to install babel-cli globally (considered not to be a best practice)
"code-runner.executorMap": {
"javascript": "$workspaceRoot/node_modules/.bin/babel-node --harmony $fullFileName"
}
The --harmony flag will be optional based on how you are using babel-node
@formulahendry thanks for such a great product - providing the necessary file/folder locations so this can be accomplished is awesome and necessary in a node world where all imports/requires are relative to the file performing the import
Works faster with npm i esm then:
"code-runner.executorMap": {
"javascript": "node -r esm $fullFileName"
}
Most helpful comment
Works faster with
npm i esmthen:"code-runner.executorMap": { "javascript": "node -r esm $fullFileName" }