eirslett / frontend-maven-plugin

"Maven-node-grunt-gulp-npm-node-plugin to end all maven-node-grunt-gulp-npm-plugins." A Maven plugin that downloads/installs Node and NPM locally, runs NPM install, Grunt, Gulp and/or Karma.
Apache License 2.0
4.2k stars 867 forks source link

Private Registry Authentication - Npm Install #1067

Closed Sovenique closed 1 year ago

Sovenique commented 1 year ago

Greetings Community,

This is a question and not a issue, please label it properly. My apologies if this is not the right place to ask.

OS: Ubuntu 20.04 frontend-maven-plugin: 1.12.1 node: v16.14.0 npm: 8.3.1

Thing is that during npm install which is invoked through the plugin, I can't seem to find a way to authenticate to a private npm registry in order to download specific dependencies.

I've read the documentation and noticed there's a <npmRegistryURL>http://myregistry.example.org/</npmRegistryURL> directive but as yarn install configuration and not npm install. Is this something that can be consumed from npm install as well? If that's the case, where I could provide my credentials? An external settings.xml file would do?

This is my configuration:

 <plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.12.1</version>
    <configuration>
        <nodeVersion>v16.14.0</nodeVersion>
        <npmVersion>8.3.1</npmVersion>
    </configuration>
    <executions>
        <execution>
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
        </execution>
        <execution>
            <id>npm install</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>install</arguments>
            </configuration>
        </execution>
        <execution>
            <id>npm run build</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>run build-dev</arguments>
            </configuration>
        </execution>
    </executions>
  </plugin>

Thanks in advance!

eirslett commented 1 year ago

I would recommending putting registry config in an .npmrc file.

mnckstle commented 1 year ago

I have a similar issue with my .npmrc file which contains a line "registry=https://my.internal.vpn.nexusrepo/repository/npm-repo/" and an additional line containing the nexus auth token.

I tried to put it into the "project's root directory" (which I suppose the the root directory, where the pom.xml resides), but also in the node-folder of the installDirectory, as well as workingDirectory. On all three places the .npmrc seems to be ignored, since the registry-URL inside the .npmrc-file is only reachable with an active VPN-connection, which I shutdown before the mvn build but the node_modules folder inside the working directory gets filled up nevertheless.

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.12.1</version>
    <executions>
        <execution>
            <id>install node and npm</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <configuration>
                <!-- See https://nodejs.org/en/download/ for latest node and npm (lts) versions -->
                <nodeVersion>v18.14.0</nodeVersion>
            </configuration>
        </execution>
        <execution>
            <id>npm ci</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>ci</arguments>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <workingDirectory>${project.basedir}/target/checkout</workingDirectory>
        <installDirectory>${project.basedir}/target</installDirectory>-->
    </configuration>
</plugin>

@eirslett Could you please give me an advice? Or could this be related to the "npm ci" command instead of running "npm install"?

Thanks in advance!

PS: This is more or less the folder-structure. "checkout"-Folder is where the node-sourcecode incl. package.json resides.

projectDirTree

PPS: I think I found the problem. Before I switched off the VPN/used my own .npmrc file with the private repository inside, I built the maven project several times. It seems that the contents of node_modules were somewhere cached on my machine, so npm just took those from the cache location instead of downloading them via whatever registry is being set. After I ran npm cache clean --force, it respected the .npmrc in the root folder (so next to the pom.xml) and without having VPN enabled it timed-out when trying to download contents for the node_modules directory. Sorry again for the fuss :)