如何解决Semantic UI本地安装报错及primordials未定义问题
安装Semantic-UI与Mongoose时的错误排查与解决
Hey,我来帮你搞定这两个安装问题,先拆解下错误根源和对应的解决办法:
一、问题重现
你在本地安装Semantic-UI时碰到一堆弃用警告,最终因为ReferenceError: primordials is not defined直接终止安装;之后装Mongoose也出现报错。具体错误日志如下:
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 resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated 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 deprecated natives@1.1.6: This module relies on Node.js's internals and will break at some point. Do not use it, and update to graceful-fs@4.x. npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies. npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2. > semantic-ui@2.4.2 install C:\Users\USER\Desktop\Education\uni\questions\node_modules\semantic-ui > gulp install fs.js:45 } = primordials; ^ ReferenceError: primordials is not defined at fs.js:45:5 at req_ (C:\Users\USER\Desktop\Education\uni\questions\node_modules\natives\index.js:143:24) at Object.req [as require] (C:\Users\USER\Desktop\Education\uni\questions\node_modules\natives\index.js:55:10) at Object.<anonymous> (C:\Users\USER\Desktop\Education\uni\questions\node_modules\vinyl-fs\node_modules\graceful-fs\fs.js:1:37) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) npm WARN rollback Rolling back source-map@0.6.1 failed (this is probably harmless): EPERM: operation not permitted, lstat 'C:\Users\USER\Desktop\Education\uni\questions\node_modules\concat-with-sourcemaps\node_modules' npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules\gulp-copy\node_modules\chokidar\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules\gulp-watch\node_modules\chokidar\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm WARN questions@1.0.0 No repository field. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.3 (node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! semantic-ui@2.4.2 install: `gulp install` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the semantic-ui@2.4.2 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: npm ERR! C:\Users\USER\AppData\Roaming\npm-cache\_logs\2020-11-29T01_27_16_929Z-debug.log
安装Mongoose时的错误提示:
npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\USER\AppData\Roaming\npm-cache\_logs\2020-11-29T01_27_16_929Z-debug.log
二、错误原因分析
- Semantic-UI安装失败核心原因:你当前用的Node.js版本(大概率是12.x及以上)和Semantic-UI 2.4.2依赖的Gulp 3.x不兼容。Gulp 3依赖的
graceful-fs版本过低,而Node.js 12+移除了内部APIprimordials,直接触发报错。 - Mongoose安装错误:大概率是Semantic-UI安装失败残留的缓存问题,或者Node.js版本与Mongoose版本不匹配引发的连锁反应。
三、解决方案
针对Semantic-UI的问题,三种可行方案任你选:
方案1:降级Node.js版本到兼容Gulp 3的版本
- 安装Node版本管理器(Windows用nvm-windows,Mac/Linux用nvm),轻松切换不同Node版本。
- 安装并切换到Node.js 11.x版本(比如11.15.0),这是Gulp 3支持的最高Node版本。
- 切换版本后清除npm缓存:
npm cache clean --force - 重新安装Semantic-UI:
npm install semantic-ui --save
方案2:用npm依赖覆盖功能强制升级兼容包(适合npm 8.x及以上)
- 在项目根目录的
package.json中添加overrides字段,强制升级Semantic-UI依赖的兼容包:"overrides": { "semantic-ui": { "gulp": "^4.0.2", "graceful-fs": "^4.2.11", "natives": "^1.1.6" } } - 删除
node_modules文件夹和package-lock.json文件,重新执行安装:rm -rf node_modules package-lock.json npm install
方案3:改用Semantic-UI CDN版本(最简便,无需本地构建)
如果不需要自定义Semantic-UI样式,直接在HTML中引入CDN资源即可,完全避开本地安装问题:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css"> <script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
针对Mongoose的问题:
- 先解决Semantic-UI的安装问题,确保Node.js版本和依赖环境正常。
- 清除npm缓存:
npm cache clean --force - 匹配Node.js版本安装对应Mongoose:
- Node.js 12+ → 安装Mongoose 6.x及以上版本
- Node.js 8-11 → 安装Mongoose 5.x版本
- 执行安装命令:
npm install mongoose --save
内容的提问来源于stack exchange,提问作者Chukwuyem Obiazi




