prudhvireddy
prudhvireddy

Reputation: 226

node js link commands running error

i am creating node commands using node bin object from package.json but those are failed to execute due to microsoft javascript run time. how to point node environment

{ 
"name": "test",
 "version": "1.0.0",
 "description": "",
  "main": "index.js",
 "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
  },
 bin":{
"runMy":"index.js"
 },
 "author": "",
"license": "ISC"
}

index.js file have console.log("Happi")..

i used npm link command to "link" command to link the commands globally.

When i am running the "runMy" command i am getting following microsoft error enter image description here

enter image description here

Upvotes: 0

Views: 99

Answers (1)

rsp
rsp

Reputation: 111268

You can try few things that may solve the problem. I don't have Windows to test it but here's what you can try:

First, make sure to add a shebang line as the first line of your script:

#!/usr/bin/env node

So the script would look like this:

#!/usr/bin/env node
console.log("Happi");

Note that there is no space or anything before the shebang line - the "#" hash character is the first character of the file.

That line is not used by the Windows system - it is used on Unix-like systems - but the Node's cmd-shim should install a correct wrapper on Windows when it sees that line.

I am not sure if running npm link is enough or running npm install ... to install that module is needed for the cmd-shim to take effect.

The other thing you may try is to change:

"runMy": "index.js"

to:

"runMy": "node index.js"

This should work in the "scripts" section in package.json. I'm not sure if that will work in the "bin" section.

There is also a wrap-cmd module that should let you wrap your script manually, see https://www.npmjs.com/package/wrap-cmd

And last, you can write your own .cmd or .bat file that would run "node your-script.js". An example.bat file could look like:

@echo off
node c:\path\to\your_program.js

It should work as long as you have node in your PATH.

Upvotes: 1

Related Questions