srs / gradle-node-plugin

Gradle plugin for integrating NodeJS in your build. :rocket:
Apache License 2.0
866 stars 211 forks source link

Custom .npmrc for npmSetup task #298

Open udalmik opened 6 years ago

udalmik commented 6 years ago

Hi!

If custom npm version is chosen, e.g. 5.7.1, then plugin installs it with npm install --global npm@5.7.1 ...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 commented 6 years ago

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

udalmik commented 6 years ago

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.

mnguyen36 commented 5 years ago

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?

micheljung commented 4 years ago

My workaround:

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

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?

mnguyen36 commented 4 years ago

@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

clarencejychan commented 4 years ago

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