nodejs12版本安装node-sass失败各种解决方案(全)

nodejs12版本安装node-sass失败各种解决方案(全)
今天把node 版本升级到12.10,想体验下新特性,但是没想到 在安装node-sass时出现了意外,
就个人经验来说,本人启动的 npm i 命令次数也不少了,node-sass安装失败的原因,总结下来也就几种
最常见的报错
1.没有权限: 解决方法
第一种: npm install --unsafe-perm node-sass
--unsafe-perm: 在root下默认值为false,其他的默认值为true
设置为true,在run package script时会切换UID/GID 明确设置为false,在非root用户下安装会失败
第二种 npm install --user=root node-sass
--user=root:指定了在执行脚本时使用的账号(UID),默认是nobody
应该还有其他解决方案,但是一般这两种基本能解决问题

2.网络原因, 安装 node-sass 时在 node scripts/install 阶段会从 github.com 上下载一个 .node文件,
大部分安装不成功的原因都源自这里,
因为 GitHub Releases 里的文件都托管在 s3.amazonaws.com
上面,而这个网址在国内总是网络不稳定,
所以我们需要通过第三方服务器下载这个文件。:
解决方法:

第一种  设置淘宝源 npm config set registry https://registry.npm.taobao.org;
第二种  用cnpm 
        先全局安装 cnpm  npm i cnpm -g
        cnpm i node-sass -D
第三种 在项目中添加 .npmrc文件
        sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
        registry=https://registry.npm.taobao.org
        然后直接 npm install即可 注意安装 node-sass就会自动从淘宝源上下载

复制代码
但是今天拉项目的时候又发现了一种:node版本与fsevents版本不兼容。问题复现
node版本12.10.1 fsevents版本1.2.1,如果node版本在12以下的应该没有影响,但是,如果node版本是12的话 运行npm i 运行日志会报错:
../../../../nan/nan_converters_43_inl.h:22:1: warning: 'ToBoolean' is deprecated: ToBoolean can never throw. Use Local version. [-Wdeprecated-declarations]
X(Boolean)
^
../../../../nan/nan_converters_43_inl.h:18:12: note: expanded from macro 'X'
val->To ## TYPE(isolate->GetCurrentContext()) \\n ^
:201:1: note: expanded from here
ToBoolean
^
/Users/rene/.node-gyp/12.1.0/include/node/v8.h:2523:3: note: 'ToBoolean' has been explicitly marked deprecated here
V8_DEPRECATE_SOON("ToBoolean can never throw. Use Local version.",
^
/Users/rene/.node-gyp/12.1.0/include/node/v8config.h:322:29: note: expanded from macro 'V8_DEPRECATE_SOON'
declarator attribute((deprecated(message)))
^
In file included from ../fsevents.cc:6:
In file included from ../../../../nan/nan.h:221:
In file included from ../../../../nan/nan_converters.h:67:
../../../../nan/nan_converters_43_inl.h:40:1: warning: 'BooleanValue' is deprecated: BooleanValue can never throw. Use Isolate version. [-Wdeprecated-declarations]
X(bool, Boolean)
^
../../../../nan/nan_converters_43_inl.h:37:15: note: expanded from macro 'X'
return val->NAME ## Value(isolate->GetCurrentContext()); \\n ^
:208:1: note: expanded from here
BooleanValue
^
/Users/rene/.node-gyp/12.1.0/include/node/v8.h:2561:3: note: 'BooleanValue' has been explicitly marked deprecated here
V8_DEPRECATED("BooleanValue can never throw. Use Isolate version.",
^
/Users/rene/.node-gyp/12.1.0/include/node/v8config.h:307:29: note: expanded from macro 'V8_DEPRECATED'
declarator attribute((deprecated(message)))
^
In file included from ../fsevents.cc:6:
In file included from ../../../../nan/nan.h:222:
In file included from ../../../../nan/nan_new.h:189:
../../../../nan/nan_implementation_12_inl.h:103:42: error: no viable conversion from 'v8::Isolate *' to 'Local'
return scope.Escape(v8::Function::New( isolate
^~~~~~~
/Users/rene/.node-gyp/12.1.0/include/node/v8.h:183:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'v8::Isolate *' to 'const v8::Local &' for 1st argument
class Local {
^
/Users/rene/.node-gyp/12.1.0/include/node/v8.h:183:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'v8::Isolate ' to 'v8::Local &&' for 1st argument
/Users/rene/.node-gyp/12.1.0/include/node/v8.h:187:13: note: candidate template ignored: could not match 'Local' against 'v8::Isolate '
V8_INLINE Local(Local that)
^
/Users/rene/.node-gyp/12.1.0/include/node/v8.h:4126:22: note: passing argument to parameter 'context' here
Local context, FunctionCallback callback,
^
In file included from ../fsevents.cc:6:
In file included from ../../../../nan/nan.h:222:
In file included from ../../../../nan/nan_new.h:189:
../../../../nan/nan_implementation_12_inl.h:337:37: error: too few arguments to function call, expected 2, have 1
return v8::StringObject::New(value).As();
~~~~~~~~~~~~~~~~~~~~~ ^
/Users/rene/.node-gyp/12.1.0/include/node/v8.h:5380:3: note: 'New' declared here
static Local New(Isolate
isolate, Local value);
^
In file included from ../fsevents.cc:6:
In file included from ../../../../nan/nan.h:222:
In file included from ../../../../nan/nan_new.h:189:
../../../../nan/nan_implementation_12_inl.h:337:58: error: expected '(' for function-style cast or type construction
return v8::StringObject::New(value).As();
~~~~~~~~~~~~~~~~^
../../../../nan/nan_implementation_12_inl.h:337:60: error: expected expression
return v8::StringObject::New(value).As();
^
In file included from ../fsevents.cc:6:
../../../../nan/nan.h:1063:44: error: no matching member function for call to 'ToString'
v8::Local string = from->ToString();
~~~~~^~~~~~~
/Users/rene/.node-gyp/12.1.0/include/node/v8.h:2528:44: note: candidate function not viable: requires single argument 'context', but no arguments were provided
V8_WARN_UNUSED_RESULT MaybeLocal ToString(
^
/Users/rene/.node-gyp/12.1.0/include/node/v8.h:2544:35: note: candidate function not viable: requires single argument 'isolate', but no arguments were provided
Local ToString(Isolate
isolate) const);
^
In file included from ../fsevents.cc:6:
...
复制代码
这是因为 项目中的 fsevents版本比较低,只有1.2.9以上的才支持node12版本,所以导致编译不通过.
解决方法如下:
删除node_moules文件夹
删除package-lock.json文件
安装最新版本的 fsevents: npm i fsevents -D
然后重新安装依赖 npm i
解决
复制代码
这种情况一般出现在 本地node版本较高,但是项目中的fsevents版本比较低的时候
注意!
现在可以用 dart-sass 无缝替换 node-sass 了!dart-sass 兼容 node-sass 的 API,而且安装过程无需下载二进制文件,这样大家就不需要用本篇文章的方式安装 node-sass 了。

你可能感兴趣的:(nodejs12版本安装node-sass失败各种解决方案(全))