2019-05-06 07:31:26 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7fb4367417b2b2512c00
|
2021-02-16 13:01:20 -08:00
|
|
|
|
title: 使用 npm 的外部包扩展项目
|
2019-05-06 07:31:26 -04:00
|
|
|
|
challengeType: 2
|
2020-09-18 00:12:32 +08:00
|
|
|
|
forumTopicId: 301527
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: expand-your-project-with-external-packages-from-npm
|
2019-05-06 07:31:26 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
强大的依赖管理特性是使用包管理器的最大原因之一。 每当在新的计算机上开始一个项目时,无需手动,npm 会自动安装所有的依赖项。 但是 npm 如何准确地知道项目需要哪些依赖呢? 来看看 package.json 文件中 `dependencies` 这一部分。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
在这部分,你的项目需要按照下面这种格式来存储依赖包:
|
2020-09-18 00:12:32 +08:00
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
"dependencies": {
|
|
|
|
|
"package-name": "version",
|
|
|
|
|
"express": "4.14.0"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
在 package.json 文件的 `dependencies` 字段中添加一个版本号为“2.14.0”的“moment”包。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
**注意:** Moment 是一个非常方便地用来处理时间和日期的库。
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
“dependencies”字段应该包含“moment”
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
(getUserInput) =>
|
|
|
|
|
$.get(getUserInput('url') + '/_api/package.json').then(
|
|
|
|
|
(data) => {
|
|
|
|
|
var packJson = JSON.parse(data);
|
|
|
|
|
assert.property(
|
|
|
|
|
packJson.dependencies,
|
|
|
|
|
'moment',
|
|
|
|
|
'"dependencies" does not include "moment"'
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-05-06 07:31:26 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
“moment”的版本应该是“2.14.0”
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
(getUserInput) =>
|
|
|
|
|
$.get(getUserInput('url') + '/_api/package.json').then(
|
|
|
|
|
(data) => {
|
|
|
|
|
var packJson = JSON.parse(data);
|
|
|
|
|
assert.match(
|
|
|
|
|
packJson.dependencies.moment,
|
|
|
|
|
/^[\^\~]?2\.14\.0/,
|
|
|
|
|
'Wrong version of "moment" installed. It should be 2.14.0'
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-05-06 07:31:26 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
/**
|
|
|
|
|
Backend challenges don't need solutions,
|
|
|
|
|
because they would need to be tested against a full working project.
|
|
|
|
|
Please check our contributing guidelines to learn more.
|
|
|
|
|
*/
|
|
|
|
|
```
|