Node.js命令行工具在记事本中打开而不是被执行 [英] Node.js command line tool opens in notepad instead of being executed

查看:196
本文介绍了Node.js命令行工具在记事本中打开而不是被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个简单的node.js命令行工具来解决这个概念。我正在关注教程。使用 npm init ,我创建了一个名为 lb-model-discovery 的项目。这就是package.json

I try to create a simple node.js command line tool to get around this concept. I am following this tutorial. Using npm init, I have created a project named lb-model-discovery. This is the content of the package.json

{
  "name": "lb-model-discovery",
  "version": "1.0.0",
  "description": "loopback model discovery command line tool",
  "main": "index.js",
  "bin" :{
      "lb-discover":"./index.js"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "seya obey",
  "license": "ISC"
}

这是 index.js(非常简单)的内容在项目的根目录:

console.log('hello world');

之后,我从文件夹中执行了此命令(如上一教程所建议):

After that I executed this command from within the folder (as suggested by the above tutorial):

npm install -g

这可以将我的工具正确安装为全局节点命令行模块。进入系统中的节点模块的全局存储库(Windows),我可以看到这是在npm文件夹中创建的文件: lb-discover.cmd 。其内容如下:

This correctly install my tool as a global node command line module. Going into the global repository of node modules in my system (windows), I can see that this is the file created in the npm folder: lb-discover.cmd. And here is its content:

@"%~dp0\node_modules\lb-model-discovery\index.js"   %*

但是现在当我从命令行提示符下运行新工具时:

But now when I run my new tool from the command line prompt:

$ lb-discover

它没有在控制台中显示 Hello world消息,而是打开记事本,仅显示index.js文件的内容。

instead of displaying a "Hello world" message in the console, it opens notepad instead and merely display the content of the index.js file.

我是什么做错了吗?如何执行自定义的node.js命令行?

What am I doing wrong? How can I execute my custom node.js command line?

任何帮助将不胜感激。

推荐答案

#!/ usr / bin / env节点添加到 lb-model-discovery / index的第一行.js 。然后再次安装该软件包。

Add #!/usr/bin/env node to the first line of lb-model-discovery/index.js. Then install the package again.

将生成此脚本

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\node_modules\lb-model-discovery\index.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node  "%~dp0\node_modules\lb-model-discovery\index.js" %*
)

而不是

@"%~dp0\node_modules\lb-model-discovery\index.js"   %*

来源: https://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm

这篇关于Node.js命令行工具在记事本中打开而不是被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆