简体   繁体   中英

Node.js can't compile one of the installed tests

I am trying to port a PHP project to node.js

I installed node 0.10.26 using the universal installer for mac os x downloaded from node.org. The path env contains the right dir paths to the executables..etc.

I converted the first file/module, tried to check its syntax using

node - p ~/path/with/no/spaces/to/myFile.js

The result is an error that reads:

Minis-Mac:Projects newbnz$ node -p ~/Projects/convert/application/models/mdAPI.js 
[eval]:1
/Users/maat/Projects/convert/application/models/mdAPI.js 
^
SyntaxError: Invalid flags supplied to RegExp constructor 'newbnz'
    at new RegExp (<anonymous>)
    at [eval]:1:1
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at startup (node.js:80:7)
    at node.js:902:3

I went ahead and tried to do the same with one of the test files that get installed with node as such:

Minis-Mac:Projects newbnz$ node -p  /usr/local/lib/node_modules/npm/test/common.js

[eval]:1
/usr/local/lib/node_modules/npm/test/common.js
^
SyntaxError: Invalid flags supplied to RegExp constructor 'local'
    at new RegExp (<anonymous>)
    at [eval]:1:1
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at startup (node.js:80:7)
    at node.js:902:3

I am running it on OS Mavericks.

Disclosure: i am c/c++ dev and a newbie to JS and node.js.

You are passing the -p flag, which according to node --help causes the following:

  -p, --print          evaluate script and print result

And then you pass your application's file path, ~/path/with/no/spaces/to/myFile.js , which is translated by your shell into /Users/maat/Projects/convert/application/models/mdAPI.js .

Now since you used the -p flag, that "path" you thought you were telling node to use to launch your application is actually interpreted as raw javascript as though it was passwed to the eval function.

And given the format of the path (starting with a / and with no enclosing single or double quotes around it), node interprets it as a javascript regular expression and then tries to evaluate it (with incorrect regex syntax) - thus causing the mysterious error in your console.

Long story short, remove the flag and you should get the behaviour that you want.

Since I am doing a port from PHP to Node, I missed an 'array' declaration at an obscure line at the beginning of the file. ie a syntactic error.

After fixing the syntax error, I pushed my luck to see if i can use node -p as a means to check syntax only (posted somewhere in one of the questions on stack overflow), but it still didn't work.

The way I found the error is that I opened a new js file, started copying stmt by stmt till I found it.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM