简体   繁体   English

节点,当我运行package.json`bin``mand`时,给我一个'意外令牌附近的语法错误`('`

[英]Node, when I run package.json `bin` `command` , give me `syntax error near unexpected token `(' `

when I run package.json bin command , give me syntax error near unexpected token (' ` . 当我运行package.json bin commandsyntax error near unexpected token给我syntax error near unexpected token ('`

package.json : package.json

 "bin": {
    "grabfilenames": "./index.js"
 }

npm link : npm link

/usr/local/bin/grabfilenames -> /usr/local/lib/node_modules/grabfilename/index.js                                                                                                                                                                               
/usr/local/lib/node_modules/grabfilename -> /Users/dulin/workspace/grabfilename  

when I run my cli : 当我运行我的cli

grabfilenames -p /Users/dulin/workspace/learn-jquery

give me an error: 给我一个错误:

/usr/local/bin/grabfilenames: line 1: syntax error near unexpected token `('
/usr/local/bin/grabfilenames: line 1: `const fs = require('fs');'

How to solve it? 怎么解决? Thanks! 谢谢!

The documentation states that: 文件指出:

On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs. 在安装时,npm会将该文件符号链接到用于全局安装的前缀/ bin,或者用于本地安装的./node_modules/.bin/。

This means that npm does nothing special to your file and expect it to be executable on unix. 这意味着npm对您的文件没有任何特殊之处 ,并且期望它在unix上可执行。 Your bin file can be a perl script, a compiled C program, a shell script, a Ruby script or even a node.js javascript app. 您的bin文件可以是perl脚本,已编译的C程序,shell脚本,Ruby脚本甚至是node.js javascript应用程序。

Therefore what causes your app to run is not npm. 因此,导致您的应用运行的原因不是npm。 It is your OS. 这是你的操作系统。 So your script must be executable (as I said, it can even be a compiled binary). 所以你的脚本必须是可执行的(正如我所说,它甚至可以是一个已编译的二进制文件)。

On unix, to automatically execute a script with the correct interpreter you need to have a sh-bang as the first line in the file. 在unix上,要使用正确的解释器自动执行脚本,您需要将sh-bang作为文件中的第一行。 For node.js I generally use this line: 对于node.js我通常使用这一行:

#! /usr/bin/env node

You can generally just use: 你通常可以使用:

#! /whatever/path/to/node

but depending on the OS or even distro node.js may be installed at different locations. 但取决于操作系统甚至发行版node.js可能安装在不同的位置。 So /usr/bin/env is a program that loads your default environment variables which includes $PATH that will allow the shell to automatically find where node.js is installed. 所以/usr/bin/env是一个程序,它加载你的默认环境变量,包括$PATH ,它允许shell自动找到node.js的安装位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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