在gradle-node-plugin中配置nodeModulesDir [英] Configuring nodeModulesDir in gradle-node-plugin
本文介绍了在gradle-node-plugin中配置nodeModulesDir的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图设置npm包的缓存。这是我的插件配置:
节点{
version ='4.5.0'
npmVersion =' 3.10.6'
distBaseUrl ='https://nodejs.org/dist'
download = true
workDir = file($ webAppSourceAbsolute / nodejs)
nodeModulesDir = file($ webAppSourceAbsolute /)
}
这是我的任务:
pre $ 任务npmCacheConfig(类型:NpmTask){
description =配置NPM缓存
outputs.upToDateWhen {
false
}
def npmCacheDir =$ {gradle.getGradleUserHomeDir()} / caches / npm
outputs.files文件(npmCacheDir)
args = ['config','set','cache',npmCacheDir]
}
但是当我运行这个任务时,我得到一个错误:
:arm-bpa:nodeSetup UP-TO- DATE
:arm-bpa:npmCacheConfig失败
失败:构建失败,出现异常。
*出错:
任务'myModule:npmCacheConfig'的执行失败。
>
无法运行npm命令 - 未找到本地npm,但在gradle节点配置中请求。
这样做的一个常见原因是存在npm-shrinkwrap.json文件并且不安装npm。
要解决这个问题,请将版本'3.10.6'的npm添加到package.json中。
我可以这样解决它:
npmCacheConfig.doFirst {
this.project.node.nodeModulesDir = file($ webAppSourceAbsolute / nodejs / node-v4.5.0-linux-x64 / lib /)
npmCacheConfig.doLast {
this.project.node.nodeModulesDir = file($ webAppSourceAbsolute /)
}
有没有办法在没有这种硬编码的情况下修复它?
解决方案 div>我已经用这种方式修复了它:
npmCacheConfig.doFirst {
def nodeJsDirectory = null
def nodeVersion = this.project.node.version
file($ webAppSourceAbsolute / nodejs /)。遍历(
类型:FileType.DIRECTORIES,
nameFilter:〜 ^ node-v $ nodeVersion。*,
postDir:{
return FileVisitResult.TERMINATE
}
){
nodeJsDirectory = it
}
if(nodeJsDirectory! = null){
this.project.node.nodeModulesDir = file($ nodeJsDirectory / lib /)
} else {
throw new IllegalStateException(nodejs is not installed)
$ b npmCacheConfig.doLast {
this.project.node.nodeModulesDir = file($ webAppSourceAbsolute /)
}
I trying to set up caching of npm packages. This is my plugin configuration:
node { version = '4.5.0' npmVersion = '3.10.6' distBaseUrl = 'https://nodejs.org/dist' download = true workDir = file("$webAppSourceAbsolute/nodejs") nodeModulesDir = file("$webAppSourceAbsolute/") }
And this is my task:
task npmCacheConfig(type: NpmTask) { description = "Configure the NPM cache" outputs.upToDateWhen { false } def npmCacheDir = "${gradle.getGradleUserHomeDir()}/caches/npm" outputs.files file(npmCacheDir) args = ['config', 'set', 'cache', npmCacheDir] }
But when I run this task, I got an error:
:arm-bpa:nodeSetup UP-TO-DATE :arm-bpa:npmCacheConfig FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':myModule:npmCacheConfig'. > Could not run npm command - local npm not found but requested in gradle node configuration. A common reason for this is an npm-shrinkwrap.json file is present and un-installs npm. To resolve this, add npm with version '3.10.6' to your package.json.
I can fix it this way:
npmCacheConfig.doFirst { this.project.node.nodeModulesDir = file("$webAppSourceAbsolute/nodejs/node-v4.5.0-linux-x64/lib/") } npmCacheConfig.doLast { this.project.node.nodeModulesDir = file("$webAppSourceAbsolute/") }
Is there any way to fix it without this hardcoding?
解决方案I have fixed it this way:
npmCacheConfig.doFirst { def nodeJsDirectory = null def nodeVersion = this.project.node.version file("$webAppSourceAbsolute/nodejs/").traverse( type: FileType.DIRECTORIES, nameFilter: ~"^node-v$nodeVersion.*", postDir: { return FileVisitResult.TERMINATE } ) { nodeJsDirectory = it } if (nodeJsDirectory != null) { this.project.node.nodeModulesDir = file("$nodeJsDirectory/lib/") } else { throw new IllegalStateException("nodejs is not installed") } } npmCacheConfig.doLast { this.project.node.nodeModulesDir = file("$webAppSourceAbsolute/") }
这篇关于在gradle-node-plugin中配置nodeModulesDir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文