Unable to import local npm package due to an error

We are in the process of migrating multiple websites, each with its own project, to Vue.js. As we transfer files over and bundle them using Webpack, we have encountered a need to consolidate similar components and core JavaScript files into a shared library for efficiency.

To address this, we extracted various `.vue` files into a separate folder alongside the websites and organized them into a basic npm module with its own `package.json`. We included this shared module in our main projects by specifying a file path in the `package.json`, like so: `"vue-shared": "file:../CommonJavascript/Vue"`. However, when attempting to build the bundle using Webpack, we encountered an error:

ERROR in ../CommonJavascript/Vue/index.js Module build failed (from ./node_modules/eslint-loader/index.js): Error: Failed to load plugin react: Cannot find module 'eslint-plugin-react'

This issue puzzled us as we do not use React anywhere in our codebase, and everything compiled successfully prior to moving the files. Our shared module currently only has a dependency on Moment.js and contains just four `.vue` files along with a basic wrapper to package them together:

import button from 'Button.vue'
import loading from 'Loading.vue'
import modal from 'Modal.vue'
import progressBar from 'ProgressBar.vue'

export default {
  button,
  loading,
  modal,
  progressBar,
}

Upon adding the babel transform runtime package to investigate if it would resolve the initial error, we encountered a new one:

ERROR in ../CommonJavascript/Vue/index.js Module build failed (from ./node_modules/babel-loader/lib/index.js): ReferenceError: Unknown plugin "transform-runtime" specified in "base" at 0, attempted to resolve relative to "C:\Projects\Tmo\Code\CommonJavascript\Vue"

This second error seems more logical since the transform is utilized in our main project but not required in the shared module. Despite this, we are unsure why the inclusion in the primary project should cause build issues with the shared module.

It appears there may be confusion in how npm resolves dependencies, causing unexpected behavior. Additionally, the mystery dependency on `eslint-plugin-react` raises further questions about our approach. Is relocating `.vue` files to a separate entity, bundling them as a module, and importing them into the primary project the correct method? If not, what would be the optimal way to handle shared dependencies in this scenario?

Answer №1

This issue was a result of encountering two separate problems:

  • The problem with the import statements stemmed from not correctly referencing the file. The proper syntax should be:
    import button from './Button.vue'
    (noting the adjustment to the file path)
  • When adding a local package to npm through a path, it generates a symlink to the directory instead of duplicating the files (this has been the behavior since npm v5+). As a result, webpack's way of resolving dependencies changes as it now looks up from the location of the shared files to resolve dependencies like eslint and babel.
  • The issue with the eslint-plugin-react dependency arose because I had installed the eslint plugin in Visual Studio Code, which seemed to have created a .eslintrc file referencing the react plugin in my user folder (c:\users\<username>). Eslint defaults to using this if it cannot find a configuration file (which was the case here due to the pathing challenges mentioned earlier).

To address these issues going forward, we have decided to implement a git submodule for handling these files.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The combination of Angular Hottowel's 'blocks.exception' and 'blocks.router' prevents the App from being displayed in the browser

After delving into Angular's fundamentals a couple of months back, I am now venturing into building a practice app that mirrors industry standards. I recently completed John Papa's Play by Play and Clean Code courses on Pluralsight, which furthe ...

What is the best way to give stacked bar charts a rounded top and bottom edge?

Looking for recommendations on integrating c3.js chart library into an Angular 6 project. Any advice or suggestions would be greatly appreciated! ...

Using ngTable within an AngularJS application

While working on my angularjs application, I encountered an issue with ngtable during the grunt build process. It seems that the references are missing, resulting in the following error: Uncaught Error: [$injector:modulerr] Failed to instantiate module pa ...

React Object is throwing an error - not a function

I need assistance resolving this issue. The error Object(...) is not a function keeps appearing in my code. Here is the snippet of code that seems to be causing the problem: It centers around the declaration of const useStyles = makeStyles(() => ({. ...

Adjust words to fit any screen size as needed

Looking for a small program that can dynamically change a word within an SVG? The goal is to create an effect where regardless of the word or group of words, they always stretch along the entire height (due to a 90-degree rotation) by adjusting the font si ...

Conceal a particular object from view by selecting it from the menu

I want to hide a specific element when the burger button is clicked. (See CaptureElementNoDisplay.PNG for the element to hide) When I click the burger button again to close the menu, I want the hidden item to be displayed once more. I've successfull ...

Ways to update the initial value received as props in Vue3?

Imagine you're working with a todo-list component that displays the provided todos, where todos is of type Ref. <todo-list :todos="todos"/> This todo-list component also allows users to delete any of the displayed todos. However, in ...

Begin counting when a particular div element is visible on the screen

I have a plugins.init.js file that contains a try-catch block which runs on page load. I am looking for a way to execute this code only once when the div with the class counter-value comes into view. Is there a method to achieve this? try { const count ...

Switching buttons with AngularJS

I am currently working on a Github search app using the Github API in Angular. My goal is to make it so that when the user clicks the "Add to Favorite" button, the button disappears and the "Remove Favorite" button is displayed instead. I attempted to achi ...

The data retrieved from the PHP script is not accessible within the main Vue instance

I am currently working on a Vue.js component for a modal window. Once the user fills out all the necessary data in the fields, I need to achieve the following: Send the data to the server. Apply a timeout to show the user that the data is being sent (by ...

Using Node-twitter within the Express framework

I'm currently exploring the node-twitter module within the express framework. Although I have a basic understanding of node, including routing, rendering jade templates, and installing packages, I find myself overwhelmed by the sheer number of package ...

Creating a jQuery Mobile Multi-Select feature with dynamically loaded data

I'm facing an issue with integrating jQuery Mobile and AngularJS. I need a multi-select option for users to choose categories, which have parent-child relationships. The data structure is fetched asynchronously from a database. The problem is that t ...

retrieve the month and year data from the input date

I encountered a situation where I'm working with the following unique HTML code: <input type="date" id="myDate"/> <button type="button" class="btn btn-secondary" id="clickMe">MyButton</ ...

How can you prevent videos from constantly reloading when the same source is used in multiple components or pages?

How can we enhance loading performance in Javascript, React, or Next JS when utilizing the same video resource across various components on different pages of the website to prevent unnecessary reloading? Is there a method to store loaded videos in memor ...

Is it necessary to close the navigation drawer after selecting an item?

Currently, I'm working on a project for my University where I am trying to incorporate a feature that will automatically close the navigation drawer whenever any of its items are clicked. However, I am facing some challenges figuring out how to achiev ...

Interactive Inner Frame inspired by Google Maps, designed for easy dragging and navigating

Recently, I've been toying with the idea of creating a JavaScript game, particularly in the style of real-time strategy (RTS) games. The main question on my mind is this: How can I develop a draggable inner frame, akin to the functionality found in G ...

I am seeking assistance in transmitting data to my server utilizing Ajax, PHP, and mySQL without relying on a form

I have been researching tutorials on how to work with JavaScript without using forms. Currently, I have the JavaScript code that maps my answers based on clicks and generates an array shown in an alert. However, I am unsure if the Ajax request is sending i ...

The image map library functions seamlessly with React but encounters issues when integrated with Next.js

I have been working on a client project that requires the use of an image map. I searched for a suitable library, but struggled to find one that is easy to maintain. However, I came across this particular library that seemed promising. https://github.com/ ...

Saving form blueprints and operations in a Data Repository

My team and I are working on a sophisticated web application with a complex back end. We have hundreds of form schemas paired with their corresponding return functions, which are triggered upon form submission. These JSON objects dynamically generate forms ...

Verification of email address is required, emails must be unique and not duplicated

I am working on validating email addresses to ensure they are not repeated. So far, I have successfully pushed them from the server into an array using regex for validation. What steps should I take next to compare and further validate these emails? ...