gradle-node-plugin icon indicating copy to clipboard operation
gradle-node-plugin copied to clipboard

Custom .npmrc for npmSetup task

Open udalmik opened this issue 6 years ago • 7 comments

Hi!

If custom npm version is chosen, e.g. 5.7.1, then plugin installs it with npm install --global [email protected] ...other flags it works well in dev environment, however fails on CI environment if remote registry requires authentication, and there is no way to update agent's global settings.

The question - is there a way to use custom .npmrc file for this scenario?

If not, could you extend existing configuration with custom npmrc file location?

Thank you!

udalmik avatar May 16 '18 09:05 udalmik

I've checked this issue, however I think .npmrc from project dir will be ignored in case of installing with --global flag.

udalmik avatar May 16 '18 09:05 udalmik

I think I've found a workaround. Plugin uses --prefix attribute to set custom global location for installed npm, pointing to "$projectDir/.gradle/npm/npm-v$npmVersion". So we can copy custom config to this location before the npmSetup task.

def customNpmVersion = '5.7.1'

node {
  npmVersion = customNpmVersion
}

// generate/copy custom config to 'global' folder

task customNpmrc() {
    doLast {
        def customConfig = """
            # custom .npmrc content goes here
        """
        def configFolder = "$projectDir/.gradle/npm/npm-v$customNpmVersion/etc"
        file(configFolder).mkdirs()
        file("$configFolder/npmrc").text = customConfig;
    }
}

// add new task as a dependency to npmSetup

tasks.npmSetup.dependsOn customNpmrc

It depends on plugin's internal logic, so I'll keep this open to get more opinions on correct solution.

udalmik avatar May 16 '18 11:05 udalmik

npm has a --userconfig=/path/to/.npmrc flag you can pass, is there an option to add additional paramaters to the npm install command or npm build?

mnguyen36 avatar Jul 25 '19 20:07 mnguyen36

My workaround:

npmSetup {
  doFirst {
    copy {
      from '.npmrc'
      into "${node.npmWorkDir}/npm-v${node.npmVersion}/etc"
      rename '.npmrc', 'npmrc'
    }
  }
}

micheljung avatar Nov 27 '19 16:11 micheljung

Hi guys,

I'm trying to use a custom .npmrc to be able to authenticate on azure pipelines.

I've noticed that npm is being installed on "build/npm/npm-v6.9.0/lib/node_modules/npm/npmrc" I've tried to changed the content of this file with the authentication value and then run the nodesetup but it doesn't seems to be working.

any new ideas?

lopesdasilva avatar May 19 '20 16:05 lopesdasilva

@lopesdasilva , npm will try to load the ~/.npmrc file, if you want a more specific path then you would do --userconfig=/path/to/your/.npmrc

mnguyen36 avatar May 19 '20 17:05 mnguyen36

Is there some way to have the registry for the yarn install/npm install? I.e. I got the package installation AFTER the yarn/npm is installed already but I need the yarn/npm installation to come from a specific artifactory as well.

I tried using the workaround above but it'll still grab the package from https://registry.npmjs.org/yarn

clarencejychan avatar Jun 05 '20 00:06 clarencejychan