Vscode-code-runner: Javascript module reference

Created on 9 Nov 2016  路  5Comments  路  Source: formulahendry/vscode-code-runner

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. (C:\lab\temp-ackikjupmw.js:3:36)
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

question

Most helpful comment

Works faster with npm i esm then:

"code-runner.executorMap": { "javascript": "node -r esm $fullFileName" }

All 5 comments

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:

  1. Can i use require('some-lib') ? If so, how? May i see an example?
  2. Can i use import syntax "import moment from 'moment'" ? If so, how? May i see 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" }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mjaniec2013 picture mjaniec2013  路  5Comments

w3adventures picture w3adventures  路  3Comments

emadb picture emadb  路  5Comments

0x7FFFFFFFFFFFFFFF picture 0x7FFFFFFFFFFFFFFF  路  3Comments

ranolfi picture ranolfi  路  3Comments