Nir Berko
Nir Berko

Reputation: 1428

node.js command line application not runs

I am building a node js command line pacakge, and when I am linking one of my projects to the command line project when I am trying to use the command, it's trying to open it like a js file on windows, and not as a node js application.

this is the command line project package.json:

{
  "name": "sitemap-generator",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "commander": "^2.19.0",
    "esm": "^3.0.84",
    "fs": "0.0.1-security"
  },
  "bin": {
    "smg": "./cli.js"
  },
  "engines": {
    "node": ">=9.5"
  }
}

and the index.js file:

'use strict';

const program = require('commander');

program
    .version('0.0.1')
    .description('Contact management system');

program.parse(process.argv);

the error:

enter image description here

Upvotes: 0

Views: 214

Answers (2)

Nir Berko
Nir Berko

Reputation: 1428

The answer is to add the following line to the top of the cli.js file: #!/usr/bin/env node

so the file looks like this:

#!/usr/bin/env node

const program = require('commander');

program
    .version('0.0.1')
    .description('Contact management system');

program.parse(process.argv);

@mklement0 - #!/usr/bin/env node is an instance of a shebang line...

Upvotes: 1

S.V.
S.V.

Reputation: 1221

I believe the command you want to run is node index.js [arguments] instead of opening the js file directly.

Upvotes: 1

Related Questions