Cannot overwrite out dir using declaration - node.js

I'm using Typescript and in tsconfig.json I'm getting this error:
Cannot write file 'c:/michael/Documents/razor/lib/client.d.ts' because it would overwrite input file.
this is my tsconfig.json content:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "Node",
"sourceMap": true,
"outDir": "./lib",
"removeComments": true,
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"declaration": true
},
"exclude": [
"node_modules",
"test"
]
}
as you can see I have enabled declaration: true to export also the d.ts files.
This is my project folders structure:
/lib
/node_modules
/src
/test
.gitignore
.npmignore
razor-1.0.0.tgz
package.json
packages-lock.json
readme.md
tsconfig.json
If I try to run tsc -p I cannot compile, so each time I must delete the folder lib manually which contains the compiled project.
How can I fix this?

TypeScript is picking up files from lib directory when compiling. Hence, that error. Try adding lib to the exclude array.

Related

Cannot find name 'module' in client folder on full stack app

I've been trying to add tailwind to my full stack app where I am playing around with socket.io. However, in setting it up I get this error in tailwind.config.js and postcss.config.js which blocks tailwind from working.
When copying over my tsconfig.json into a repo which is just the client and not server folder this export is recognised, suggesting to me that this is an issue with an automatic import looking in the wrong path when there are multiple folders that contain tsconfig.json files?
client/tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": "client",
"paths": {
"#/components/*": ["client/components/*"],
"#/containers/*": ["client/containers/*"],
"#/pages/*": ["client/pages/*"],
"#/types": ["client/types"]
},
"typeRoots": ["client/node_modules/#types"],
"types": ["node"]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
The full repo - https://github.com/puyanwei/socketio-chat-app
Things I have done to try and resolve this issue;
Deleted node_modules and package.lock/yarn lock and reinstalled the packages with yarn and npm
Changed baseUrl to /client and .
Restarted VS Code
Uninstalled and reinstalled node
Added paths to typeRoots key/value pair in tsconfig.json
Added "types: ["node"] to tsconfig.json
Added #types/node to package.json but it doesn't seem to apply the types
Any help would be appreciated, thanks!

NestJS prevent server reloading in development

I have a route which creates a new folder inside public directory which is a static content served with app.useStaticAssets.
The problem is that even if I added the public directory inside exclude array of both tsconfig.build.json and tsconfig.json, my server still reloads in development when a new folder is deleted or created inside public directory.
I'm missing something ?
UPDATE:
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"noImplicitAny": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
},
"exclude": ["node_modules", "dist", "public"]
}
I mention that public folder is outside of the src folder. They are on the same level.
I was able to reproduce this and found out that this seems to be a common issue -> https://github.com/nestjs/nest/issues/3510
As propsed in the github issue, adding include as a workaround seems to fix the problem:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"noImplicitAny": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
},
"include": ["src"],
"exclude": ["node_modules", "dist", "public"]
}
If you want to completely disable hot reload temporarily, remove the --watch flag from your start:dev script.
Go into package.json
Find your "start:dev" script under "scripts"
Duplicate your "start:dev" script and rename it - I named it start:dev-noreload
Your script should look something like this:
"start:dev-noreload": "npm run prebuild && npm run kill:dev-process && npm run build:docker-postgres && env ENV=DEV nest start"
Run your app using
npm run start:dev-noreload
In order to get hot reload back, just run the way you normally do.
Either using npm start, or npm run start:dev.

Typescript will not exclude folder in the 'excludes' array of tsconfig no matter what (TS 3.7.2)

I've read countless threads and documentation and tried dozens of different approaches to get this to work and it simply will not ignore that testing folder no matter what.
My TS Config:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es2018", "dom"],
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "lib",
"strict": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
"noImplicitAny": true,
"moduleResolution": "node"
},
"include": ["src"],
"exclude": ["**/testing/*", "testing", "**/testing/**/*"]
}
My folder structure:
Turns out excluding folders like that was quite pointless and the whole folder structure that I was seeing with "src" folder and so on was already a sign of things going wrong compared to my tsconfig which should just place the JS result of the files inside "src" straight inside "lib".
My spec files were in SRC and they were depending on assets inside the 'testing' folder. Once I excluded the spec files using:
"exclude": ["src/**/*.spec.ts"]
I didn't need to specify the testing folder to the 'excludes' array at all. Now the result of tsc, outputs this folder/file structure without no other changes to the tsconfig:

TypeScript OutDir contains package.json

I'm trying to build my simple npm package, and all is well. However when I'm trying to build it it goes a bit wrong.
I get the following:
- dist
- package.json
- src
- index.js
- index.d.ts
- ...
But this is not what I expected to get, I'm pretty sure I've done this before and gotten, the following:
- dist
- index.js
- index.d.ts
- ...
This is what I want to end up with, but so far nothing has worked.
My tsconfig.json looks like this:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es6", "dom"],
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"include": ["src"],
"exclude": ["node_modules", "build", "dist"]
}
I haven't been able to find a reason for it to include the package.json or the src path. I'm using typescript#3.6.4.
Are you importing your package.json anywhere in your code? If you are, it's getting copied because of resolveJsonModule.
If you want to read JSON files without them being copied over, you can do a readFile and JSON.parse.

Rollup does not bundle declaration file

I am trying to build a library made with TypeScript. I am using the paths property in tsconfig.json.
My tsconfig.json looks like this:
{
"compilerOptions": {
"module": "es2015",
"moduleResolution": "node",
"target": "es2015",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"experimentalDecorators": true,
"sourceMap": true,
"lib": [
"esnext",
"dom"
],
"strict": true,
"sourceRoot": "./src",
"rootDir": "./src",
"outDir": "build",
"declaration": true,
"declarationDir": "build",
"baseUrl": "./src",
"noUnusedLocals": true,
"paths": {
"modules/*": [
"modules/*"
],
"common/*": [
"common/*"
]
}
},
"exclude": [
"node_modules",
"build"
]
}
And my rollup config looks like this:
import typescript from 'rollup-plugin-typescript2'
export default {
entry: "./src/index.ts",
output: {
file: './build/index.js',
format: 'cjs'
},
plugins: [
typescript({ typescript: require("typescript"), declaration: true, clean: true })
]
}
However, when I build using rollup -c, it creates this in my build folder:
Where the index.js is bundled just fine, but the declaration file here simply imports from the /common folder and that folder basically contains all the other folders from the src directory but only with a single declaration file per folder, furthermore, these files try to import using the paths aliases instead of being built with relative imports, so they do not work.
How do I tell rollup to build a single declaration file instead of this?
rollup-plugin-typescript keeps the folder structure for declaration files. To bundle the declaration too, you should use rollup-plugin-ts as well:
https://github.com/wessberg/rollup-plugin-ts

Resources