安装cnpm报错npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules npm ERR! path

在Linux环境中安装cnpm时遇到权限错误npm WARN checkPermissions,原因是缺少最高权限。解决方法是使用sudo并输入root密码进行安装,安装成功后还需创建软链接确保正常使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装cnpm失败报错

报错信息

hua@hua-PC:~/vue$ npm install cnpm -g
npm WARN checkPermissions Missing write access to /usr/software/nodejs/lib/node_modules
npm ERR! path /usr/software/nodejs/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/software/nodejs/lib/node_modules'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/software/nodejs/lib/node_modules']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/software/nodejs/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/software/nodejs/lib/node_modules' }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissio
安装 报错npm WARN deprecated npmlog@4.1.2: This package is no longer supported. npm WARN deprecated are-we-there-yet@1.1.7: This package is no longer supported. npm WARN deprecated gauge@2.7.4: This package is no longer supported. npm ERR! code 1 npm ERR! path /Users/yangjia/workspace/company/dkYun/SDK2.0/node_modules/microtime npm ERR! command failed npm ERR! command sh -c prebuild-install || node-gyp rebuild npm ERR! CXX(target) Release/obj.target/microtime/src/microtime.o npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@8.4.1 npm ERR! gyp info using node@16.14.0 | darwin | arm64 npm ERR! gyp info find Python using Python version 3.12.1 found at "/Library/Frameworks/Python.framework/Versions/3.12/bin/python3" npm ERR! gyp info spawn /Library/Frameworks/Python.framework/Versions/3.12/bin/python3 npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py', npm ERR! gyp info spawn args 'binding.gyp', npm ERR! gyp info spawn args '-f', npm ERR! gyp info spawn args 'make', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/Users/yangjia/workspace/company/dkYun/SDK2.0/node_modules/microtime/build/config.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/common.gypi', npm ERR! gyp info spawn args '-Dlibrary=shared_library', npm ERR! gyp info spawn args '-Dvisibility=default', npm ERR! gyp info spawn args '-Dnode_root_dir=/Users/yangjia/Library/Caches/node-gyp/16.14.0', npm ERR! gyp info spawn args '-Dnode_gyp_dir=/usr/local/lib/node_modules/npm/node_modules/node-gyp', npm ERR! gyp info spawn args '-Dnode_lib_file=/Users/yangjia/Library/Caches/node-gyp/16.14.0/<(target_arch)/node.lib', npm ERR! gyp info spawn args '-Dmodule_root_dir=/Users/yangjia/workspace/company/dkYun/SDK2.0/node_modules/microtime', npm ERR! gyp info spawn args '-Dnode_engine=v8', npm ERR! gyp info spawn args '--depth=.', npm ERR! gyp info spawn args '--no-parallel', npm ERR! gyp info spawn args '--generator-output', npm ERR! gyp info spawn args 'build', npm ERR! gyp info spawn args '-Goutput_dir=.' npm ERR! gyp info spawn args ] npm ERR! gyp info spawn make npm ERR! gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ] npm ERR! In file included from ../src/microtime.cc:9: npm ERR! In file included from ../../nan/nan.h:222: npm ERR! In file included from ../../nan/nan_converters.h:67: npm ERR! ../../nan/nan_converters_43_inl.h:22:1: error: no viable conversion from 'Local<v8::Context>' to 'v8::Isolate *' npm ERR! X(Boolean) npm ERR! ^~~~~~~~~~ npm ERR! ../../nan/nan_converters_43_inl.h:18:23: note: expanded from macro 'X' npm ERR! val->To ## TYPE(isolate->GetCurrentContext()) \ npm ERR! ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:3086:37: note: passing argument to parameter 'isolate' here npm ERR! Local<Boolean> ToBoolean(Isolate* isolate) const; npm ERR! ^ npm ERR! In file included from ../src/microtime.cc:9: npm ERR! In file included from ../../nan/nan.h:222: npm ERR! In file included from ../../nan/nan_converters.h:67: npm ERR! ../../nan/nan_converters_43_inl.h:22:1: error: no member named 'FromMaybe' in 'v8::Local<v8::Boolean>' npm ERR! X(Boolean) npm ERR! ^~~~~~~~~~ npm ERR! ../../nan/nan_converters_43_inl.h:19:12: note: expanded from macro 'X' npm ERR! .FromMaybe(v8::Local<v8::TYPE>())); \ npm ERR! ^ npm ERR! ../../nan/nan_converters_43_inl.h:40:1: error: no viable conversion from 'Local<v8::Context>' to 'v8::Isolate *' npm ERR! X(bool, Boolean) npm ERR! ^~~~~~~~~~~~~~~~ npm ERR! ../../nan/nan_converters_43_inl.h:37:29: note: expanded from macro 'X' npm ERR! return val->NAME ## Value(isolate->GetCurrentContext()); \ npm ERR! ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:3096:30: note: passing argument to parameter 'isolate' here npm ERR! bool BooleanValue(Isolate* isolate) const; npm ERR! ^ npm ERR! In file included from ../src/microtime.cc:9: npm ERR! In file included from ../../nan/nan.h:222: npm ERR! In file included from ../../nan/nan_converters.h:67: npm ERR! ../../nan/nan_converters_43_inl.h:40:1: error: no viable conversion from returned value of type 'bool' to function return type 'imp::ToFactory<bool>::return_t' (aka 'Maybe<bool>') npm ERR! X(bool, Boolean) npm ERR! ^~~~~~~~~~~~~~~~ npm ERR! ../../nan/nan_converters_43_inl.h:37:10: note: expanded from macro 'X' npm ERR! return val->NAME ## Value(isolate->GetCurrentContext()); \ npm ERR! ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:10437:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'bool' to 'const v8::Maybe<bool> &' for 1st argument npm ERR! class Maybe { npm ERR! ^ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:10437:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'bool' to 'v8::Maybe<bool> &&' for 1st argument npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:10492:12: note: explicit constructor is not a candidate npm ERR! explicit Maybe(const T& t) : has_value_(true), value_(t) {} npm ERR! ^ npm ERR! In file included from ../src/microtime.cc:9: npm ERR! In file included from ../../nan/nan.h:223: npm ERR! In file included from ../../nan/nan_new.h:189: npm ERR! ../../nan/nan_implementation_12_inl.h:356:37: error: too few arguments to function call, expected 2, have 1 npm ERR! return v8::StringObject::New(value).As<v8::StringObject>(); npm ERR! ~~~~~~~~~~~~~~~~~~~~~ ^ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:6196:23: note: 'New' declared here npm ERR! static Local<Value> New(Isolate* isolate, Local<String> value); npm ERR! ^ npm ERR! In file included from ../src/microtime.cc:9: npm ERR! In file included from ../../nan/nan.h:2722: npm ERR! ../../nan/nan_object_wrap.h:24:25: error: no member named 'IsNearDeath' in 'Nan::Persistent<v8::Object>' npm ERR! assert(persistent().IsNearDeath()); npm ERR! ~~~~~~~~~~~~ ^ npm ERR! /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert' npm ERR! (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0) npm ERR! ^ npm ERR! In file included from ../src/microtime.cc:9: npm ERR! In file included from ../../nan/nan.h:2722: npm ERR! ../../nan/nan_object_wrap.h:127:26: error: no member named 'IsNearDeath' in 'Nan::Persistent<v8::Object>' npm ERR! assert(wrap->handle_.IsNearDeath()); npm ERR! ~~~~~~~~~~~~~ ^ npm ERR! /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert' npm ERR! (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0) npm ERR! ^ npm ERR! In file included from ../src/microtime.cc:9: npm ERR! In file included from ../../nan/nan.h:2818: npm ERR! ../../nan/nan_typedarray_contents.h:34:43: warning: 'GetContents' is deprecated: Use GetBackingStore. See http://crbug.com/v8/9908. [-Wdeprecated-declarations] npm ERR! data = static_cast<char*>(buffer->GetContents().Data()) + byte_offset; npm ERR! ^ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:5614:3: note: 'GetContents' has been explicitly marked deprecated here npm ERR! V8_DEPRECATED("Use GetBackingStore. See http://crbug.com/v8/9908.") npm ERR! ^ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8config.h:454:35: note: expanded from macro 'V8_DEPRECATED' npm ERR! # define V8_DEPRECATED(message) [[deprecated(message)]] npm ERR! ^ npm ERR! ../src/microtime.cc:75:10: error: no matching member function for call to 'Set' npm ERR! array->Set(Nan::New<v8::Integer>(0), Nan::New<v8::Number>((double)t.tv_sec)); npm ERR! ~~~~~~~^~~ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:3961:37: note: candidate function not viable: requires 3 arguments, but 2 were provided npm ERR! V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, npm ERR! ^ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:3964:37: note: candidate function not viable: requires 3 arguments, but 2 were provided npm ERR! V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index, npm ERR! ^ npm ERR! ../src/microtime.cc:76:10: error: no matching member function for call to 'Set' npm ERR! array->Set(Nan::New<v8::Integer>(1), Nan::New<v8::Number>((double)t.tv_usec)); npm ERR! ~~~~~~~^~~ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:3961:37: note: candidate function not viable: requires 3 arguments, but 2 were provided npm ERR! V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, npm ERR! ^ npm ERR! /Users/yangjia/Library/Caches/node-gyp/16.14.0/include/node/v8.h:3964:37: note: candidate function not viable: requires 3 arguments, but 2 were provided npm ERR! V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index, npm ERR! ^ npm ERR! 1 warning and 9 errors generated. npm ERR! make: *** [Release/obj.target/microtime/src/microtime.o] Error 1 npm ERR! gyp ERR! build error npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2 npm ERR! gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23) npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:520:28) npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12) npm ERR! gyp ERR! System Darwin 22.6.0 npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild" npm ERR! gyp ERR! cwd /Users/yangjia/workspace/company/dkYun/SDK2.0/node_modules/microtime npm ERR! gyp ERR! node -v v16.14.0 npm ERR! gyp ERR! node-gyp -v v8.4.1 npm ERR! gyp ERR! not ok npm ERR! A complete log of this run can be found in: npm ERR! /Users/yangjia/.npm/_logs/2025-06-30T06_40_09_765Z-debug-0.log
07-01
在使用 npm 进行全局安装包时,遇到 `Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules'` 错误,通常是由于当前用户对目标目录没有写权限导致的。以下是几种常见的解决方法: ### 1. 修改 `/usr/local/lib/node_modules` 目录权限 可以通过修改目标目录的权限,将当前用户设置为该目录的所有者,从而避免权限问题。执行以下命令: ```bash sudo chown -R $USER /usr/local/lib/node_modules ``` 该命令会将 `/usr/local/lib/node_modules` 目录及其子目录的所有权更改为当前用户,确保 npm 有权限在此目录下创建文件和目录 [^2]。 ### 2. 使用 `sudo` 执行 npm 安装命令 如果不想修改目录权限,也可以在安装命令前加上 `sudo`,以管理员权限执行安装操作: ```bash sudo npm install -g <package-name> ``` 例如,安装 `cnpm` 时可以使用: ```bash sudo npm install -g cnpm --registry=https://registry.npm.taobao.org ``` 这种方式适用于临时解决权限问题,但每次安装全局包时都需要使用 `sudo`,可能带来一定的安全风险 [^2]。 ### 3. 配置 npm 的全局安装路径 为了避免对系统目录的权限依赖,可以配置 npm 使用自定义的全局安装路径。首先,创建一个全局安装目录,例如: ```bash mkdir ~/.npm-global ``` 然后,配置 npm 使用该目录作为全局安装路径: ```bash npm config set prefix '~/.npm-global' ``` 最后,将该目录添加到系统的 `PATH` 环境变量中,以便可以直接在命令行中使用全局安装的包。可以在 `~/.bashrc` 或 `~/.zshrc` 文件中添加以下内容: ```bash export PATH=~/.npm-global/bin:$PATH ``` 之后,运行以下命令使配置生效: ```bash source ~/.bashrc # 或者 source ~/.zshrc ``` 这种方法避免了权限问题,并且不会影响系统目录,是一种更为安全和持久的解决方案。 ### 4. 使用 Node 版本管理工具(如 `nvm`) 使用 `nvm`(Node Version Manager)可以避免全局安装目录的权限问题,因为 `nvm` 默认会将全局包安装在当前用户的目录下。安装 `nvm` 后,所有全局包都会安装在 `~/.nvm/versions/node/<version>/lib/node_modules` 中,无需额外权限。 安装 `nvm` 的步骤如下: 1. 安装 `nvm` 脚本: ```bash curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash ``` 2. 重新加载 shell 配置: ```bash source ~/.bashrc # 或者 source ~/.zshrc ``` 3. 安装所需的 Node.js 版本: ```bash nvm install node ``` 使用 `nvm` 管理 Node.js 和 npm安装路径,可以有效避免权限问题 [^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值