【问题标题】:Nodejs on ubuntu 16.04 - install browser-sync gulp properlyubuntu 16.04 上的 Nodejs - 正确安装浏览器同步 gulp
【发布时间】:2018-02-18 16:14:56
【问题描述】:

我在 ubuntu 16.04 上安装了 nodejs,如下所示:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

我在 apache 上运行它。

我的第一个应用程序 hello.js(保存在 /var/www/html/nodejs/ 中)是:

#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function (request, response) {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end('Hello World! Node.js is working correctly!\n');
}).listen(8080);
console.log('Server running at http://localhost:8080/');

当我在浏览器中输入http://localhost:8080/ 我得到:

Hello World! Node.js is working correctly! 

现在我想像这里建议的那样安装浏览器同步:

Using browser-sync with node.js app

但是当我跑步时:

$ sudo npm install browser-sync gulp --save-dev

我得到以下信息:

npm WARN registry Using stale data from https://registry.npmjs.org/ because the host is inaccessible -- are you offline?
npm WARN registry Using stale package data from https://registry.npmjs.org/ due to a request error during revalidation.
npm WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
npm WARN deprecated graceful-fs@3.0.11: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated minimatch@0.2.14: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated graceful-fs@1.2.3: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js

> uws@9.14.0 install /home/sven/node_modules/uws
> node-gyp rebuild > build_log.txt 2>&1 || exit 0

sh: 1: cannot create build_log.txt: Permission denied
npm WARN saveError ENOENT: no such file or directory, open '/home/sven/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/sven/package.json'
npm WARN sven No description
npm WARN sven No repository field.
npm WARN sven No README data
npm WARN sven No license field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"ia32"})

+ browser-sync@2.23.6
+ gulp@3.9.1
added 547 packages in 57.958s

现在是否安装了浏览器同步?为什么我会收到这么多警告?这应该是这样的吗?我是 nodejs 的新手...

【问题讨论】:

  • 是的,它已安装。

标签: node.js apache ubuntu-16.04


【解决方案1】:

不要使用sudo 在本地安装模块。绝不。这是不必要的,它会导致权限错误。相反,您应该使用:

$ npm install browser-sync gulp --save-dev

当您安装节点模块时,警告是非常典型的。提供这些是为了帮助您在出现任何错误时进行调试。

其中一些通知您模块依赖于已弃用的依赖项:

npm WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
npm WARN deprecated graceful-fs@3.0.11: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated minimatch@0.2.14: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated graceful-fs@1.2.3: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js

但这是包开发者的问题。你可以忽略那部分。

其他人会通知您跳过的可选依赖项:

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"ia32"})

这个警告意味着 npm 没有安装 fsevents。但你不应该关心它,因为它是 OS X 的可选依赖项(wanted {"os":"darwin","arch":"any"};darwin 是 OS X 的代号)。

因此您可以忽略大部分警告。

但这里有一个重要的日志部分:

sh: 1: cannot create build_log.txt: Permission denied

您的主目录中可能存在一些权限问题。

npm WARN saveError ENOENT: no such file or directory, open '/home/sven/package.json'

在这里,我可以看到您尝试从主目录安装软件包。在项目目录中安装本地依赖项。在这种情况下,您应该在/var/www/html/nodejs/ 中使用npm install browser-sync gulp --save-dev,而不是在/home/sven/ 中。

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/sven/package.json'
npm WARN sven No description
npm WARN sven No repository field.
npm WARN sven No README data
npm WARN sven No license field.

这些警告意味着在您的主目录文件package.json 中不存在。首先,从项目目录安装本地包。其次,使用$ npm init初始化项目目录中的包是个好主意。

【讨论】:

  • 感谢您的解释。既然我已经用 sudo 安装了它...我应该先将其删除,然后将其安装在项目文件夹 /var/www/html/nodejs/ 中吗?
  • 那么到目前为止安装的其他库呢? nodejs,build-essential,npm2 ...我是否也应该在没有 sudo 的情况下安装它们? ...在项目文件夹中?
  • 是的,您应该使用sudo rm -r ~/node_modules/ 删除主目录中的这些节点模块。我不知道你在哪里安装了这些模块。如果您安装在 `/var/www/html/nodejs/` 中,则不需要删除它们。您只能更改 `/var/www/html/nodejs/` 中所有文件的权限:$ cd /var/www/html/nodejs/ && sudo chown -R seven:sever * 如果您将它们安装在主目录中,则应在 /var/www/html/nodejs/ 中再次安装此模块。一般来说:您应该找到所有项目文件并从/var/www/html/nodejs/ 安装所有模块。
  • 非常感谢。我会去做的。要仔细检查... nodejs(所以 node js 本身),build-essential,npm2 也应该安装在 /var/www/html/nodejs/ 中?
  • 仅节点包 (node_modules) - 使用 $ npm install ... 安装的东西。
【解决方案2】:

回答你的问题:

现在是否安装了浏览器同步?

是的,它已安装,为了确认,您可以运行 browser-sync --version gulp --version。这应该会显示您安装的版本,因为它已经显示在您的日志中

  • 浏览器同步@2.23.6
  • gulp@3.9.1

正如您从日志中看到的那样,它们只是警告,不会阻止您的模块正常运行。

npm WARN 注册表使用来自https://registry.npmjs.org/ 的陈旧数据,因为主机不可访问——您离线了吗? npm WARN 注册表使用来自https://registry.npmjs.org/ 的陈旧包数据,因为在重新验证期间出现请求错误。 npm WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - 按照https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5 的指南替换它 npm WARN deprecated graceful-fs@3.0.11:请升级到 graceful-fs 4 以兼容当前和未来版本的 Node.js npm WARN 已弃用 minimatch@2.0.10:请更新到 minimatch 3.0.2 或更高版本以避免 RegExp DoS 问题 npm WARN deprecated minimatch@0.2.14: 请更新到 minimatch 3.0.2 或更高版本以避免 RegExp DoS 问题 npm WARN deprecated graceful-fs@1.2.3: 请升级到 graceful-fs 4 以兼容当前和未来版本的 Node.js

uws@9.14.0 安装 /home/sven/node_modules/uws 节点gyp重建> build_log.txt 2>&1 || 0号出口

它们基本上是 Browsersync 使用的已弃用模块。

还有:

sh: 1: 无法创建 build_log.txt: 权限被拒绝 npm WARN saveError ENOENT:没有这样的文件或目录,打开'/home/sven/package.json' npm notice 创建了一个锁文件作为 package-lock.json。你应该提交这个文件。 npm WARN enoent ENOENT:没有这样的文件或目录,打开 '/home/sven/package.json' npm WARN sven 无描述 npm WARN sven 没有存储库字段。 npm WARN sven 没有 README 数据 npm WARN sven 没有许可证字段。 npm WARN 可选跳过可选依赖:fsevents@1.1.3 (node_modules/fsevents): npm WARN notsup 跳过可选依赖:fsevents@1.1.3 不受支持的平台:想要 {"os":"darwin","arch":"any"}(当前:{"os":"linux","arch": "ia32"})

是一个许可的东西。可能

  • 目录启用了不可变标志。或者

  • 目录以只读权限挂载。

或其他一些原因。

我希望这会有所帮助。

【讨论】:

  • 感谢您的解释...运行 sven@sven-MS-7664:~$ browser-sync --version .... 我得到 .... browser-sync: 命令没有找到....虽然。为什么会这样?
  • 哇!这有点像安装有问题。也许您可能需要运行 sudo npm install browser-sync gulp --save-dev 并查看我们得到的结果。
  • 我确实做到了。往上看。另一个答案中的 Pawel 说我应该使用 sudo 安装?我很困惑
  • @PhilippM 是的,使用sudo 可能有点冒险,但尽管如此,您仍然可以通过运行sudo chown -R whoami` /usr/local/lib/node_modules` 更改node_modules 的所有权否则在 difualt 上,您必须在任何时候使用 sudo 运行 npm -g
猜你喜欢
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 2015-04-15
  • 2017-10-31
相关资源
最近更新 更多