1. 程式人生 > >"it does not contain a package.json file." npm install問題解決

"it does not contain a package.json file." npm install問題解決

困擾了2個小時終於搞定。

目的是要安裝bignumber.js包

第一次出現問題:

npm WARN [email protected] No repository field.

+ [email protected]
added 1 package from 1 contributor, updated 1 package and audited 7 packages in 10.393s
found 0 vulnerabilities

說明問題主要是使用npm執行安裝時,呼叫repository(庫)失敗。

也就是說repository裡邊可能沒有bignumber的檔案。

再次執行:

npm ERR! code ENOLOCAL
npm ERR! Could not install from "node_modules/bignumber.js" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neo/.npm/_logs/2018-10-05T09_45_26_493Z-debug.log

這次報的錯其實就已經很明顯了。在package.json指定的庫中沒有包含這個檔案。

於是,現在的任務主要就是package.json中新增包含這個檔案的庫。

直接輸入npm命令,會顯示npm的簡介資訊,裡面包含npm安裝的路徑

[email protected]:~/node_modules/neo$ npm

Usage: npm <command>

where <command> is one of:
    access, adduser, audit, bin, bugs, c, cache, ci, cit,
    completion, config, create, ddp, dedupe, deprecate,
    dist-tag, docs, doctor, edit, explore, get, help,
    help-search, hook, i, init, install, install-test, it, link,
    list, ln, login, logout, ls, outdated, owner, pack, ping,
    prefix, profile, prune, publish, rb, rebuild, repo, restart,
    root, run, run-script, s, se, search, set, shrinkwrap, star,
    stars, start, stop, t, team, test, token, tst, un,
    uninstall, unpublish, unstar, up, update, v, version, view,
    whoami
npm <command> -h quick help on <command> npm -l display full usage info npm help <term> search for help on <term> npm help npm involved overview Specify configs in the ini-formatted file: /Users/neo/.npmrc or on the command line via: npm <command> --key value Config info can be viewed via: npm help config [email protected] /usr/local/lib/node_modules/npm

最後一行就是路徑:cd到那個路徑下,再cd到npm

$ cd /usr/local/lib/node_modules
$ cd npm/

裡面包含一個package.json,這個檔案就是我們要修改的檔案。

[email protected]:/usr/local/lib/node_modules/npm$ ls
AUTHORS         LICENSE         appveyor.yml    configure       lib             node_modules    scripts
CHANGELOG.md    Makefile        bin             doc             make.bat        npmrc
CONTRIBUTING.md README.md       changelogs      html            man             package.json

然後我們直接啟動vi

$ vi package.json

在vi下,輸入’/'然後再輸入repository,再敲回車,查詢到repository的位置

2805   "repository": {                                                                                                                    
2806     "type": "git",
2807     "url": "git+https://github.com/npm/cli.git"
2808   },

我的是2805行…前面是行號,可以忽略。

儲存退出,然後再執行

$ npm install --save bignumber.js
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/npm/node_modules
npm ERR! path /usr/local/lib/node_modules/npm/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules/npm/node_modules'
npm ERR!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules/npm/node_modules'
npm ERR!   stack: 'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules/npm/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules/npm/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 permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neo/.npm/_logs/2018-10-05T10_51_39_317Z-debug.log

這個是許可權拒絕。所以我們重新執行一下,請求一下許可權sudo

$ sudo npm install --save bignumber.js
Password:
npm notice created a lockfile as package-lock.json. You should commit this file.
+ [email protected]
added 1 package from 1 contributor and audited 5019 packages in 5.752s
found 0 vulnerabilities

回車,輸入密碼,搞定。

我們啟動一下node,試試。

$ node
> require('bignumber.js')
{ [Function: BigNumber]
  clone: [Function: clone],
  ROUND_UP: 0,
  ROUND_DOWN: 1,
  ROUND_CEIL: 2,
  ROUND_FLOOR: 3,
  ROUND_HALF_UP: 4,
  ROUND_HALF_DOWN: 5,
  ROUND_HALF_EVEN: 6,
  ROUND_HALF_CEIL: 7,
  ROUND_HALF_FLOOR: 8,
  EUCLID: 9,
  set: [Function],
  config: [Function],
  isBigNumber: [Function],
  max: [Function],
  maximum: [Function],
  min: [Function],
  minimum: [Function],
  random: [Function],
  BigNumber: [Circular],
  default: [Circular] }

Have done~