在 Ubuntu 18.04 上安装 node-sass 失败

Installing node-sass fails on Ubuntu 18.04

我有一个简单的 next.js 项目,是我按照他们的教程创建的。它在我的 Windows 开发机器上构建并 运行 运行良好。

我想在 Ubuntu 18.04.02 LTS 上部署它,但是当我克隆 git 项目和 运行 npm install 时,它无法安装 node-sass 并出现以下错误消息:

> node-sass@4.12.0 install /var/www/example.com/beta/node_modules/node-sass
> node scripts/install.js

internal/modules/cjs/loader.js:584
    throw err;
    ^

Error: Cannot find module '/var/www/example.com/beta/node_modules/node-sass/scripts/install.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.12.0 install: `node scripts/install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the node-sass@4.12.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: ...

这里是 log file 的要点。查看错误的最后几行(我已经包括了整个文件以防有其他有用的信息)。

我的NodeJS版本是10.15.3,NPM版本是6.4.1。我不确定如何 运行 next.js 在 Ubuntu.

上投影

尝试手动安装它,然后再安装所有其他包 - 可能存在一些包依赖性问题。

npm install node-sass@4.12.0 && npm install

显然,我需要使用 --unsafe-perms 标记来安装它:

npm install --unsafe-perms

没有解释为什么需要这个,unsafe 这个词不被很多人推荐。

此外,文件夹权限需要使脚本可以创建文件夹。我必须创建一个文件夹 me,在那里克隆 repo,运行 npm install --unsafe-perms,然后设置生产 Web 服务器的权限。

参考 here

本模块对node版本有限制,如下:

解决方法是:安装时指定模块版本。

P.S。其他方法:

如果您只想使用 scss/sass,请使用此模块:

 npm install sass sass-loader

这不适用于:节点 16,ubuntu18

npm install --unsafe-perms

for node 14, ubuntu 18 , 我可以安装它:

npm install node-sass