Executing NPM scripts in bin directory upon import

My TypeScript 4.8.4 library is being packaged with npm. Within my package.json, I have a "bin" section that includes several utility scripts for consumers of the package.

  "bin": {
    "generate-docs": "./docs/docs.py",
    "configure-this-and-that": "lib/configure-this-and-that.js",
    "register-things": "lib/data/register-things.js"
  }

The issue I am facing is that simply importing the package triggers the execution of all these scripts. This behavior started occurring after upgrading to TypeScript 4.8. Previously, this was not an issue with TS 3.7.

import { Anything } from 'my-package-from-above';

// This causes the files in "bin" above to run
const anything = new Anything();
console.log('Done');

Is this a new behavior in TS4? Or could it be related to the package.json configuration?

Edit The class that is being executed upon import looks like this. Is there something in its structure that could be causing this issue?

#!/usr/bin/env node
import { DeployUtility } from './deploy-utility';
const deployUtility = new DeployUtility();
deployUtility.configureDomainMapping().then(data => {
  return data;
});

The issue is that the contents of this class execute whenever the package is imported, even if the importing code is not directly using this specific class.

Answer №1

After identifying the root cause, it was discovered that the scripts were mistakenly included in the index.ts file. As a result, the *.ts files responsible for generating the *.js files were unnecessarily triggered whenever the package was referenced from the "bin" section of the package.json in the index.ts exports.

To resolve this issue, it was necessary to eliminate the exports that resembled the configureDomainMapping example mentioned earlier.

To fix the problem, the following code snippet should be removed (as well as any similar instances) from index.ts

export * from './configure-domain';

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

I searched through the interface folder in the component.ts file but couldn't locate my typescript property

My coding dilemma goes something like this:- Within the interface folder, I have created a book.ts file with properties. However, my book.component.ts is not recognizing the book.ts property. It seems to work fine when I import the interface (book.ts) dir ...

Tips for resolving a typescript Redux connect error related to ActionCreatorsMapObject

Encountering typescript error: ERROR in [at-loader] ./src/app/components/profile/userProfile/_userMain.tsx:130:37 TS2345: Argument of type 'typeof "/usr/src/app/src/js/src/app/redux/actions/sessionActions"' is not assignable to parameter of ...

Testing an npm module with peerDependencies: A step-by-step guide

I am trying to wrap my head around peerDependencies, and I have been searching various resources for guidance on how to test an npm module that includes peerDependencies in its package.json: Understanding Peer Dependencies Deciphering the npm dependency ...

Full compatibility with Angular 2 is now available on Visual Studio 2015 with the added support of

I have some inquiries regarding Visual Studio 2015, Resharper 10, and Angular 2. Is there any syntax highlighting support for HTML markup in TypeScript files in Visual Studio 2015 or Resharper 10? For example, when using a multiline string in a componen ...

When using Multer, the individual field name cannot be obtained when logging to the console

To enable file uploads in my Node.js app, I incorporated the multer module within an Express app. In my multiple-file upload setup, I am looking to access the fieldname property for each item individually. By using console.log(req.files);, I can view all ...

"In Nextjs, it is not possible to utilize the alert function or assign variables directly to the window object

Are you attempting to quickly sanity test your application using the window object's alert function, only to encounter errors like "Error: alert is not defined"? import Image from "next/image"; alert('bruh'); export default function Home() ...

I'm struggling to make basic CSS work in my Next.js 13 project. I'm a beginner, can someone please help?

I am facing issues with the default CSS in my latest project. I have a file called page.modules.css and I am using classname = styles.style page.tsx import styles from'./page.module.css' export default function Home() { return ( <div cl ...

Can anyone explain why the Splice function is removing the element at index 1 instead of index 0 as I specified?

selectedItems= [5,47] if(this.selectedItems.length > 1) { this.selectedItems= this.selectedItems.splice(0,1); } I am attempting to remove the element at index 0 which is 5 but unexpectedly it deletes the element at index ...

Guide on how to include jquery-ui css file in Vue3

I am new to TypeScript and VueJs. Recently, I decided to incorporate jquery-ui into my project using Vue3, TypeScript, and Electron. After some trial and error, I was able to successfully set up the environment. However, when I attempted to use the jquery ...

What is the process for setting up git hooks when running the "npm install" command?

I'm looking to set up an automatic pre-commit git hook for code linting whenever someone installs my-package. I attempted to include a postinstall script: "scripts": { "postinstall": "./scripts/install-git-hooks" } This method works well. When a ...

The improved approach to implementing guards in Angular

I am currently looking for the most effective way to utilize Angular "guards" to determine if a user is logged in. Currently, I am checking if the token is stored. However, I am wondering if it would be better to create an endpoint in my API that can verif ...

Looking to conceal the input div without compromising the functionality in Angular 14

Here is the provided HTML code for scanning a barcoder and assigning it to the barCodeNumber variable. The onChange() function will be called once the barcode is scanned. Question: How can I hide the div on the UI while still allowing the function to work ...

Issue with Angular 2 Observable not triggering the complete function

I've been experimenting with the hero app tutorial for Angular 2 and currently have this Component set up: import { Component, OnInit } from '@angular/core' import { Subject } from 'rxjs/Subject'; import { Hero } from "./hero"; im ...

Dealing with Errors in Npm Mail Notifier

I'm facing a bit of a dilemma here. I've implemented an npm mail-notifier module that seems to be functioning properly. However, I'm unsure about how to handle failed logins. Here's a snippet of my code: var imap = { user: ...

Difficulties encountered in the deployment of a Vaadin application for live usage

Currently tackling a challenge with Java 8 and Vaadin 22. As I try to deploy my starter app to production using the command mvnw clean package -Pproduction, I encounter multiple issues during the frontend build process. The error message "Cannot find modul ...

Leveraging split function within a node.js module

const telegram = require('telegram-bot-api'); const api = new telegram({ token: 'YOUR_TOKEN_HERE', updates: { enabled: true, get_interval: 1000 } }); api.on('message', function(message){ const chat_id = message.chat.i ...

The directory "Users/node_modules/crypto-browserify" is not a valid directory in react

After discovering the package crypto-browserify on GitHub, I attempted to integrate it into my React Native project. Following the command npm install --save crypto-browserify, I then imported it using var crypto = require('crypto-browserify');. ...

Utilizing the namespace importation method correctly

How can I efficiently utilize classes from a namespace that may be the same or different from the current file's namespace? I currently have the following two files. TypeA.ts: export namespace Game { @ccclass export class TypeA extends cc.Component ...

Traversing through JSON objects in Angular 2

I am currently facing an issue while trying to iterate through a JSON object. Below is the sample JSON data: floors.ts this.floors= [ { floorName: "floor 1", result: [ { resFloor: "1", ...

What are the advantages of integrating npm with Laravel?

I often notice npm being used with Laravel boilerplate projects, which is the package manager for Node, but I can't quite understand why. Take for instance this laravel-5-boilerplate which includes the step to run npm install during the installation ...