`How can I define dependencies and build an npm package?`

I have authored several npm modules, all of which are designed to enhance existing libraries such as three.js or react.

While the packages appear to be downloaded, I have not received any feedback confirming whether they have been implemented correctly.

Dependencies

What is the main objective behind defining dependencies at a high level?

three.js:

The process can be confusing as each "extension" assumes the existence of a THREE object within a certain context.

Therefore, my three.js module only includes:

"devDependencies": {
 "three": "^0.88.0"
}

And it is utilized in this manner:

require( 'three-instanced-mesh' )(THREE)

This approach both makes sense and raises questions.

The module cannot function without three.js and a proper context provided (via THREE), but since it is passed in at runtime(?), it may not truly be considered a dependency. However, when working on the repository and wanting to develop within it, installing three.js becomes necessary for the code to run.

React

I have released a React component with the intention that it should be used as follows:

  1. npm install my-module
  2. import MyModule from 'my-module'
  3. <MyModule/>

However, for some reason, I listed react as a peerDependencies dep.

Using <MyModule/> in JSX presupposes that measures have been taken to ensure react is already accessible in this context (similar to passing in THREE in the first example?).

The key difference lies in the fact that the class is not defined at runtime here, so importing it using import MyModule necessitates having react available in MyModule.js.

What exactly is the desired goal and how can it be articulated? All I know is that I do not wish for npm install my-module to result in a different version of react being installed, or causing additional react to be bundled into the final build (although I am uncertain about this).

What type of dependency (if any) should react be with respect to my-react-component, and how can it be effectively linked to my module?

For instance, should one utilize the externals feature with webpack or actually implement import React from 'react'?

Build

If I configure my repo to function with cutting-edge JS (or perhaps even something other than JS), what exactly should I publish and how?

import Foo from 'foo' //<-- where does 'foo' point and what is 'foo'?

Answer №1

What is the primary objective when outlining dependencies?

When defining dependencies, it is important to specify which ones are required for development purposes only (devDependencies), which ones are mandatory for package installation and will be automatically installed (dependencies), and peerDependencies, which need to be available but the user is expected to install them separately.

In this context, if I don't define the class at runtime and try to import MyModule, does it mean that React must be available in MyModule.js?

Yes, React would need to be accessible where the file is being imported. For example, if component A imports myModule, component A must have React imported as well. Therefore, specifying it as a peer dependency is recommended.

How does using externals with webpack differ from directly importing React with 'import React from 'react''?

Using externals in webpack instructs webpack not to bundle React and assumes that React has already been imported before the component import. On the other hand, manually importing React ensures its inclusion in the bundle.

If I configure my repository to support the latest features of JavaScript, how should I package and publish it?

The typical approach involves packaging the library into a minified/bundled index.js file and publishing it through npm. Ensure to set up the main field in the package.json file.

What does 'import Foo from 'foo'' point to and represent?

The 'foo' in the import statement refers to the name of the package that was created, under which it was published. When searching for 'foo' on npmjs.org, you will find the corresponding package. The 'foo' package is located within your node_modules directory.

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

Route parameters do not function correctly with computed properties

I'm facing an issue with my getter function that stores all products. When I try to retrieve a single product dynamically based on this.$route.params.id, it doesn't return any value. The code works fine when I navigate to a specific product, but ...

What is the process for populating dropdown options from state?

I've been struggling to populate a select element with options based on an array in state. Despite trying various methods, the code snippet below seems to be the most detailed (I'm still getting familiar with React after taking a break for a few ...

Error Loading JQuery: The function $() is not recognized in the Shopify platform

I seem to be overlooking something obvious. I am using Shopify and have included jQuery in the head section of the theme.liquid file: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> This script is placed rig ...

JavaScript - turn off online connectivity

Is there a way to test my website for offline use by disabling connectivity on the bootstrap before anything loads, so that all data will be loaded from cache? I have tried various offline libraries such as this one, but I haven't found a way to prog ...

Next.js 13 React Server Component not displaying updated data upon build completion

I have a React Server Component that retrieves its data at build time and does not reload it while the site is running. I expected it to fetch the data once when the server component is first rendered. Is there a way to force this server component to relo ...

Why does the selectedItems in the AngularJS grid keep coming back as undefined?

Is there a specific reason why the angularjs grid in my sample isn't displaying the selectedItems properly? Every time I attempt to reference the selectedItems, it returns "undefined." I have tested this in both Chrome and IE with identical outcomes. ...

What is the best way to replicate a synchronous ajax call? (mimicking synchronous behavior with asynchronous methods)

Given that a "native" synchronous ajax call can block the user interface of the browser, it may not be suitable for many real-world scenarios (including mine). I am curious to know if there is a way to mimic a synchronous (blocking) ajax call using an asy ...

Transforming Several Dropdowns using jQuery, JSON, and PHP

Hey there! I'm looking to update the state depending on the country and city based on the chosen state. <label>Country:</label><br/> <select onchange="getval(this)"> <option value="">Select Country</op ...

Updating a data table using POST values in ASP.NET: A step-by-step guide

I am working on updating values in my database and want to ensure that my script is functioning correctly. In order to check, I created a POST method to send the values and confirm they are being received. Now, my question is, once the values are being se ...

A guide on how to use Javascript to take a screenshot of an entire webpage

When a user triggers a script, it injects JavaScript code into the current page to make DOM changes. After interacting with the page, the user may want to save their modifications for later viewing or editing. However, if the original page source is edited ...

Is there a way to transform a fixed parameter into a user input value and display the function's outcome on a different page?

I am in the process of developing a straightforward web application for book searching based on ISBN. The website will feature a text input field for entering an ISBN and a submit button. Essentially, a user can enter an ISBN in the designated box, click s ...

Rearrange the sequence of numbers using JQuery when an HTML element is deleted

I'm currently working on a simple functionality where I have 5 rows, each with its own number. So initially, the rows are numbered from 5 to 1. If I remove a value like 3, the sequence should adjust to 4, 2, 1, indicating that I now have only 4 rows ...

Error occurs when incorporating OrbitalControl into Three.js leads to TypeError

Attempting to incorporate OrbitalControls into a Three.js Script to allow users to view two spheres in an empty space. However, upon running the code, the following error is encountered: three.js:5353 Uncaught TypeError: Cannot read property 'center& ...

Create new <div> elements dynamically within a loop using the value of a variable as a condition

I am in need of assistance with a function that generates news tiles ("cards") as displayed below: renderNews = <div className={styles["my-Grid-col"] + " " + styles["col-sm12"]+ " " + styles["col-md12"] + " " + styles["col-lg12"] + " " + styles["col-xl ...

The outcome of a function within the $.ajax method is transformed into a string

Trying to pass an array of IDs using the $.ajax data variable is proving to be a challenge. The array is generated by a function, and I've noticed that if I define this function outside of the $.ajax call, it works fine. However, when I place the same ...

The addition and deletion of classes can sometimes lead to disruptions in the DOM

I've been struggling to phrase this question for a while now. I'm working on a single-page e-commerce site that operates by modifying the HTML in divs and using CSS along with JQuery to show and hide those divs. My problem arises when, occasional ...

Ensure that the default value in the text box remains unchanged until new text is input by utilizing jQuery

Can you please review my code snippet on http://jsfiddle.net/7995m/? The issue I'm facing is that the default text in the text box disappears as soon as it's clicked. What I want is for the default text to remain until the user starts typing. Her ...

Changing HTML lists into JSON format

I am facing a slight issue with my HTML template while trying to convert it to JSON. My goal is to convert a list of HTML messages into JSON format. I appreciate your assistance in advance. HTML Template <div class="message"> <div class="mes ...

The ionic app was unable to initiate on Google Maps

Hello, I am facing an issue where my Ionic app fails to start after installing the Google Maps plugin. However, when I uninstall the plugin, the app starts successfully. The reason behind this behavior is unknown to me. I would appreciate any assistance yo ...

Vue router is designed to maintain the state of child components without requiring them to

Encountering a strange problem with vue-router. Developed a simple CRUD app for managing users. The app includes four main views: User list and a create button Create view with a form child component that fetches field data from an API within the creat ...