I'm trying to create a package that can be installed globally. My package.json has:
我正在尝试创建一个可以全局安装的软件包。我的package.json有:
{
"name": "my-new-package",
"version": "1.0.0",
"main": "index.js",
"preferGlobal": true,
"bin": {
"my-new-package": "index.js"
}
}
I can run it with "node index.js" and I can publish it to npm.
我可以使用“node index.js”运行它,我可以将它发布到npm。
The problem is that when I install it globally npm i -g my-new-package
, on Windows the file my-new-package.cmd
is:
问题是,当我在全球安装npm i -g my-new-package时,在Windows上my-new-package.cmd文件是:
@"%~dp0\node_modules\my-new-package\index.js" %*
@“%~dp0 \ node_modules \ my-new-package \ index.js”%*
So when I execute my-new-package
it opens the javascript file with the default editor.
因此,当我执行my-new-package时,它会使用默认编辑器打开javascript文件。
In contrast, other global packages use node.exe
. Here's the cute-files.cmd
example:
相反,其他全局包使用node.exe。这是cute-files.cmd示例:
@IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\node_modules\cute-files\cute-files.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\node_modules\cute-files\cute-files.js" %*
btw, I get the same behavior when I install it on Linux.
顺便说一下,当我在Linux上安装它时,我会得到相同的行为。
So how can I configure my package to use node when it's installed globally?
那么如何在全局安装时将我的软件包配置为使用节点?
1 个解决方案
#1
2
Here is how to create a globally executable nodejs package
以下是如何创建全局可执行的nodejs包
mkdir my-new-package
cd my-new-package
first create cat package.json
首先创建cat package.json
{
"name": "my-new-package",
"version": "1.0.0",
"description": "global module my-new-package",
"main": "./lib/index.js",
"bin": {
"my-executable-name": "./bin/test-module.js"
},
"author": "",
"license": "ISC",
"preferGlobal": "true"
}
now make these two dirs as children of dir my-new-package
现在把这两个dirs作为dir my-new-package的孩子
mkdir bin
mkdir lib
here is the another file we need cat lib/index.js
这是另一个我们需要cat lib / index.js的文件
var sayHello = function(name) {
return 'Hello, ' + name;
};
// Allows us to call this function from outside of the library file
// Without this, the function would be private to this file
exports.sayHello = sayHello;
now create a file cat bin/test-module.js
现在创建一个文件cat bin / test-module.js
#!/usr/bin/env node
var lib= require('../lib/index.js');
var greeting = lib.sayHello('hello everyone');
console.log(greeting);
now get into dir my-new-package
where file package.json
lives and issue the global install command
现在进入dir my-new-package文件package.json,并发出全局安装命令
cd my-new-package
npm install -g .
notice the period in above install command
注意上面安装命令中的句点
its output will be something like this
它的输出将是这样的
/home/stens/node-v6.5.0/bin/my-new-package -> /home/stens/node-v6.5.0/lib/node_modules/my-new-package/bin/test-module.js
/home/stens/node-v6.5.0/lib
└── my-new-package@1.0.0
now that its globally installed you can issue this while in any directory
现在它的全局安装你可以在任何目录中发出它
my-executable-name
and it will respond with
它会回应
Hello, hello everyone
Interestingly our new executable lives in nodejs global bin directory as per
有趣的是,我们的新可执行文件存在于nodejs全局bin目录中
which my-executable-name
which responds with
响应
/home/stens/node-v6.5.0/bin/my-executable-name
#1
2
Here is how to create a globally executable nodejs package
以下是如何创建全局可执行的nodejs包
mkdir my-new-package
cd my-new-package
first create cat package.json
首先创建cat package.json
{
"name": "my-new-package",
"version": "1.0.0",
"description": "global module my-new-package",
"main": "./lib/index.js",
"bin": {
"my-executable-name": "./bin/test-module.js"
},
"author": "",
"license": "ISC",
"preferGlobal": "true"
}
now make these two dirs as children of dir my-new-package
现在把这两个dirs作为dir my-new-package的孩子
mkdir bin
mkdir lib
here is the another file we need cat lib/index.js
这是另一个我们需要cat lib / index.js的文件
var sayHello = function(name) {
return 'Hello, ' + name;
};
// Allows us to call this function from outside of the library file
// Without this, the function would be private to this file
exports.sayHello = sayHello;
now create a file cat bin/test-module.js
现在创建一个文件cat bin / test-module.js
#!/usr/bin/env node
var lib= require('../lib/index.js');
var greeting = lib.sayHello('hello everyone');
console.log(greeting);
now get into dir my-new-package
where file package.json
lives and issue the global install command
现在进入dir my-new-package文件package.json,并发出全局安装命令
cd my-new-package
npm install -g .
notice the period in above install command
注意上面安装命令中的句点
its output will be something like this
它的输出将是这样的
/home/stens/node-v6.5.0/bin/my-new-package -> /home/stens/node-v6.5.0/lib/node_modules/my-new-package/bin/test-module.js
/home/stens/node-v6.5.0/lib
└── my-new-package@1.0.0
now that its globally installed you can issue this while in any directory
现在它的全局安装你可以在任何目录中发出它
my-executable-name
and it will respond with
它会回应
Hello, hello everyone
Interestingly our new executable lives in nodejs global bin directory as per
有趣的是,我们的新可执行文件存在于nodejs全局bin目录中
which my-executable-name
which responds with
响应
/home/stens/node-v6.5.0/bin/my-executable-name