错误!添加 cordova 插件时代码 ENOENT(cordova 删除它正在寻找的文件)

npm ERR! code ENOENT when adding cordova plugins (cordova deletes the file it's looking for)

我陷入了一个有点无限的挣扎循环!请帮忙 xD

无论我使用什么 phonegap 或 cordova cli 命令 运行,cordova 似乎都会在查找文件之前删除它正在查找的文件。

这里只是一些已知的命令,它们会为我触发错误:

$ cordova plugin add <plugin name>

$ phonegap plugin add <plugin name>

$ cordova platform update <platform name>

$ phonegap platform update <platform name>

$ cordova platform remove <platform name>

$ phonegap platform remove <platform name>

请参阅下面的终端输出,它显示了我在 运行 执行这些命令时得到的输出。我真的不确定为什么 cordova 在尝试重命名之前删除它试图重命名的文件。非常令人沮丧!

第 1 步(检查以确保我们有一个干净的 git 头部并且我们的工作集没有变化)

apple@Child-2:~/projects/infiniteinternet/prodmode/prodmode-client-mobile-builds/phonegap * feature-barcode-scanner$ git status
# On branch feature-barcode-scanner
nothing to commit, working directory clean

步骤 2(尝试添加插件)

apple@Child-2:~/projects/infiniteinternet/prodmode/prodmode-client-mobile-builds/phonegap * feature-barcode-scanner$ cordova plugin add phonegap-plugin-barcodescanner --variable CAMERA_USAGE_DESCRIPTION="To scan barcodes"
Unmet project requirements for latest version of phonegap-plugin-barcodescanner:
cordova-android (6.2.3 in project, >=6.3.0 required)
Fetching highest version of phonegap-plugin-barcodescanner that this project supports: 6.0.8 (latest is 7.0.0)
Error: Failed to fetch plugin phonegap-plugin-barcodescanner@6.0.8 via registry.
Probably this is either a connection problem, or plugin spec is incorrect.
Check your connection and plugin name/version/URL.
Error: npm: Command failed with exit code 254 Error output:
npm WARN cordova-plugin-file-transfer@1.5.1 requires a peer of cordova-plugin-file@^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN cordova-plugin-media-capture@1.2.0 requires a peer of cordova-plugin-file@^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN com.prodmod.mobileclient@1.0.0 No repository field.
npm WARN com.prodmod.mobileclient@1.0.0 No license field.

npm ERR! path prodmode-client-mobile-builds/phonegap/node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename
npm ERR! enoent ENOENT: no such file or directory, rename 'prodmode-client-mobile-builds/phonegap/node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js' -> 'prodmode-client-mobile-builds/phonegap/node_modules/cordova-ios/node_modules/simple-plist/node_modules/.base64-js.DELETE'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

┌──────────────────────────────────────────────────────────┐
│                 npm update check failed                  │
│           Try running with sudo or get access            │
│           to the local update config store via           │
│ sudo chown -R $USER:$(id -gn $USER) /Users/apple/.config │
└──────────────────────────────────────────────────────────┘

npm ERR! A complete log of this run can be found in:
npm ERR! .npm/_logs/2017-11-10T00_23_21_467Z-debug.log

步骤3(检查git步骤2中命令引起的变化)

好的,所以它失败了。让我们找出假定丢失的文件发生了什么。好吧,显然 cordova 在查找文件之前删除了文件?

apple@Child-2:~/projects/infiniteinternet/prodmode/prodmode-client-mobile-builds/phonegap * feature-barcode-scanner$ git status
# On branch feature-barcode-scanner
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/.npmignore
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/.travis.yml
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/LICENSE.MIT
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/README.md
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/base64js.min.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/bower.json
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/lib/b64.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/package.json
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/test/big-data.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/test/convert.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/base64-js/test/url-safe.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/.jshintrc
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/.travis.yml
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/History.md
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/LICENSE
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/Makefile
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/README.md
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/dist/plist-build.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/dist/plist-parse.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/dist/plist.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/examples/browser/index.html
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/index.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/lib/build.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/lib/parse.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/plist/package.json
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/.npmignore
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/CHANGELOG.md
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/LICENSE
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/README.md
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/Utility.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLAttribute.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLCData.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLComment.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLDTDAttList.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLDTDElement.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLDTDEntity.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLDTDNotation.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLDeclaration.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLDocType.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLDocument.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLDocumentCB.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLElement.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLNode.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLProcessingInstruction.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLRaw.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLStreamWriter.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLStringWriter.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLStringifier.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLText.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/XMLWriterBase.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/lib/index.js
#   deleted:    node_modules/cordova-ios/node_modules/simple-plist/node_modules/xmlbuilder/package.json
#
no changes added to commit (use "git add" and/or "git commit -a")

如果有人对如何解决此问题有任何建议,我将不胜感激。我不确定是什么原因造成的,也不知道它是什么时候生效的,但现在已经有几个星期了,我还无法将插件添加到我的项目中。

提前致谢!

编辑: 环境设置:

apple@Child-2:/ $ cordova --version
7.0.1


┌──────────────────────────────────────────┐
│ Update available: 7.1.0 (current: 7.0.1) │
│ Run npm install -g cordova to update.    │
└──────────────────────────────────────────┘

apple@Child-2:/ $ phonegap --version
7.1.1
apple@Child-2:/ $ node --version
v6.11.1
apple@Child-2:/ $ npm --version
5.4.2

我真的不知道你的项目哪里出了问题,但是当我升级 cordova 版本并将我的项目从 Eclipse 迁移到 Android Studio 时,我有类似的行为。

添加一些插件导致多次删除,我的所有项目都不起作用

我尝试了很多方法来修复它但都失败了,每次都有其他异常。


所以我通过从头开始创建新项目来解决它。一一添加所有插件,然后从旧项目复制所有 www 文件夹。

我花了大约 30 分钟,但很值得。


提示: 我会将 node_modules 文件夹放入 .gitignore 文件。您不需要将所有依赖项推送到 git。用户可以从根项目中克隆项目并 运行 $ npm install

嗯,所以我能够解决这个问题。线程中的讨论有助于在没有实际解决问题的情况下引导我找到正确的答案。

这是我所做的:

第一步: 更新cordova

$ sudo npm install -g cordova

Phonegap 已经更新到最新版本,但我的 cordova 还没有。

第二步更新节点和npm

显然,在更新这 2 个组件后,我的应用程序中的所有内容都恢复正常了。此时它将使用 phonegap plugin add <plugin name> 安装到 /plugins,但它不会将插件代码传播到 Android,直到我切换 Maven 存储库的位置。

第三步: 更新 build.gradle

中的 Maven 存储库位置

这与我的问题并没有真正的关系,但是我在 <app root>/phonegap/platforms/android/build.gradle

中更改 Maven 存储库位置后就能够构建 apk 权限

这是我在 build.gradle...

中所做的更改

我原来的build.gradle设置:

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }

    // Switch the Android Gradle plugin version requirement depending on the
    // installed version of Gradle. This dependency is documented at
    // http://tools.android.com/tech-docs/new-build-system/version-compatibility
    // and https://issues.apache.org/jira/browse/CB-8143
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
    }
}

// Allow plugins to declare Maven dependencies via build-extras.gradle.
allprojects {
    repositories {
        mavenCentral()
        jcenter()
    }
} 

我改成了:

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }

    // Switch the Android Gradle plugin version requirement depending on the
    // installed version of Gradle. This dependency is documented at
    // http://tools.android.com/tech-docs/new-build-system/version-compatibility
   // and https://issues.apache.org/jira/browse/CB-8143
   dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
   }
}

// Allow plugins to declare Maven dependencies via build-extras.gradle.
allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}

之后,我能够使用正确的插件权限构建我的 APK,并使用我添加的插件。