IBa*_*Bam 203
我想你可以使用"引擎"字段:
{ "engines" : { "node" : ">=0.12" } }
Run Code Online (Sandbox Code Playgroud)
正如您所说,您的代码肯定不适用于任何较低版本,您可能也需要"engineStrict"标志:
{ "engineStrict" : true }
Run Code Online (Sandbox Code Playgroud)
可以在npmjs站点上找到package.json文件的文档
更新
engineStrict现已弃用,因此这只会发出警告.npm config set engine-strict true如果他们想要这个,现在由用户运行.
Mik*_*kel 78
加
至 package.json
"engines": {
"node": ">=10.0.0",
"npm": ">=6.0.0"
},
Run Code Online (Sandbox Code Playgroud)
到文件.npmrc(靠近package.json,相同的目录)
engine-strict=true
Run Code Online (Sandbox Code Playgroud)
Ada*_*dam 45
就像Ibam说的那样,engineStrict现在已被弃用了.但我找到了这个解决方案:
入住version.js:
import semver from 'semver';
import { engines } from './package';
const version = engines.node;
if (!semver.satisfies(process.version, version)) {
console.log(`Required node version ${version} not satisfied with current version ${process.version}.`);
process.exit(1);
}
Run Code Online (Sandbox Code Playgroud)
的package.json:
{
"name": "my package",
"engines": {
"node": ">=50.9" // intentionally so big version number
},
"scripts": {
"requirements-check": "babel-node check-version.js",
"postinstall": "npm run requirements-check"
}
}
Run Code Online (Sandbox Code Playgroud)
点击此处了解更多信息:https: //medium.com/@adambisek/how-to-check-minimum-required-node-js-version-4a78a8855a0f#.3oslqmig4
.nvmrc
还有一件事......一个dotfile'.nvmrc'可用于要求特定的节点版本(但我还没试过) - https://github.com/creationix/nvm#nvmrc
.nvmrc
如果您可能正在使用像这样的NVM,则可以在git跟踪的.nvmrc文件中指示给定项目所需的nodejs版本:
echo v10.15.1 > .nvmrc
Run Code Online (Sandbox Code Playgroud)
这不会自动对生效cd,这是理智的:用户然后必须执行以下操作:
nvm use
Run Code Online (Sandbox Code Playgroud)
现在该节点版本将用于当前shell。
您可以列出拥有的节点的版本:
nvm list
Run Code Online (Sandbox Code Playgroud)
.nvmrc记录在:https : //github.com/creationix/nvm/tree/02997b0753f66c9790c6016ed022ed2072c22603#nvmrc
使用NVM 0.33.11测试。
这是我的完整的现成脚本,基于Adam 的回答。
check-version.js:
/* eslint-disable no-console */
const fs = require('fs');
const semver = require('semver');
const childProcess = require('child_process');
// checks that current node and npm versions satisfies requirements in package.json
// to run manually: node check-version.js [verbose]
const VERBOSE_FORCED = false;
const args = process.argv.slice(2);
const VERBOSE = VERBOSE_FORCED || (args.length > 0 && args[0] === 'verbose');
const printErrAndExit = (x) => {
console.error(x);
console.error('Aborting');
process.exit(1);
};
const checkNpmVersion = (npmVersionRequired) => {
if (!npmVersionRequired) {
console.log('No required npm version specified');
return;
}
const npmVersion = `${childProcess.execSync('npm -v')}`.trim();
if (VERBOSE) console.log(`npm required: '${npmVersionRequired}' - current: '${npmVersion}'`);
if (!semver.satisfies(npmVersion, npmVersionRequired)) {
printErrAndExit(`Required npm version '${npmVersionRequired}' not satisfied. Current: '${npmVersion}'.`);
}
};
const checkNodeVersion = (nodeVersionRequired) => {
if (!nodeVersionRequired) {
console.log('No required node version specified');
return;
}
const nodeVersion = process.version;
if (VERBOSE) console.log(`node required: '${nodeVersionRequired}' - current: '${nodeVersion}'`);
if (!semver.satisfies(nodeVersion, nodeVersionRequired)) {
printErrAndExit(`Required node version '${nodeVersionRequired}' not satisfied. Current: '${nodeVersion}'.`);
}
};
const json = JSON.parse(fs.readFileSync('./package.json'));
if (!json.engines) printErrAndExit('no engines entry in package json?');
checkNodeVersion(json.engines.node);
checkNpmVersion(json.engines.npm);
Run Code Online (Sandbox Code Playgroud)
它应该放在项目根目录中。
它检查节点和/或 npm 版本,如package.json(engines条目)中指定的,例如
"engines": {
"node": ">=16.0.0 <17.0.0",
"npm": ">=8.0.0 <9.0.0"
},
Run Code Online (Sandbox Code Playgroud)
您可以手动调用它
node check-version.js [verbose]
或将其作为脚本包含在内部package json,作为独立脚本或作为其他脚本的先决条件,例如
"scripts" : {
"start": "node check-version.js && vite",
"build": "node check-version.js && vite build",
"lint": "node check-version.js && eslint .",
"check-version": "node check-version.js verbose"
},
Run Code Online (Sandbox Code Playgroud)
还有另一种更简单的方法:
npm install Node@8 (将Node 8保存为package.json中的依赖项)之所以有效,node是因为它只是一个将节点作为其二进制程序包提供的程序包。它仅包含为node_module / .bin,这意味着它仅使节点可用于程序包脚本。不是主壳。
在此处查看Twitter上的讨论:https://twitter.com/housecor/status/962347301456015360
| 归档时间: |
|
| 查看次数: |
92144 次 |
| 最近记录: |