Installing Optional Dependencies with NPM

In our shared library, we have streamlined the bootstrapping code installation through Bootstrap for most users who utilize plain Javascript on the front end. However, there are also a few individuals who prefer to work with Typescript.

Could it be possible to incorporate a list of dependencies specific to Typescript in the package.json file? These could include modules like @types and other components related to Typescript, which would only be installed if a certain flag is provided. For instance, running npm install would solely install the standard packages, while executing npm install --some-flag would trigger the installation of the supplementary packages as well.

Answer №1

Perhaps a solution to your problem involves utilizing optionalDependencies in conjunction with the --no-optional flag.

As stated on https://docs.npmjs.com/files/package.json :

optionalDependencies

If a dependency is not crucial for your project, you can specify it within the optionalDependencies object. This allows npm to continue with installation even if the optional dependency cannot be found or fails to install. It functions similarly to dependencies but does not halt the installation process upon build failures.

It's important for your program to handle situations where the optional dependency is missing. For example:

try {
  var bar = require('bar')
  var barVersion = require('bar/package.json').version
} catch (error) {
  bar = null
}
if ( notGoodBarVersion(barVersion) ) {
  bar = null
}

// .. later in your code ..

if (bar) {
  bar.doBarThings()
}

Entries in optionalDependencies will take precedence over entries with the same name in dependencies, so it's recommended to consolidate these declarations.

Additionally, on https://docs.npmjs.com/cli/install :

The --no-optional flag can be used to exclude optional dependencies from the installation process.

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

Adjusting ngClass dynamically as values change

Currently, I am facing a situation where I want to dynamically add a class to my view using ngClass based on changes in the output value. The output value is dependent on the response received from an API, and I am fetching data from the endpoint every sec ...

A specialized React hook designed for fetching data in a continuous loop

Been struggling with this issue for hours :( I have a hook called useCollection which is designed to provide data retrieved from my API and updated in real-time using a websocket. It works perfectly for real-time updates, but I want it to also update the ...

Commands for NPM and Git are not being identified

I'm encountering the following errors: 'git' is not recognized as an internal or external command, operable program or batch file. and 'npm' is not recognized as an internal or external command, operable program or batch file. ...

Simple method to determine if a variable belongs to an enumeration

Among the numerous discussions on this topic, none seem to focus on developing a versatile function. This function should take an enum and a variable as input, check if the variable is part of that enum, and update the variable type if it is. I have made ...

Nodemailer configurations with Mailtrap (Issue: Exceeding email rate limit)

How can I properly set up nodemailer options for the mailtrap free version? I keep encountering this error consistently despite my attempts: "Error: Data command failed: 550 5.7.0 Requested action not taken: too many emails per second" Note: Mailtrap fre ...

"I am having trouble calling the useStyles function in React with Typescript and Material-

There seems to be a problem with calling the useStyles function as it is throwing the following error message: This expression is not callable. Type 'never' has no call signatures.ts(2349) const useStyles: never Below is the complete code snip ...

Issue with self-published NPM package: Imports not functioning after installation

I released a simple NPM package that contains the following code: module.exports = { foo: "baz" }; After being webpacked, the code transforms into the following, and this file is designated as the main property in the package.json. (()=>{var ...

What is the solution for positioning a checkbox above a label in ngx-formly?

The checkbox control is displayed on top of the label. The control is defined as follows: { key: 'selected', type: 'checkbox', templateOptions: { label: 'Virtual Bollo', indet ...

Verify whether a component is a React.ReactElement<any> instance within a child mapping operation

I am facing a challenge with a component that loops through children and wraps them in a div. I want to exclude certain elements from this process, but I am struggling to determine if the child is a ReactElement or not (React.ReactChild can be a string or ...

Challenge with the dependencies among peers

I recently integrated @material-ui/pickers into my React project and received warnings about installing peer dependencies. I followed the instructions to install them, but there is still one persistent warning. Even though I installed the dependency, I con ...

Attempting to add the graphql package to a Node.js project

npm ERR! code ENOSELF npm ERR! The installation of the package named "graphql" under a package npm ERR! with the same name "graphql" is being refused. Have you accidentally npm ERR! named your project the same as the dependency you ...

Using the angular2-cookie library in an Angular 2 project built on the rc5 version

Starting a new angular2 rc5 project, I wanted to import the angular2 cookie module. After installing the module with npm, I made changes to my angular-cli-build.js file : npm install angular2-cookie edited my angular-cli-build.js file : module.exports ...

Angular allows for a maximum time span of 60 days between two date inputs

I am looking to implement a validation in JavaScript or TypeScript for Angular where the start date cannot be more than 60 days after the end date is entered. The requirement is to enforce a "maximum range of 60 days" between the two input dates (dateFro ...

Having trouble with clearInterval in React TypeScript?

I have been encountering issues with the clearInterval function in TypeScript for React. I am not sure why the interval is not being cleared. To address this problem, I defined a variable let interval_counter;, and used it as follows: interval_counter = ...

Learn how to bring a component into another component within Angular

I have developed a component named CopySchedulefromSiteComponent and now I am looking to import it into another component called SiteScheduleComponent. However, I am unsure of the correct way to do this. The CopySchedulefromSiteComponent contains one fiel ...

Encountered a problem while setting up the uiautomator2 driver in Appium

Just starting out with appium, I gave a shot at installing the uiautomator2 driver on the latest version of appium (2.0.0-beta.41) using the command appium driver install uiautomator2 However, every time I attempt this, I encounter the following output: & ...

Is TypeScript to blame for the unexpected token error in Nock?

My code snippet in the ts file looks like this: nock('https://example.test').post('/submit').reply(200,{ "status": "Invalid", "message": "Invalid Request", }); However, when I try to ...

Searching and adding new elements to a sorted array of objects using binary insertion algorithm

I'm currently working on implementing a method to insert an object into a sorted array using binary search to determine the correct index for the new object. You can view the code on codesanbox The array I have is sorted using the following comparis ...

What is the process for converting this lambda type from Flow to TypeScript?

Currently, I am in the process of converting the below code snippet from Flow to TypeScript let headAndLines = headerAndRemainingLines(lines, spaceCountToTab), header: string[] = headAndLines.header, groups: string[][]= headAndLines.groups, ...

Angular 6 - Preload service with asynchronous requests prior to injecting dependencies

I've encountered an issue with my service that loads a configuration object in memory based on the environment file. The problem is that the settings are read asynchronously, causing the application to start before all settings are loaded and resultin ...