when I run package.json bin
command
, give me syntax error near unexpected token
(' ` .
当我运行package.json bin命令时,在意外令牌附近给我语法错误('`
package.json
:
"bin": {
"grabfilenames": "./index.js"
}
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
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!
怎么解决?谢谢!
1 个解决方案
#1
12
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. 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.
这意味着npm对您的文件没有任何特殊之处,并且期望它在unix上可执行。您的bin文件可以是perl脚本,已编译的C程序,shell脚本,Ruby脚本甚至是node.js javascript应用程序。
Therefore what causes your app to run is not npm. It is your OS. So your script must be executable (as I said, it can even be a compiled binary).
因此,导致您的应用运行的原因不是npm。这是你的操作系统。所以你的脚本必须是可执行的(正如我所说的,它甚至可以是一个已编译的二进制文件)。
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. For node.js I generally use this line:
在unix上,要使用正确的解释器自动执行脚本,您需要将sh-bang作为文件中的第一行。对于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. 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.
但取决于操作系统甚至发行版node.js可能安装在不同的位置。所以/ usr / bin / env是一个程序,它加载你的默认环境变量,包括$ PATH,它允许shell自动找到node.js的安装位置。
#1
12
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. 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.
这意味着npm对您的文件没有任何特殊之处,并且期望它在unix上可执行。您的bin文件可以是perl脚本,已编译的C程序,shell脚本,Ruby脚本甚至是node.js javascript应用程序。
Therefore what causes your app to run is not npm. It is your OS. So your script must be executable (as I said, it can even be a compiled binary).
因此,导致您的应用运行的原因不是npm。这是你的操作系统。所以你的脚本必须是可执行的(正如我所说的,它甚至可以是一个已编译的二进制文件)。
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. For node.js I generally use this line:
在unix上,要使用正确的解释器自动执行脚本,您需要将sh-bang作为文件中的第一行。对于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. 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.
但取决于操作系统甚至发行版node.js可能安装在不同的位置。所以/ usr / bin / env是一个程序,它加载你的默认环境变量,包括$ PATH,它允许shell自动找到node.js的安装位置。