svarog
svarog

Reputation: 9839

NetBeans can't run package.json on a Maven web application

Using NetBeans 8.2, I've created a new Maven web application. I have placed a working package.json file inside the Web Pages folder so its path is:

C:\Users\xxx\Documents\NetBeansProjects\MyApp\src\main\webapp\package.json

I left click on the package.jsonfile and click the npm install option. I was then greeted by these errors/warnings:

"C:\Program Files\nodejs\npm.cmd" "install"
npm WARN ENOENT ENOENT: no such file or directory, open 'C:\Users\Nick\Documents\NetBeansProjects\MyApp\package.json'
npm WARN EPACKAGEJSON C:\Users\xxx\Documents\NetBeansProjects\MyApp No description
npm WARN EPACKAGEJSON C:\Users\xxx\Documents\NetBeansProjects\MyApp No repository field.
npm WARN EPACKAGEJSON C:\Users\xxx\Documents\NetBeansProjects\MyApp No README data
npm WARN EPACKAGEJSON C:\Users\xxx\Documents\NetBeansProjects\MyApp No license field.
Done.

I noticed NetBeans attempted to look for the package.json file at the wrong place:

C:\Users\Nick\Documents\NetBeansProjects\MyApp\package.json'

And I can't seem to figure out how to tell the IDE where to look for it.

When I go to Project PropertiesJavaScript Librariesnpm, I get an empty view with :

package.json not found

How can I set up NetBeans to see that 'package.json' and run it? It worked fine when I tried using it in an HTML5 project, but I need npm support for a Java web application project.

Upvotes: 5

Views: 1925

Answers (1)

svarog
svarog

Reputation: 9839

I eventually found a solution outside the the builtin IDE mechanics. I used the frontend-maven-plugin to setup a local npm repository for my project.

Than instead of using the npm install option given by the IDE, I used Maven to define a goal, and run it using nbactions.xml.

  1. To install the plugin, edit your pom.xml file (snippet from here), add the plugin descriptor and the execution that will download and install npm inside your repository:

     <plugins>
         <plugin>
             <groupId>com.github.eirslett</groupId>
             <artifactId>frontend-maven-plugin</artifactId>
             <version>1.8.0</version>
             <executions>
                 <!-- Installs node + npm in your repo -->
                 <execution>
                     <id>install node and npm</id>
                     <goals>
                         <goal>install-node-and-npm</goal>
                     </goals>
                     <phase>generate-resources</phase>
                 </execution>
                 ...
    
  2. I've appended this code to the one above. It's an execution that runs npm install (snippet found here):

         ....
         <execution>
             <id>npm install</id>
             <goals>
                 <goal>npm</goal>
             </goals>
    
             <!-- optional: default phase is "generate-resources" -->
             <phase>generate-resources</phase>
    
             <configuration>
                 <!-- optional: The default argument is actually
                 "install", so unless you need to run some other npm command,
                 you can remove this whole <configuration> section.
                 -->
                 <arguments>install</arguments>
             </configuration>
         </execution>
     </plugin>
    
  3. Create a package.json file (in the same directory where your POM is) and start putting stuff into it (example)

  4. I've added a reference to the npm install goal inside nbactions.xml. I've gave it a display name of npm-install

     </actions>
         ...
         <action>
             <actionName>CUSTOM-npm-install</actionName>
             <displayName>npm-install</displayName>
             <goals>
                 <goal>npm</goal>
             </goals>
         </action>
     </actions>
    
  5. Then, right click on the Project (from the projects view) -> Run Maven -> npm-install

Enter image description here

The plugin will then run and install a package.json and a Node.js runtime in a separate directory that won't be bundled with your project, you can edit it there and rerun the goal.

Upvotes: 0

Related Questions