Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript module reference #29

Closed
rana opened this issue Nov 9, 2016 · 5 comments
Closed

Javascript module reference #29

rana opened this issue Nov 9, 2016 · 5 comments
Labels

Comments

@rana
Copy link

rana commented Nov 9, 2016

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

@formulahendry
Copy link
Owner

Hi @rana , do you have moment npm package installed in global or in C:\lab\node_modules?

@rana
Copy link
Author

rana commented Nov 9, 2016

Hi @formulahendry,

I love code-runner, very helpful. Hoping to use it more!

yes, the library is in 'C:\lab\node_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

@formulahendry
Copy link
Owner

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"
    }
}

@eudaimos
Copy link

eudaimos commented Apr 5, 2017

@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

@MVSICA-FICTA
Copy link

Works faster with npm i esm then:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants