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.
Related
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!
I have a project with the following ts config. However when I run the nodejs server and use zeromq, I get Typescript errors in node_modules\zeromq\lib\index.js where export.Context (line 755) doesn't exists and zmqVersion() (line 19) is not a function. I do have ts set to ignore nodejs modules.
When I look at the ts files npx tsc --listFiles zeromq and other node_mods are not listed. So I am not sure why I am getting ts errors in zeromq. It's the only module that I am having issues with.
I do have #types/zeromq installed, but I still see these errors in the js file.
Reproducing:
tsconfig file:
{
"compilerOptions": {
"outDir": "./distTemp/",
"sourceMap": true,
"noImplicitAny": false,
"module": "commonjs",
"target": "es6",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"suppressImplicitAnyIndexErrors": true,
"checkJs": false, // <-I added this to try
"allowJs": true, // <-I added this to try
"skipLibCheck": true, // <-I added this to try
"skipDefaultLibCheck": true, // <-I added this to try
"types": ["zeromq"] // <-I added this to try
},
"ts-node": {
"esm": true
},
"lib": ["esnext"],
"paths": [
"global.d.ts"
],
"include": ["./src", "./server.ts"],
"exclude": ["./node_modules", "./build", "./dist", "./src/**/*.js", "./src/**/*.jsx"]
}
"I added this to try" are the lines I added to try and fix this issue. I run the code with
node -r ts-node/register server.ts --development
This has been really frustrating. I like zeromq and have used it before in different languages - I am really annoyed at the ts functionality here. After all this is a JS file.
Also this wasn't always popping up. I did have this running at one point. But to my knowledge no packages were updated, so I am not sure what change caused this.
I am writing a TypeScript library. I have gotten to the point where I would like to test said library. Because it's a very visual thing, I chose to go with storybook so I can show off the different functionalities of my library.
My package has an index.ts with the following in it:
export { Container } from "./Container";
My folder structure looks like this:
library/
dist/
src/
index.ts
Container.ts
package.json
storybook/
stories/
package.json
This is the package.json of my library:
{
"name": "#wesp/customcontainer",
"main": "dist/index.js",
"files": [
"dist"
],
}
This is the dependencies for the package.json of the storybook folder:
"dependencies": {
"#wesptest/customcontainer": "file: ../",
},
Now when I try to use the custom library in for example storybook/stories/test.stories.ts:
import {Container} from "#wesp/customcontainer";
but then the story will throw this error:
_wesp_customcontainer__WEBPACK_IMPORTED_MODULE_1__.Container is undefined
What do I have to change so I can successfully import this class?
thanks.
-- edit --
My tsconfig.json:
{
"compilerOptions": {
"target": "es2019",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"jsx": "react",
"esModuleInterop": true,
"outDir": "./dist",
"declaration": true,
},
"include": ["./src"]
}
main of your lib is dist/index.js.
Storybook file will try to import from dist dir, so first check if js file exists in that dir.
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.
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: