使用npm我们可以使用-g
option 全局安装模块.我们如何在package.json文件中执行此操作?
假设,这些是我在package.json文件中的依赖项
"dependencies": {
"mongoose": "1.4.0",
"node.io" : "0.3.3",
"jquery" : "1.5.1",
"jsdom" : "0.2.0",
"cron" : "0.1.2"
}
Run Code Online (Sandbox Code Playgroud)
当我运行时npm install
,我只希望node.io
全局安装,其他的应该在本地安装.这有选择吗?
Jas*_*say 211
新注意:您可能不希望或不需要这样做.您可能想要做的只是将这些类型的命令依赖devDependencies
项放在build.json 的部分中进行构建/测试等. 无论何时使用scripts
package.json中的内容,devDependencies命令(在node_modules/.bin中)就像它们在您的路径中一样.
例如:
npm i --save-dev mocha # Install test runner locally
npm i --save-dev babel # Install current babel locally
Run Code Online (Sandbox Code Playgroud)
然后在package.json中:
// devDependencies has mocha and babel now
"scripts": {
"test": "mocha",
"build": "babel -d lib src",
"prepublish": "babel -d lib src"
}
Run Code Online (Sandbox Code Playgroud)
然后在命令提示符下运行:
npm run build # finds babel
npm test # finds mocha
npm publish # will run babel first
Run Code Online (Sandbox Code Playgroud)
但是如果你真的想要全局安装,你可以在package.json的脚本部分添加一个预安装:
"scripts": {
"preinstall": "npm i -g themodule"
}
Run Code Online (Sandbox Code Playgroud)
所以实际上我的npm安装再次执行npm install ..这很奇怪,但似乎工作.
注意:如果您使用最常见的设置来npm
安装全局Node包,则可能会遇到问题sudo
.一种选择是更改npm
配置,因此不需要:
npm config set prefix ~/npm
,将$ HOME/npm/bin添加到$ PATH,方法是附加export PATH=$HOME/npm/bin:$PATH
到您的~/.bashrc
.
joe*_*dle 12
由于下面描述的缺点,我建议遵循接受的答案:
npm install --save-dev [package_name]
然后使用执行脚本:Run Code Online (Sandbox Code Playgroud)$ npm run lint $ npm run build $ npm test
我的原始但不推荐的答案如下.
您可以将包添加到devDependencies
(--save-dev
),然后从项目内的任何位置运行二进制文件,而不是使用全局安装:
"$(npm bin)/<executable_name>" <arguments>...
Run Code Online (Sandbox Code Playgroud)
在你的情况下:
"$(npm bin)"/node.io --help
Run Code Online (Sandbox Code Playgroud)
该工程师提供了npm-exec
别名作为快捷方式. 这位工程师使用一个名为的shellcript env.sh
.但我更喜欢$(npm bin)
直接使用,以避免任何额外的文件或设置.
虽然它会使每次调用变得更大,但它应该可以工作,防止:
sudo
缺点:
似乎更好的解决方案是将常见任务(例如构建和缩小)放在您的"脚本"部分中package.json
,正如Jason在上面演示的那样.
这有点旧,但我遇到了要求,所以这里是我提出的解决方案.
问题:
我们的开发团队维护着许多我们正在迁移到AngularJS/Bootstrap的.NET Web应用程序产品.VS2010不易于自定义构建过程,我的开发人员经常处理我们产品的多个版本.我们的VCS是颠覆(我知道,我知道.我想移动到Git的,但我讨厌的营销人员是如此苛刻的)和一个与解决方案将包括几个单独的项目.我需要我的员工有,而不必在同一台机器上安装相同的节点程序包(一饮而尽,凉亭等)多次初始化其开发环境的常用方法.
TL; DR:
需要"npm install"来安装全局Node/Bower开发环境以及.NET产品的所有本地需要的软件包.
只有在尚未安装的情况下才应安装全局软件包.
必须自动创建指向全局包的本地链接.
解决方案:
我们已经拥有了所有开发人员和所有产品共享的通用开发框架,因此我创建了一个NodeJS脚本,以便在需要时安装全局包并创建本地链接.该脚本位于相对于产品基本文件夹的"....\SharedFiles"中:
/*******************************************************************************
* $Id: npm-setup.js 12785 2016-01-29 16:34:49Z sthames $
* ==============================================================================
* Parameters: 'links' - Create links in local environment, optional.
*
* <p>NodeJS script to install common development environment packages in global
* environment. <c>packages</c> object contains list of packages to install.</p>
*
* <p>Including 'links' creates links in local environment to global packages.</p>
*
* <p><b>npm ls -g --json</b> command is run to provide the current list of
* global packages for comparison to required packages. Packages are installed
* only if not installed. If the package is installed but is not the required
* package version, the existing package is removed and the required package is
* installed.</p>.
*
* <p>When provided as a "preinstall" script in a "package.json" file, the "npm
* install" command calls this to verify global dependencies are installed.</p>
*******************************************************************************/
var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');
/*---------------------------------------------------------------*/
/* List of packages to install and 'from' value to pass to 'npm */
/* install'. Value must match the 'from' field in 'npm ls -json' */
/* so this script will recognize a package is already installed. */
/*---------------------------------------------------------------*/
var packages =
{
"bower" : "bower@1.7.2",
"event-stream" : "event-stream@3.3.2",
"gulp" : "gulp@3.9.0",
"gulp-angular-templatecache" : "gulp-angular-templatecache@1.8.0",
"gulp-clean" : "gulp-clean@0.3.1",
"gulp-concat" : "gulp-concat@2.6.0",
"gulp-debug" : "gulp-debug@2.1.2",
"gulp-filter" : "gulp-filter@3.0.1",
"gulp-grep-contents" : "gulp-grep-contents@0.0.1",
"gulp-if" : "gulp-if@2.0.0",
"gulp-inject" : "gulp-inject@3.0.0",
"gulp-minify-css" : "gulp-minify-css@1.2.3",
"gulp-minify-html" : "gulp-minify-html@1.0.5",
"gulp-minify-inline" : "gulp-minify-inline@0.1.1",
"gulp-ng-annotate" : "gulp-ng-annotate@1.1.0",
"gulp-processhtml" : "gulp-processhtml@1.1.0",
"gulp-rev" : "gulp-rev@6.0.1",
"gulp-rev-replace" : "gulp-rev-replace@0.4.3",
"gulp-uglify" : "gulp-uglify@1.5.1",
"gulp-useref" : "gulp-useref@3.0.4",
"gulp-util" : "gulp-util@3.0.7",
"lazypipe" : "lazypipe@1.0.1",
"q" : "q@1.4.1",
"through2" : "through2@2.0.0",
/*---------------------------------------------------------------*/
/* fork of 0.2.14 allows passing parameters to main-bower-files. */
/*---------------------------------------------------------------*/
"bower-main" : "git+https://github.com/Pyo25/bower-main.git"
}
/*******************************************************************************
* run */
/**
* Executes <c>cmd</c> in the shell and calls <c>cb</c> on success. Error aborts.
*
* Note: Error code -4082 is EBUSY error which is sometimes thrown by npm for
* reasons unknown. Possibly this is due to antivirus program scanning the file
* but it sometimes happens in cases where an antivirus program does not explain
* it. The error generally will not happen a second time so this method will call
* itself to try the command again if the EBUSY error occurs.
*
* @param cmd Command to execute.
* @param cb Method to call on success. Text returned from stdout is input.
*******************************************************************************/
var run = function(cmd, cb)
{
/*---------------------------------------------*/
/* Increase the maxBuffer to 10MB for commands */
/* with a lot of output. This is not necessary */
/* with spawn but it has other issues. */
/*---------------------------------------------*/
exec(cmd, { maxBuffer: 1000*1024 }, function(err, stdout)
{
if (!err) cb(stdout);
else if (err.code | 0 == -4082) run(cmd, cb);
else throw err;
});
};
/*******************************************************************************
* runCommand */
/**
* Logs the command and calls <c>run</c>.
*******************************************************************************/
var runCommand = function(cmd, cb)
{
console.log(cmd);
run(cmd, cb);
}
/*******************************************************************************
* Main line
*******************************************************************************/
var doLinks = (process.argv[2] || "").toLowerCase() == 'links';
var names = Object.keys(packages);
var name;
var installed;
var links;
/*------------------------------------------*/
/* Get the list of installed packages for */
/* version comparison and install packages. */
/*------------------------------------------*/
console.log('Configuring global Node environment...')
run('npm ls -g --json', function(stdout)
{
installed = JSON.parse(stdout).dependencies || {};
doWhile();
});
/*--------------------------------------------*/
/* Start of asynchronous package installation */
/* loop. Do until all packages installed. */
/*--------------------------------------------*/
var doWhile = function()
{
if (name = names.shift())
doWhile0();
}
var doWhile0 = function()
{
/*----------------------------------------------*/
/* Installed package specification comes from */
/* 'from' field of installed packages. Required */
/* specification comes from the packages list. */
/*----------------------------------------------*/
var current = (installed[name] || {}).from;
var required = packages[name];
/*---------------------------------------*/
/* Install the package if not installed. */
/*---------------------------------------*/
if (!current)
runCommand('npm install -g '+required, doWhile1);
/*------------------------------------*/
/* If the installed version does not */
/* match, uninstall and then install. */
/*------------------------------------*/
else if (current != required)
{
delete installed[name];
runCommand('npm remove -g '+name, function()
{
runCommand('npm remove '+name, doWhile0);
});
}
/*------------------------------------*/
/* Skip package if already installed. */
/*------------------------------------*/
else
doWhile1();
};
var doWhile1 = function()
{
/*-------------------------------------------------------*/
/* Create link to global package from local environment. */
/*-------------------------------------------------------*/
if (doLinks && !fs.existsSync(path.join('node_modules', name)))
runCommand('npm link '+name, doWhile);
else
doWhile();
};
Run Code Online (Sandbox Code Playgroud)
现在,如果我想为开发人员更新全局工具,我会更新"packages"对象并检入新脚本.我的开发人员检查它并使用"node npm-setup.js"或"npm install"从正在开发的任何产品中运行它来更新全局环境.整件事需要5分钟.
此外,要为新开发人员配置环境,他们必须首先只安装NodeJS和GIT for Windows,重新启动计算机,查看"共享文件"文件夹和正在开发的任何产品,然后开始工作.
.NET产品的"package.json"在安装之前调用此脚本:
{
"name" : "Books",
"description" : "Node (npm) configuration for Books Database Web Application Tools",
"version" : "2.1.1",
"private" : true,
"scripts":
{
"preinstall" : "node ../../SharedFiles/npm-setup.js links",
"postinstall" : "bower install"
},
"dependencies": {}
}
Run Code Online (Sandbox Code Playgroud)
笔记
请注意,即使在Windows环境中,脚本引用也需要正斜杠.
"故宫ls",将给予"NPM ERR无关!"消息在当地联系的,因为他们不是在"的package.json""依赖性"中列出的所有软件包.
编辑1/29/16
npm-setup.js
上面更新的脚本已修改如下:
包"version" var packages
现在是npm install
在命令行上传递给的"包"值.这已更改为允许从注册存储库以外的其他位置安装包.
如果软件包已安装但不是所请求的软件包,则会删除现有软件包并安装正确的软件包.
由于未知原因,npm会在执行安装或链接时定期抛出EBUSY错误(-4082).捕获此错误并重新执行命令.错误很少发生第二次,似乎总是清理.
构建您自己的脚本来安装全局依赖项。这并不需要太多。package.json 具有很强的可扩展性。
const { execSync } = require('child_process');
const fs = require('fs');
const package = JSON.parse(fs.readFileSync('package.json'));
let keys = Object.keys(package.dependencies);
let values = Object.values(package.dependencies);
for (let index = 0; index < keys.length; index++) {
const key = keys[index];
let value = values[index].replace("~", "").replace("^", "");
console.log(`Installing: ${key}@${value} globally`,);
execSync('npm i -g ' + `${key}@${value}`);
}
Run Code Online (Sandbox Code Playgroud)
使用上面的内容,您甚至可以将其内联,如下!
看下面的预安装:
{
"name": "Project Name",
"version": "0.1.0",
"description": "Project Description",
"main": "app.js",
"scripts": {
"preinstall": "node -e \"const {execSync} = require('child_process'); JSON.parse(fs.readFileSync('package.json')).globalDependencies.forEach(globaldep => execSync('npm i -g ' + globaldep));\"",
"build": "your transpile/compile script",
"start": "node app.js",
"test": "./node_modules/.bin/mocha --reporter spec",
"patch-release": "npm version patch && npm publish && git add . && git commit -m \"auto-commit\" && git push --follow-tags"
},
"dependencies": [
},
"globalDependencies": [
"cordova@8.1.2",
"ionic",
"potato"
],
"author": "author",
"license": "MIT",
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^5.2.0"
},
"bin": {
"app": "app.js"
}
}
Run Code Online (Sandbox Code Playgroud)
Node的作者可能不承认package.json是一个项目文件。但它是。
您可以使用单独的文件,例如npm_globals.txt
,而不是package.json
。该文件将在每个新行中包含每个模块,如下所示:
mongoose@1.4.0
node.io@0.3.3
jquery@1.5.1
jsdom@0.2.0
cron@0.1.2
Run Code Online (Sandbox Code Playgroud)
然后在命令行中运行
< npm_globals.txt xargs npm install -g
Run Code Online (Sandbox Code Playgroud)
检查它们是否正确安装,
npm list -g --depth=0
Run Code Online (Sandbox Code Playgroud)
至于是否应该执行此操作,我认为这完全取决于用例。对于大多数项目,这不是必需的。并且最好将您的项目package.json
将这些工具和依赖项封装在一起。
但是如今,我发现,create-react-app
当我跳上一台新计算机时,我总是在全球范围内安装CLI和其他CLI。当版本控制无关紧要时,有一种简单的方法可以很好地安装全局工具及其依赖项。
而如今,我使用npx
,一包NPM亚军,而不是全球安装软件包。
归档时间: |
|
查看次数: |
104485 次 |
最近记录: |