2016-08-17 1 views
2

전 세계적으로 설치할 수있는 패키지를 만들려고합니다. 내 package.json은 다음과 같습니다 :npm 전역 설치 노드를 사용하지 않음

{ 
    "name": "my-new-package", 
    "version": "1.0.0", 
    "main": "index.js", 
    "preferGlobal": true, 
    "bin": { 
    "my-new-package": "index.js" 
    } 
} 

"node index.js"로 실행할 수 있으며 npm에 게시 할 수 있습니다.

문제는 내가 전 세계적으로 npm i -g my-new-package를 설치하면 Windows에서 파일 my-new-package.cmd는 점이다

@"%~dp0\node_modules\my-new-package\index.js" %*

내가 my-new-package을 실행할 때 그래서 기본 편집기로 자바 스크립트 파일을 엽니 다.

대조적으로 다른 글로벌 패키지는 node.exe을 사용합니다. 나는 리눅스를 설치할 때

@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, 내가 같은 동작을 얻을 : 여기 cute-files.cmd 예입니다.

그렇다면 패키지가 전 세계적으로 설치되었을 때 노드를 사용하도록 패키지를 구성하려면 어떻게해야합니까?여기

답변

2

먼저 지금 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" 
} 

여기 my-new-package

mkdir bin 
mkdir lib 

이 디렉토리의 자식으로이 두 DIRS을 만들 세계적으로 실행 nodejs 패키지

mkdir my-new-package 
cd my-new-package 

를 만드는 방법입니다 우리가 필요로하는 또 다른 파일 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; 

지금 설치 글로벌 명령을 위의 명령

설치에

cd my-new-package 

npm install -g . 

통지 기간을 디렉토리 my-new-package 경우 파일 package.json의 삶에 들어가 파일 지금 cat bin/test-module.js

#!/usr/bin/env node 

var lib= require('../lib/index.js'); 
var greeting = lib.sayHello('hello everyone'); 

console.log(greeting); 

를 작성하고 발행

의 출력은 지금이

/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 
└── [email protected] 

같은 것입니다 자사의 세계적 당신이 어떤 디렉토리

my-executable-name 

이 동안을 발행 할 수 있습니다 설치는 흥미롭게도 우리의 새로운

Hello, hello everyone 

로 응답합니다 nodejs의 전역 bin 디렉토리에있는 실행 파일의 위치 :

which my-executable-name 

/home/stens/node-v6.5.0/bin/my-executable-name