I am in need of a package that is required as both a devDependency and a regular

Encountered an issue where I needed the same package to be included as both a dependency and a devDependency.

The specific packages in question are @babel/core and @babel/preset-env.

Reason?

Because my package is designed to facilitate transpilation of another part of the project in the future:

  • Original source code is transpiled using babel (as a dev dependency).
  • Transpiled code is then published on NPM.
  • Later, the transpiled source from NPM will be processed through webpack (which relies on babel-loader and therefore requires @babel/core). This step will be performed by end users using webpack.

I aim to include all necessary dev-assets (excluding webpack) within the package so that end users do not need to separately install babel-loader, @babel/core, etc.

Initially, I considered adding them as dependencies, but found this approach somewhat messy.

This could potentially be categorized as an X/Y problem?

The solution that seems to work involves:

  • Creating a separate package named xxx-dev-dependencies.
  • Incorporating the dependencies required for webpack transpilation into that subsequent package.
  • Requiring the package and passing the modules to webpack.

HOWEVER ... I consistently receive a warning when installing the dev dependency package:

[email protected] necessitates a peer version of webpack@>=2 which is currently missing. It is necessary to manually install peer dependencies.

Even though I have added webpack to peerDependencies as illustrated below:

  "name": "xxx-dev-dependencies",
  "dependencies": {
    "@babel/core": "^7.9.6",
    "@babel/preset-env": "^7.9.6",
    "babel-loader": "^8.1.0"
  },
  "peerDependencies": {
    "webpack": "4.x"
  }

Answer №1

After some experimentation, I came up with a workaround that seems to do the trick:

  • My Solution

    • Development Dependencies
      • @babel/core
      • @babel/preset-env
      • my-project-dev-deps
  • my-project-dev-deps

    • Dependencies
      • @babel/core
      • @babel/preset-env
      • babel-loader
      • webpack (added to address peer dependency warning)
    • Peer dependencies
      • webpack (included to ensure end user has webpack installed)

The `my-project-dev-deps` module essentially exports the dependencies in this format:

var path = require("path")

module.exports = {
    babel: {
        loader: path.resolve(__dirname, "node_modules", "babel-loader"),
        presets: [
            path.resolve(__dirname, "node_modules", "@babel", "preset-env")
        ]
    }
}

`my-project` then imports `my-project-dev-deps` and uses it as follows:

var dep = require("my-project-dev-deps")
// ..
module: {
    rules: [{
        test: /\.js$/,
        use: {
            loader: dep.babel.loader,
            options: {
                presets: dep.babel.presets
            }
        }
    }]
}

I have personally tested that webpack does indeed utilize the `@babel/core` version packaged within `my-project-dev-deps`.

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

Combining arrays in JavaScript to form object values with lodash

In my current project, I am attempting to manipulate JavaScript object arrays using lodash. The goal is to stack array values from different objects and map them to corresponding objects in a new array. Here's an example of what I'm working with: ...

Looping in REACT with state updates can cause the values to be overwritten

I'm encountering a problem with my function in React that updates the state. It fetches data from a URL in an array and creates a new item, but when trying to update another state array with this new object, it keeps overriding the first item each tim ...

The MVC execution sequence: Controller initialization happening after Ajax call

I'm struggling to understand the execution order in MVC architecture. In my code, I am overriding initialization in the following way: public class HomeController: Controller { protected override void Initialize(RequestContext requestContext) ...

Ways to showcase JSON data with jQuery

My code displays movie data from a JSON variable and populates it on a dropdown list based on the selected city. I aim to include show timings along with other details from the JSON content. The code snippet is as follows: $(document).ready(function() ...

Include the JS file after finishing the control processing

I've been grappling with an issue for several days now. The view I have is populated by my controller through an API call, which works perfectly fine in rendering the HTML. $http.get(url). success(function(data, status, headers, config) { ...

Flow the child process output as it streams

I have a unique custom command line tool implemented in Python which uses the "print" statement to display its output. To interact with this tool from Node.js, I spawn a child process and send commands to it using the child.stdin.write method. Below is an ...

CASL user update has been refreshed

My CASL implementation is quite basic and I've noticed that the documentation lacks detail. The code I'm using is essentially a copy-paste from the docs: import { abilitiesPlugin } from '@casl/vue' import defineAbilitiesFor from &apos ...

Transferring MongoDB information to a Jade template in an ExpressJS application

Hey there, hoping you can assist me with a query issue I'm facing. To give you some context, I am querying a MongoDB collection and trying to pass the results back to a Jade view. app.helpers({ clients: function(){ users.find({uid:req.session.u ...

Error encountered when referencing iscrollview and iscroll js

Greetings! I am new to the world of JavaScript and jQuery, currently working on developing a phonegap application. As part of my project, I am exploring the implementation of the pull-to-refresh feature using iscroll and iscrollview as demonstrated on & ...

What could be causing the error when trying to use a PUT request with fetch in Vue?

Whenever I attempt to send a PUT request, an error pops up on the console. export function putUserData() { fetch(`${url}/user/${getCookie("ID")}`, { method: "PUT", headers: { "Authorization": `B ...

Using Node-Forge for importing in an Angular 2 service

I've been attempting to incorporate Forge (https://github.com/digitalbazaar/forge) into my Angular 2 project. After executing the command :npm install node-forge, the node-forge directory was created within my application (in the node-modules directo ...

The AJAX comments triggered an Uncaught SyntaxError due to an unexpected identifier

Can someone assist me in identifying the issue? I keep receiving an Unexpected identifier error when attempting to use ajax for sending comments. Code: function funcSuccess(data) { $("#comment_ajax").text(data); } function funcBefore() { $("#comme ...

Utilize GetXmlHttpObject for an AJAX request to interact with a PHP script

I am currently attempting to call a PHP file using the GetXmlHttpObject object and so far, I have had some progress. However, it seems that there is an issue with the URL variable. Do I need to handle the URL string in a different way? Here is the releva ...

Issue with preventDefault not functioning correctly within a Bootstrap popover when trying to submit a

I am facing an issue with a bootstrap popover element containing a form. Even though I use preventDefault() when the form is submitted, it does not actually prevent the submit action. Interestingly, when I replace the popover with a modal, the functional ...

Issue with Material UI dropdown not selecting value on Enter key press

I have encountered an issue with the material UI dropdown component that I am using. When I navigate through the dropdown options using arrow keys and press enter, the selected value does not get highlighted. Below is the code snippet for the dropdown: ...

Ways to divide a string into pairs of two using jQuery

I have a variable containing a string with exactly 44 characters: 02081516171821242936374750565865666871737476 I want to split it into an array in the following format: arr[0]=02 arr[1]=08 . . arr[21]=76 Can someone help me achieve this? Thank you. UP ...

Activate Bootstrap - navigate to a specific tab and scroll to a designated anchor upon link click

My goal is to switch to another tab and scroll to a specific part of the page after clicking on a link inside a tab. Unfortunately, although I have successfully switched tabs, I am unable to scroll down to the anchor point. I would greatly appreciate any ...

Error alert: The $ symbol is not defined

I followed a tutorial to create an auto-advancing slideshow in JavaScript/jQuery and it worked perfectly on jsfiddle. However, when I transferred everything to Dreamweaver, it stopped functioning. I have triple-checked that all the necessary files are link ...

Working with Garber-Irish in Rails: Streamlining Administration and Keeping Code DRY

I am currently implementing the innovative garber-irish technique to organize my JavaScript files. Here's my issue: I have a Model (let's call it Item) with an init function located in app/assets/javascripts/item/item.js For example: MYAPP.ite ...

Discovering the minimum and maximum within an array containing objects

I am working with an array of objects that contain latitude and longitude points: var pts = [ { "X": 52.67528921580262, "Y": 8.373513221740723 }, { "X": 52.6759657545252, "Y": 8.374114036560059 }, { "X": 52.682574466310314, "Y": 8.372569084 ...