美文网首页
npm版本升级备忘

npm版本升级备忘

作者: 佚可 | 来源:发表于2022-08-13 11:42 被阅读0次

    本次总结 :升级npm版本时,需要将npm的源切换到官方源。
    本次总结 :npm版本升级后,可能需要进行node的升级

    在使用npm install项目时报下方错误

    npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@
    1, but package-lock.json was generated for lockfileVersion@2. I'll try to do my
    best with it!
    npm ERR! Unexpected token < in JSON at position 16 while parsing near '
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR! <!DOCTYPE html>
    npm ERR! <ht...'
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\LJQ\AppData\Roaming\npm-cache\_logs\2022-08-14T03_27_34_99
    5Z-debug.log
    

    从报错上看,应该是需要对npm的版本进行升级
    在百度找到npm的升级命令是:npm install -g npm ,但是执行时依然报错

    C:\Users\LJQ>npm install -g npm
    npm ERR! Unexpected token < in JSON at position 16 while parsing near '
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR!
    npm ERR! <!DOCTYPE html>
    npm ERR! <ht...'
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\LJQ\AppData\Roaming\npm-cache\_logs\2022-08-14T03_28_53_56
    6Z-debug.log
    

    这下就有点不知所措了,最后多查了下资料,有人发现需要把npm的源切换为npm官方源才能正常升级。

    于是先进行源的设置

    npm config set registry https://registry.npmjs.org/
    

    然后再进行升级,最后升级成功!

    C:\Users\LJQ>npm install -g npm
    C:\Users\LJQ\application\nodejs\node_cache\npm -> C:\Users\LJQ\application\nodej
    s\node_cache\node_modules\npm\bin\npm-cli.js
    C:\Users\LJQ\application\nodejs\node_cache\npx -> C:\Users\LJQ\application\nodej
    s\node_cache\node_modules\npm\bin\npx-cli.js
    + npm@8.17.0
    added 205 packages from 109 contributors in 79.65s
    

    下一步想将npm的源切会国内的源,但不幸的是发生了下方的报错

    C:\Users\LJQ>npm config set registry https://registry.npm.taobao.org
    ERROR: npm is known not to run on Node.js v10.16.1
    You'll need to upgrade to a newer Node.js version in order to use this
    version of npm. You can find the latest version at https://nodejs.org/
    

    看来还需要对nodejs的版本进行升级,不过呢升级过程中也并不顺利,提示nodejs 需要windows8.1以上版本支持,但是我的版本是windows8。这个时候就麻烦了,卡在这一步了,后来鼓捣了一会儿还是不行。换一个思路, 升级,但不升级为最新版本,升级为npm依赖的最低版本进行尝试。

    更换nodejs为稍低的版本后,npm install成功。紧接着进行 npm run dev运行项目。不幸的是,还是发生了问题:

    C:\Users\LJQ\IdeaProjects\fast-admin>npm run dev
    
    > fast-admin@1.0.0 dev C:\Users\LJQ\IdeaProjects\fast-admin
    > vite
    
    C:\Users\LJQ\IdeaProjects\fast-admin\node_modules\vite\bin\vite.js:2
    import { performance } from 'node:perf_hooks'
    ^^^^^^
    
    SyntaxError: Cannot use import statement outside a module
    �[90m    at Module._compile (internal/modules/cjs/loader.js:892:18)�[39m
    �[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10
    )�[39m
    �[90m    at Module.load (internal/modules/cjs/loader.js:812:32)�[39m
    �[90m    at Function.Module._load (internal/modules/cjs/loader.js:724:14)�[39m
    �[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)�[39
    m
    �[90m    at internal/main/run_main_module.js:17:11�[39m
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! fast-admin@1.0.0 dev: `vite`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the fast-admin@1.0.0 dev script.
    npm ERR! This is probably not a problem with npm. There is likely additional log
    ging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\LJQ\AppData\Roaming\npm-cache\_logs\2022-08-14T05_12_18_37
    0Z-debug.log
    

    其中提示的:SyntaxError: Cannot use import statement outside a module是比较关键的错误信息。再继续百度了解到:

    node早先只支持CommonJS的模块化方案,所以ES6的模块化特性用不了。但是在Node V13.2.0之后开始实验性的支持ESM模块化,不过需要创建package.json文件指明type类型为module。

    也还是node版本低,无法支持ES6的模块化特性。因此,还是必须提升nodejs的版本。然后再继续升级nodejs版本,向上推测的方式。最后测试的版本是v13.2.0,还是报错

    C:\Users\LJQ\IdeaProjects\fast-admin>npm --experimental-modules run dev
    
    > fast-admin@1.0.0 dev C:\Users\LJQ\IdeaProjects\fast-admin
    > vite
    
    (node:7380) ExperimentalWarning: The ESM module loader is experimental.
    file:///C:/Users/LJQ/IdeaProjects/fast-admin/node_modules/vite/bin/vite.js:7
        await import('source-map-support').then((r) => r.default.install())
        ^^^^^
    
    SyntaxError: Unexpected reserved word
    �[90m    at Loader.moduleStrategy (internal/modules/esm/translators.js:84:18)�[
    9m
    �[90m    at async link (internal/modules/esm/module_job.js:36:21)�[39m
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! fast-admin@1.0.0 dev: `vite`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the fast-admin@1.0.0 dev script.
    npm ERR! This is probably not a problem with npm. There is likely additional lo
    ging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\LJQ\AppData\Roaming\npm-cache\_logs\2022-08-14T05_35_09_8
    3Z-debug.log
    

    看来要支持比较新的前段项目,只能装最新版本的nodejs 了 。只能暂停了,操作系统升级太麻烦。

    相关文章

      网友评论

          本文标题:npm版本升级备忘

          本文链接:https://www.haomeiwen.com/subject/fezcgrtx.html