Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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

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:

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!

like image 546
slideshowp2 Avatar asked Sep 20 '16 03:09

slideshowp2


People also ask

Why does the Bash unexpected token syntax error occur?

Why the Bash unexpected token syntax error occurs? As the error suggests this is a Bash syntax error, in other words it reports bad syntax somewhere in your script or command. There are many things that can go wrong in a Bash script and cause this error.

How to fix SyntaxError-unexpected token import/exports?

Error: SyntaxError: Unexpected token import or SyntaxError: Unexpected token export Solution: Change all your imports as example And also change your export default = foo; to module.exports = foo; Show activity on this post. In case that you still can't use "import" here is how I handled it: Just translate it to a node friendly require.

What is NPM error no such file or directory package?

no such file or directory package.json Usually, this error comes when npm commands are running and package.json is not found in the application root This error gives during Below npm command running There are no ways we can create a package.json file npm init command creates package.json that is filled with values that are entered by the user.

What is package package JSON in Node JS?

package.json is a JSON configuration file of a nodejs project which contains metadata of an application + dependencies etc. In NPM based applications such as nodejs, Angular, VueJS, and ReactJS applications, the package.json file location is the application root.


1 Answers

The documentation states that:

On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.

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.

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).

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:

#! /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.

like image 184
slebetman Avatar answered Oct 04 '22 05:10

slebetman