在npm安装在vagrant框中后,package.json文件无效(损坏)

时间:2016-09-26 19:07:27

标签: node.js laravel vagrant

我一直在尝试为laravel项目修复一个流浪汉设置。

上下文

  • 主持人:Mac
  • 访客:Ubuntu 16.04

版本:

$ npm --version
2.15.9
$ node --version
v4.5.0

问题

当我运行$ npm install某些依赖包(显然是随机的)时,会出现无效字符并且$ gulp失败。

更准确地说,损坏文件的末尾会显示一些无效字符:

$ gulp
module.js:85
    throw e;
    ^

SyntaxError: Error parsing /vagrant/node_modules/gulp-bower/node_modules/bower/lib/node_modules/graceful-fs/package.json: Unexpected token 

如果你去看文件,你会看到很多奇怪的字符。

但是,正如我所说,无效的依赖包每次rm -Rf node_modules时都会有所不同,然后再次运行$ npm install

例如,第二次,我得到了这个:

$gulp
module.js:85
    throw e;
    ^

SyntaxError: Error parsing /vagrant/node_modules/gulp-bower/node_modules/bower/lib/node_modules/configstore/package.json: Unexpected token 

无效的 package.json 文件如下所示:

{
  "_args": [
    [
      "configstore@^2.0.0",
      "/private/var/folders/22/xz6_9gpx3jggts_8j68_25g80000gn/T/tmp-51023WwSVKpwQ7KvH"
    ]
  ],
  "_from": "configstore@>=2.0.0 <3.0.0",
  "_id": "configstore@2.0.0",
  "_inCache": true,
  "_installable": true,
  "_location": "/configstore",
  "_nodeVersion": "4.3.0",
  "_npmOperationalInternal": {
    "host": "packages-5-east.internal.npmjs.com",
    "tmp": "tmp/configstore-2.0.0.tgz_1456822157166_0.897884224774316"
  },
  "_npmUser": {
    "email": "sindresorhus@gmail.com",
    "name": "sindresorhus"
  },
  "_npmVersion": "2.14.12",
  "_phantomChildren": {},
  "_requested": {
    "name": "configstore",
    "raw": "configstore@^2.0.0",
    "rawSpec": "^2.0.0",
    "scope": null,
    "spec": ">=2.0.0 <3.0.0",
    "type": "range"
  },
  "_requiredBy": [
    "/",
    "/update-notifier"
  ],
  "_resolved": "https://registry.npmjs.org/configstore/-/configstore-2.0.0.tgz",
  "_shasum": "8d81e9cdfa73ebd0e06bc985147856b2f1c4e764",
  "_shrinkwrap": null,
  "_spec": "configstore@^2.0.0",
  "_where": "/private/var/folders/22/xz6_9gpx3jggts_8j68_25g80000gn/T/tmp-51023WwSVKpwQ7KvH",
  "author": {
    "email": "sindresorhus@gmail.com",
    "name": "Sindre Sorhus",
    "url": "sindresorhus.com"
  },
  "bugs"    Bp6��5�f5���Ip6���G�9Y��G�9)F[���т]�=ٞp6�1AP��1AP���#��yAP���v�nY��G�91AP���AP��ٞp6�1AP��1AP���#��yAP���v�nY��G�91AP���AP��  Bp6��G�9yAP���Ip6��>6���ys7)F[��I�Mq8�6�f5I�Mq8�Ip6� ��ys7�:�ys7)F[���6�f5Q5��~
In�f5q5��~
a��G�99Sp6�P7/
AIp6�dpackage/library/fn/array/virtual/reduce.js�iCp6��y��G�9�^�.1AP��1AP�����f5���f5Y�O{�AP��}^�.1AP��1AP�����f5Y�O{�AP���AP����f��1AP��1AP�����f5Y�O{�AP���AP����f��1AP��1AP�����f5���f5Y�O{�AP��AIp6�(db3204cd5a9de2e6cd890b85c6e2f66bcf4f620aAIp6�inflight@>=1.0.4 <2.0.0AIp6�3.9.1AIp6�5.10.1�s��'1AP��1AP��y�G�9��G�9�AP���AP��a�f��1AP��1AP�����f5���f5Y�O{�AP���f��I��O{��G�9���'1AP��1AP����G�99�G�9�AP���AP����`��1AP��1AP��Y�O{�AP���AP���AP��AIp6�8https://registry.npmjs.org/inflight/-/inflight-1.0.5.tgzAIp6�ERROR: No README data found!/nAIp6�� has}
aMp6�Z�gu�
 Mp6�45��G�91    Y��  Bp6�Y>�f5���Ip6���G�9I��G�9

如果我从主机运行$ npm install,则会正确下载软件包并且$ gulp运行正常。

我的配置

我不相信我的配置会相关,因为命令是在$ vagrant ssh之后执行的,但是,这里是:

Vagrantfile

Vagrant.configure(2) do |config|
  config.vm.box = "geerlingguy/ubuntu1604"

  config.vm.synced_folder "../laravel", "/vagrant",
    owner: "vagrant",
    group: "www-data",
    mount_options: ["dmode=775,fmode=775"]
  config.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true
  config.vm.network :forwarded_port, guest: 3306, host: 3306, auto_correct: true
  config.vm.provision :hostmanager
  config.hostmanager.manage_host = true
  config.hostmanager.manage_guest = true
  config.hostmanager.ignore_private_ip = false
  config.hostmanager.include_offline = true

  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "1024"
    vb.cpus = "1"
    vb.name = "Dev"
  end

  config.vm.define "dev" do |dev|
    config.vm.hostname = "dev.local"
    dev.vm.network :private_network, ip: '192.168.11.25'
    dev.vm.provision "ansible" do |ansible|
      ansible.playbook = "provisioning/playbook.yml"
    end
  end
end

main.yml

#Apache 2 Settings
apache_listen_port: 80
apache_create_vhosts: true
apache_remove_default_vhost: true
apache_global_vhost_settings: |
  DirectoryIndex index.php index.html
apache_vhosts:
  - servername: "dev.local"
    documentroot: "/vagrant/public/"
    serveralias: "dev.local"


#PHP Settings
php_use_managed_ini: false
php_error_reporting: "E_ALL &amp; ~E_DEPRECATED &amp; ~E_STRICT"
php_display_errors: "On"
php_display_startup_errors: "On"
php_packages:
  - libapache2-mod-php7.0
  - php7.0-mysql
  - php7.0-curl
  - php7.0-mcrypt
  - php7.0-mbstring
  - php7.0-xml
  - php7.0-gd
  - php7.0-common
  - php7.0-cli
  - php7.0-dev
  - php7.0-fpm
  - libpcre3-dev
  - php7.0-imap
  - php7.0-json
  - php7.0-opcache
  - php7.0-intl
  - php7.0-zip
  - php-sqlite3
  - php-apcu

#MySQL Settings
mysql_root_password: root
mysql_root_password_update: yes
mysql_bind_address: '0.0.0.0'

# NodeJS
nodejs_version: "4.x"

#Redis Settings
redis_port: 6379
redis_bind_interface: 0.0.0.0

playbook.yml

---
- name: Dev machine setup
  hosts: dev
  user: vagrant
  become: yes

  vars_files:
    - vars/main.yml

  pre_tasks:
    - apt: name=unzip update_cache=yes state=present

  roles:
    - role: geerlingguy.mysql
    - role: geerlingguy.apache
    - role: geerlingguy.php
    - role: geerlingguy.php-mysql
    - role: geerlingguy.nodejs
    - role: geerlingguy.redis
    - role: geerlingguy.apache-php-fpm
    - role: geerlingguy.php-xdebug
    - role: geerlingguy.composer

  tasks:

  - name: Allow root remote access
    shell: >
      mysql -u root -proot -NBe
      "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';"

  - name: Ensure MySQL databases are present.
    mysql_db:
      name: appdb
      encoding: utf8
      collation: utf8_unicode_ci

  # .env File Settings
  - name: Copy and rename .env.example
    template: src=../../laravel/.env.example dest=/vagrant/.env
  - name: set DB_HOST=dev.local
    lineinfile: dest=/vagrant/.env regexp='^DB_HOST=' line=DB_HOST=dev.local
  - name: set DB_DATABASE=appdb
    lineinfile: dest=/vagrant/.env regexp='^DB_DATABASE=' line=DB_DATABASE=appdb
  - name: set DB_USERNAME=root
    lineinfile: dest=/vagrant/.env regexp='^DB_USERNAME=' line=DB_USERNAME=root
  - name: set DB_PASSWORD=root
    lineinfile: dest=/vagrant/.env regexp='^DB_PASSWORD=' line=DB_PASSWORD=root

  # Storage Settings
  - file: path=/vagrant/storage/framework/sessions state=directory mode=0777
  - file: path=/vagrant/storage/framework/views state=directory mode=0777
  - file: path=/vagrant/storage/framework/cache state=directory mode=0777

  # Composer
  - composer: command=install working_dir=/vagrant/ no_dev=no

  # NPM Global
  - name: Install Gulp-cli globally.
    npm: name=gulp-cli global=yes

  - name: Install gulp globaly
    npm: name=gulp global=yes

  - name: Install bower globally.
    npm: name=bower global=yes

  # Artisan Tasks
  - name: Install Migrations and Seed
    command: php artisan migrate:refresh --seed
    become: true
    become_user: vagrant
    args:
      chdir: /vagrant/

  - name: Generate a new app key
    command: php artisan key:generate
    become: true
    become_user: vagrant
    args:
      chdir: /vagrant/

  - name: Generate JS Routes
    command: php artisan laroute:generate
    become: true
    become_user: vagrant
    args:
      chdir: /vagrant/

  - name: Create Symbolic Link
    file: src=/vagrant/storage/app/public dest=/vagrant/public/storage state=link

1 个答案:

答案 0 :(得分:0)

事实证明,在文件夹同步期间这是一个问题。我已经改变了这个

config.vm.synced_folder "../laravel", "/vagrant",
    :nfs => true,
    :linux__nfs_options => ['rw','no_subtree_check','all_squash','async']

它开始工作了。

最后!