Ignore all files in a directory except for specific file types in the .npmignore file

Context

In my typescript project being published to npm, all source files are contained in the "src" folder and compiled to the "dist" folder. The assets like HTML templates, CSS files, and others are also within the "src" folder and get copied over to the "dist" folder during build. Additionally, source map files are generated in the "dist" folder.

For publishing to npm, I only want to include the "dist" folder while ensuring that the source maps in it remain functional.

Objective

When publishing to npm, I aim to package and distribute the entire "dist" folder but avoid including any non-ts files from the "src" folder.

The necessity for the .ts files is due to the fact that the sourcemaps within the dist folder reference these source files. For effective debugging, having access to the original sources is crucial for visualization in the browser.

Therefore, the goal is to exclude everything from "src" except for the .ts files within it.

Attempts Made

I have experimented with various configurations based on research to achieve this objective, as outlined below:

# standard ignores

node_modules/
karma.conf.js
spec/

# ignore src

src/

# override above and include ts files

!src/**/*.ts
!src/**/*.d.ts

Despite using the above approach, when packaging, the resulting npm package still includes the entire src folder, encompassing html, css, and image files.

To cater to the possibility of a ts file directly under the src folder, I even tried:

!src/actualFile.ts

However, the outcome remained unchanged.

Answer №1

Solution:

To update your ignore pattern for the src directory, simply add the wildcards **/* as shown below:

# Ignore src directory

src/**/*

Additional note:

You can actually remove the last negation pattern (!src/**/*.d.ts) since it is redundant with the previous negation pattern (!src/**/*.ts). This ensures that all *.d.ts files in the src directory are included when using only !src/**/*.ts.


Revised .npmignore file:

Considering the provided example, the revised .npmignore file should look like this:

# Standard ignores

node_modules/
karma.conf.js
spec/

# Ignore src directory

src/**/*

# Include ts files along with other files

!src/**/*.ts

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

What is the best way to interact with Redis without using any external modules?

I am curious about the communication process between the node redis wrapper and the RESP (REdis Serialization Protocol) database. Here is a simple example: const redis = function(uri) { this.client = '' // How do we establish a connection wit ...

Automating the execution of `npm run dev` for a React Vite application running within a Docker environment

I currently have a setup where I am running a Vite React app with a Flask API inside Docker using WSL 2 and docker-compose. However, I find myself manually executing certain commands in order to access my React app in the browser: docker exec -it vite_dock ...

The error message appeared as "ENOTDIR npm install"

I'm encountering an issue while attempting to install discord.js on my Ubuntu server using the following command: npm install discord.js The error message I received is as follows: npm ERR! Linux 4.15.0 npm ERR! argv "/usr/bin/node" "/ ...

Is there any method to prevent the default icon from showing up and disable the one-click functionality?

package.json: { "name": "password-generator-unique", "productName": "Unique Password Generator", "version": "1.0.0", "description": "Custom password generation desktop tool& ...

What steps should I take to distribute files for an npm package publication?

I am looking to release an npm package that includes both my source files and distribution files. Within my GitHub repository, I have a src folder containing JavaScript source code. The build process generates a dist folder which holds the distribution fil ...

Having trouble with adding npm packages to my repository in AWS CodeArtifact

My main goal in utilizing CodeArtifact for my project was to effectively manage third-party npm and pypi dependencies. However, I encountered unexpected issues while attempting to work with npm. To address this, I set up a domain and a primary repository ...

The electron program is unable to locate the package.json module

I am new to electron and attempting to run an express app for the first time. However, I encountered this error: Need assistance updating code Error: Cannot find module 'C:\package.json' at Module._resolveFilename (module.js:440:15) ...

How can I ensure that every dependency in my package.json is updated to the newest version using Yarn?

My current react app is using deprecated dependencies and in order to get it functional, I need to update these dependencies to more recent (yet stable) versions. According to a discussion on Stack Overflow, for updating dependencies in package.json to th ...

The error `npm run server` is not able to recognize the command '.' as an internal or external command

While working on my project from github https://github.com/angular-university/reactive-angular-course, I encountered an issue. Even though I have all the latest dependencies and am running on Windows, I am facing this problem. Interestingly, it works fin ...

installing packages globally in npm with elevated permissions and disregarding security precautions

Attempting to install an npm module by running the command: sudo npm install -g now However, during this process, a warning is encountered: Warning! It is advised to reinstall Now CLI with the option --unsafe-perm. For instance: npm i -g --unsafe ...

Investigating potential causes for the crash of the Next.js Node Server: "npm ERR! code ELIFECYCLE errno 3221225477"

I recently encountered an error on node version 14.16.0, and it happened twice already. The first instance was when I created a react component using the @mui/lab/DateTimePicker. I didn't need to install it since @mui/lab was already present in the pr ...

Converting PPT files to PDF or PNG using Node.js

Are there any options available for converting ppt files to pdf or png without relying on specific operating system dependencies? I need to convert a ppt file to png, but all the current packages I have tried require tools like LibreOffice or ImageMagick ...

Performing a hands-free npm installation with --legacy-peer-deps activated, tailored specifically for an

Let's imagine a scenario where I have a package.json structured like this: { "name": "my-app", "version": "0.1.0", "dependencies": { "@aws-sdk/client-s3": "^3.21.0", & ...

When the system is rebooted, the terminal fails to acknowledge npm and node commands

After installing node and npm using nvm, a message appeared at the end of the installation process: Close and reopen your terminal to start using nvm or run the following to use it now: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && &bso ...

Tips for combining Angular 2 with a current J2EE Spring project and deploying them on the same PORT

I currently have a project with Spring on the back-end and AngularJS 1 on the front-end. When I start the Spring server, it only opens one port for me: 8080 which allows me to access REST APIs and the AngularJS front-end components. https://i.stack.imgur. ...

The attempt to access http://registry.npmjs.org/check was unsuccessful due to a connection error (ECONNREFUSED)

Upon executing the command "npm install -g check" in the cmd prompt, I encountered the following error: npm ERR! code ECONNREFUSED npm ERR! errno ECONNREFUSED npm ERR! FetchError: request to http://registry.npmjs.org/check failed, reason: connect ECONNREF ...

Learn how to synchronize global packages across multiple computers using npm

After installing several npm global packages on my work computer, I am now looking to synchronize these packages with another device. In a typical project, we utilize a package.json file to keep track of package details, making it easy to install all the ...

Establishing the exterior façade prior to generating the Docker image

I am currently new to Docker and in the process of dockerizing some applications. The project structure consists of: -PlayProject -------app ----------controllers ----------models ----------views -------ci -------conf -------project -------public ---- ...

When using npm nodemon and npm colors together, unfortunately the colors are not displaying properly alongside nodemon

Can anyone provide guidance on how to use 'nodemon' to display console colors using the npm 'colors' package? Is this even feasible? I just learned today about the 'colors' package, which allows me to output colorful strings ...

How to dynamically generate data with exceljs

Currently, I am attempting to download JSON data into an Excel spreadsheet. One of my requirements is to insert an image in cell a1 and then bold the headers. The code snippet below was sourced from Google; however, I need to populate the data dynamically ...