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

replServer defineCommand #7357

Closed
safarishi opened this issue Jun 22, 2016 · 2 comments · Fixed by #7365
Closed

replServer defineCommand #7357

safarishi opened this issue Jun 22, 2016 · 2 comments · Fixed by #7365
Labels
confirmed-bug Issues with confirmed bugs. doc Issues and PRs related to the documentations. repl Issues and PRs related to the REPL subsystem.

Comments

@safarishi
Copy link

When I try this demo

// repl_test.js
const repl = require('repl');

var replServer = repl.start({});
replServer.defineCommand('sayhello', {
  help: 'Say hello',
  action: function(name) {
    this.write(`Hello, ${name}!\n`);
    this.displayPrompt();
  }
});

then run this in my command line with node repl_test.js
// and got the output with

.sayhello Node.js User
Hello, Node.js User!
SyntaxError: Unexpected identifier
at Object.exports.createScript (vm.js:24:10)
at REPLServer.defaultEval (repl.js:235:25)
at bound (domain.js:287:14)
at REPLServer.runBound as eval
at REPLServer. (repl.js:431:12)
at emitOne (events.js:77:13)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:211:10)
at REPLServer.Interface._line (readline.js:550:8)
at REPLServer.Interface._ttyWrite (readline.js:885:20)

It got the synatax error, and I don't know why?

@mscdex mscdex added the repl Issues and PRs related to the REPL subsystem. label Jun 22, 2016
@mscdex
Copy link
Contributor

mscdex commented Jun 22, 2016

The example in the docs looks incorrect/bad. I think it should be something like console.log() instead of this.write() inside the action function. Otherwise two things happen:

  1. Node tries to interpret the argument passed to this.write() as code (or command if starting with .) to execute.
  2. Node inserts the same argument into the REPL history, which is annoying and would almost always be undesired.

@mscdex mscdex added confirmed-bug Issues with confirmed bugs. doc Issues and PRs related to the documentations. labels Jun 22, 2016
@safarishi
Copy link
Author

Thank you for your answer,I retried this with replace the code
this.write(Hello, ${name}!\n);
to console.log(name);
and it worked all right.
thankyou

@mscdex mscdex added the good first issue Issues that are suitable for first-time contributors. label Jun 22, 2016
akki added a commit to akki/node that referenced this issue Jun 22, 2016
@addaleax addaleax removed the good first issue Issues that are suitable for first-time contributors. label Jun 23, 2016
cjihrig pushed a commit that referenced this issue Jun 23, 2016
Fixes: #7357
PR-URL: #7365
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Brian White <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Fishrock123 pushed a commit that referenced this issue Jun 27, 2016
Fixes: #7357
PR-URL: #7365
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Brian White <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Fishrock123 pushed a commit that referenced this issue Jul 5, 2016
Fixes: #7357
PR-URL: #7365
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Brian White <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. doc Issues and PRs related to the documentations. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants