What is the best way to transfer my static files to the desired output directory in a TypeScript Express application?

I am attempting to transfer my static files from the input directory to the output directory using Express. I found guidance in this tutorial, which utilized shell.js for copying static files.

The code responsible for this operation is located in CopyAssets.ts:

import * as shell from "shelljs";

shell.cp( "-R", "src/views", "dist/" );

In addition, the script that triggers the command can be found in my package.json:

 "scripts": {
    "clean": "rimraf dist/*",
    "copy-assets": "ts-node tools/CopyAssets",
    "lint": "tslint -c tslint.json -p tsconfig.json --fix",
    "tsc": "tsc",
    "build": "npm-run-all clean lint tsc copy-assets",
    "dev:start": "npm-run-all build",
    "dev": "nodemon --watch src -e ts,hbs --exec npm run ",
    "start": "node .",
    "test": "echo \"Error: no test specified\" && exit 1"
  }

Despite setting up an npm run dev command, my scripts are not being copied over. Any ideas why this might be happening?

Answer №1

While working in development mode, I recommend using ts-node. It allows you to run TypeScript code without the need for transpiling.

I personally utilize npx to execute locally installed dependencies.

Take a look at my package.json scripts below:

"scripts": {
    "clean": "npx rimraf ./build/ && npx rimraf ./log/",
    "prebuild": "npm run clean",
    "build": "npx tsc",
    "postbuild": "npx ts-node ./tools/build",
    "prod": "node ./app.js",
    "dev": "npx nodemon",
    "test": "npx jest --findRelatedTests"
}

To generate a build folder containing all your compiled .ts files and assets, simply type:

npm run build

!!! Utilizing the npm script flavor:

Before initiating the build process, npm executes prebuild, which carries out a cleanse operation.

After completing the build, npm triggers postbuild, responsible for copying all assets and files into the ./build directory.


Here is the snippet from build.ts that handles the copying of assets and files:

import shell from 'shelljs';

const buildFolder = './build/';

const files = new Set(['.env', 'LICENSE', 'README.md', 'package.json', 'package-lock.json']);
const folders = new Set(['./src/views', './src/public']);

// Copy Files
files.forEach((file) => {
  shell.cp('-R', file, buildFolder);
});

// Copy Folders
folders.forEach((folder) => {
  shell.cp('-R', folder, buildFolder);
});

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

The function app.address is not supported in Node.js Express ES6

Greetings to everyone online during this festive season... I have been working on converting some basic code from ES5 to ES6 .. An example of a Node.js Express server written in ES5 JavaScript: ES5 /* eslint-disable no-console */ var express = require( ...

Cross-origin resource sharing problems: The 'Access-Control-Allow-Origin' header should not include more than one value

My goal is to enable my server to allow two distinct domains to access data without encountering a CORS issue. To achieve this, I implemented the following code snippet in node.js: app.use(function(req, res, next){ res.header("Access-Control-Allow-Or ...

Trying to determine the specific key of an object based on its value in TypeScript?

Looking to create a function that can retrieve the key for a given value. type Items<T> = Exclude<{ [P in keyof T]: [P, T[P]]; }[keyof T], undefined>[]; export const getKeyName = <T extends Record<PropertyKey, unknown>, V>( col ...

Upon transitioning from typescript to javascript

I attempted to clarify my confusion about TypeScript, but I'm still struggling to explain it well. From my understanding, TypeScript is a strict syntactical superset of JavaScript that enhances our code by allowing us to use different types to define ...

Receiving encoded characters in the response

URL: I have encountered an issue where I am trying to retrieve the PDF file from the URL above using code. In tools like Postman or Insomnia, I am able to see the output as expected in PDF format. However, when I attempt it with code, I am receiving rando ...

Ways to speed up the initial loading time in Angular 7 while utilizing custom font files

Storing the local font file in the assets/fonts folder, I have utilized 3 different types of fonts (lato, raleway, glyphicons-regular). https://i.stack.imgur.com/1jsJq.png Within my index.html under the "head" tag, I have included the following: <lin ...

Error: Authorization requires both data and salt arguments

As a novice in NodeJS, I attempted to create an authentication form using NodeJS + express. The issue I am facing is regarding password validation - specifically, when "confirmpassword" does not match "password", it should return nothing. Despite my effo ...

Navigate to the appropriate directory specified in the package.json file

Here’s a breakdown of my project structure: Project client server I am currently facing an issue while trying to start both my Node Express Server and React project at the same time. The express server starts successfully, but I am unable to navigat ...

Understanding Multiple Type Scenarios in React with Typescript

Code Demonstration: type PropsType = {top: number} | {bottom: number} // The function that moves something in one direction by a specific distance. function move(props: PropsType) { ... } Expected Usage: move({top: 100}) or move({bottom: 100}) Avoid us ...

The Google Books API has reached its limit for requests

Encountering a rate limit exceeded error from the Google Books API while using this demo: To reproduce, open the developer console in Chrome and perform some searches. The rate limit errors will be displayed in the console. [],"lazyUpdate":null},"status" ...

Setting up SSL/TLS certificates with Axios and Nest JS

I have a Nest JS application set up to send data from a local service to an online service. However, the requests are not working because we do not have an SSL certificate at the moment. Can anyone provide guidance on configuring Axios in Nest JS to accept ...

The Express request parameter ID throws an error due to the index expression not being of type 'number', causing the element to implicitly have an 'any' type

Is there a way to assign a type to an ID request parameter? It appears that the types of Express treat request params as any. This is the code snippet where I am trying to access the ID from the request: const repository: Repository = { ...reposit ...

"Exploring the process of extracting data from a JSON URL using NextJS, TypeScript, and presenting the

After tirelessly searching for a solution, I found myself unable to reach a concrete conclusion. I was able to import { useRouter } from next/router and successfully parse a local JSON file, but my goal is to parse a JSON object from a URL. One example of ...

Tips for extracting data from an Angular object using the *ngFor directive

https://i.stack.imgur.com/ai7g1.png The JSON structure displayed in the image above is what I am working with. My goal is to extract the value associated with the key name. This is the approach I have taken so far: <span *ngFor="let outlet of pr ...

I am unable to access the environment variables on my ECS task

I currently have this code implemented in my webpack.config.js file const environment = process.env.ENVIRONMENT; const uridb = `mongodb+srv://admin:admin@development-db.${process.env.URI}.mongodb.net/development?retryWrites=true&w=majority` Looking in ...

Creating a sidebar in Jupyter Lab for enhanced development features

Hi there! Recently, I've been working on putting together a custom sidebar. During my research, I stumbled upon the code snippet below which supposedly helps in creating a simple sidebar. Unfortunately, I am facing some issues with it and would greatl ...

Configuration of injected services in IONIC 2

I am curious about how the services from injected work in IONIC 2. Specifically, my question is regarding the number of instances that exist when one service is used in two or more controllers. Previously, I asked a colleague who mentioned that IONIC 2 op ...

The middleware in node.js passport is preventing successful logouts from occurring

Currently, I am developing a web application using nodejs along with express, mongoose, and passport npm modules. Initially, the login and logout features were functioning properly. However, I recently introduced a new functionality to differentiate betwe ...

Tips for developing a strongly-typed generic function that works seamlessly with redux slices and their corresponding actions

Currently, I am working with @reduxjs/toolkit and aiming to develop a function that can easily create a slice with default reducers. Although my current implementation is functional, it lacks strong typing. Is there a way to design a function in such a man ...

Using Angular: Binding Angular variables to HTML for display

I have a component with a ts file that contains HTML content as a variable. Let's say para1= <a href="www.google.com">sitename</a> more content I want to bind this paragraph in HTML as if it were actual HTML tags. sitename What is the ...