maven有一个功能我在package.json中错过了很多。在maven .pom文件中,您可以在父项目中定义变量,并在子项目的pom文件中使用它们。
npm有这样的东西吗?我们正在构建模块化项目,我想集中定义依赖版本,并在各自的package.json文件中使用它们。
由于
答案 0 :(得分:26)
可以引用任何键,从$npm_package_
开始,并为您所下的每个级别添加下划线。
示例:
{ "name": "appname", "version": "0.0.1"}
名称可以通过以下方式访问:$npm_package_name
答案 1 :(得分:13)
您可以在package.json
中拥有config对象以下是两个带示例的链接:
http://www.marcusoft.net/2015/08/npm-scripting-configs-and-arguments.html#npm-configuration
答案 2 :(得分:4)
在Windows上,您应该使用%variable_name%
。例如:
{
"name": "app_name",
"version": "1.0.0",
"custom": {
"key": "any value"
}
}
您可以使用%npm_package_name%
,%npm_package_version%
和%npm_package_custom_key%
来获取它。
答案 3 :(得分:1)
我将 package.json 文件作为变量导入到我的节点项目中以获取对象结构,而不是使用 $npm_package_name。这会将其从 JSON 转换为 JS 对象
let package = require("./package.json");
答案 4 :(得分:0)
我花了一段时间才发现我的错误:
我在 powershell 上运行,你必须使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Communities Fighting Covid</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/modern-business.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.2.0/d3.min.js"></script>
</head>
<body>
<script>
var margin = {top: 20, right: 160, bottom: 35, left: 30};
var width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
/* Data in strings like it would be if imported from a csv */
var data = [
{ race: "A", positive: "60", total: "600"},
{ race: "C", positive: "58", total: "500"},
{ race: "D", positive: "85", total: "600"},
{ race: "E", positive: "56", total: "500"},
{ race: "F", positive: "89", total: "700"},
{ race: "G", positive: "62", total: "400"}
];
// Transpose the data into layers
var keys= ["positive", "total"];
var dataset = d3.stack().keys(keys)(data);
// Set x, y and colors
var x = d3.scaleBand()
.domain(data.map(function(d) { return d.race; }))
.range([10, width-10], 0.02);
var y = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) { return d3.max(d, function(d) { return d[0] + d[1]; }); })])
.range([height, 0]);
var colors = ["b33040", "#d25c4d"];
// Define and draw axes
var yAxis = d3.axisLeft()
.scale(y)
.ticks(5)
.tickSize(-width, 0, 0)
.tickFormat( function(d) { return d } );
var xAxis = d3.axisBottom()
.scale(x)
.tickFormat(data.race); // change this to get x values to reflect new
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Create groups for each series, rects for each segment
var groups = svg.selectAll("g.cost")
.data(dataset)
.enter().append("g")
.attr("class", "cost")
.style("fill", function(d, i) { return colors[i]; })
var rect = groups.selectAll("rect")
.data(function(d) { return d; })
.enter()
.append("rect")
.attr("x", function(d) { return x(d.data.race); })
.attr("y", function(d) { return y(d[0] + d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[0] + d[1]); })
.attr("width", x.bandwidth())
.on("mouseover", function() { tooltip.style("display", null); })
.on("mouseout", function() { tooltip.style("display", "none"); })
.on("mousemove", function(d) {
var xPosition = d3.pointer(this)[0] - 15;
var yPosition = d3.pointer(this)[1] - 25;
tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
tooltip.select("text").text(d.y);
});
// Draw legend
var legend = svg.selectAll(".legend")
.data(colors)
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", function(d, i) {return colors.slice().reverse()[i];});
legend.append("text")
.attr("x", width + 5)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d, i) {
switch (i) {
case 0: return "Total Cases";
case 1: return "Positive Cases";
}
});
// Prep the tooltip bits, initial display is hidden
var tooltip = svg.append("g")
.attr("class", "tooltip")
.style("display", "none");
tooltip.append("rect")
.attr("width", 30)
.attr("height", 20)
.attr("fill", "white")
.style("opacity", 0.5);
tooltip.append("text")
.attr("x", 15)
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.attr("font-size", "12px")
.attr("font-weight", "bold");
</script>
</body>
或 $env:npm_package_name
在那里访问变量。