[英]JS file not running with Node.js in terminal
我正在尝试在命令提示符中使用node.js运行js文件,但终端正在说这而不运行文件。
>hello.js:1
(function (exports, require, module, __filename, __dirname) {
console.log(hello world)
^^^^^
SyntaxError: missing ) after argument list
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
新的错误日志:
hello.js:1
(function (exports, require, module, __filename, __dirname) {
console.log(hello world)
^^^^^
SyntaxError: missing ) after argument list
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
解决:cd进入文件所在的文件夹,然后使用node打开js文件
注意,在错误日志中,hello world不是字符串,看起来像两个不同的变量。 想想打算是'hello world'
。 Node正在抱怨参数列表,因为代码被解释为类似console.log(someVariableFoo, someVariableBar)
的参数列表,因为hello
和world
之间有空格,并且文本没有用引号引起来。
JavaScript中的字符串如下所示:
const a = 'Hello World'; // single quote
const b = "Hello World"; // double quote
const c = `Hello World`; // template literal
hello.js:1(函数(导出,需求,模块,__ filename,__ dirname){console.log( hello world )
通过MDN进一步了解字符串https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String
(function (exports, require, module, __filename, __dirname) {
console.log("hello world");
}
在引号内打个招呼世界,并确保关闭大括号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.