I know there is plenty of topic around this question but I read a lot and I didn't found any answer.
I try to use a 3rd party through NPM in my nodeJS project (nest).
But when it compiles, I have the *.default is not a constructor error.
When I go into the source in the node_modules, I see the error.
const web3_1 = require("web3");
[...]
const getClient = async (options) => {
const { url } = options;
return new web3_1.default(url);
};
If I remove the default in here, it's working like a charm, but this is not something I control ...
I put the esModuleInterop to true in my tsconfig.json but that's not working.
Here is my tsconfig file
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": "./",
"incremental": true,
"strictNullChecks": true,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"esModuleInterop": true,
"module": "CommonJS",
"target": "ES2018",
"declaration": true,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "./dist",
"rootDir": "./src",
"skipLibCheck": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
What am I missing?
According to their docs and their source code you shouldn't need to use default. Just
const web3_1 = require("web3");
[...]
const getClient = async (options) => {
const { url } = options;
return new web3_1(url);
};
And you should be good. Looks like they may have their types messed up and you'll need to tell Typescript to ignore it
Related
I use Node.js application and TS 4.8, and I updated package file-type, but now my project compilation fails with error
[1] const _fileType = /#PURE/ _interopRequireWildcard(require("file-type"));
[1] ^
[1]
[1] Error [ERR_REQUIRE_ESM]: require() of ES Module /home/victor/Desktop/alytics/node_modules/file-type/index.js from /home/victor/Desktop/alytics/dist/routes/index.js not supported.
[1] Instead change the require of /home/victor/Desktop/alytics/node_modules/file-type/index.js in /home/victor/Desktop/alytics/dist/routes/index.js to a dynamic import() which is available in all CommonJS modules.
There is my swc config:
`
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": true,
"dynamicImport": true
},
"target": "es2020",
"paths": {
"#routes/*": ["./src/routes/*"],
"#middlewares/*": ["./src/middlewares/*"]
},
"baseUrl": "."
},
"module": {
"type": "commonjs"
}
}
`
And there is my tsconfig:
`
{
"compilerOptions": {
"target": "es2020",
"module": "es2020",
"allowJs": true,
"removeComments": true,
"resolveJsonModule": true,
"typeRoots": ["./node_modules/#types"],
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"outDir": "dist",
"strict": true,
"lib": ["es2020"],
"baseUrl": ".",
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": "Node",
"skipLibCheck": true,
"paths": {
"#routes/*": ["./src/routes/*"],
"#middlewares/*": ["./src/middlewares/*"]
}
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
`
Where I use file-type:
`
import KoaRouter from "#koa/router";
import { Context } from "koa";
import { logger } from "#middlewares/index.js";
import * as FileType from "file-type";
const router = new KoaRouter();
router.get("/", logger, (ctx: Context): void => {
ctx.body = { message: JSON.stringify(FileType) };
});
export default router;
`
As I understand, file-type package using only ESM import, but , after compilation this import convert to require(). I read about this issue in their repository, but this topic was closed as TS now support ESM.
How can I configure my ts.config to exclude this module to covert to require ??
Is there any way to solve this issue only using TS config?
I find the only solution - convert from commonjs - to ESM (change type: module, and add that to my package.json), but I don't want to update all my import adding .js at the end and I dont want to convert all my imports to ESM.
I'm using tsyringe for dependency injection and trying to run unit tests.
The class is in ts and the test file is in js. When I try to run my tests by executing
TS_NODE_PROJECT=\"tsconfig.testing.json\" mocha -r ts-node/register src/**/*.test.js
I get the following compilation error:
repo.ts:27:14 - error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.
Here is my code.
// repo.ts
#injectable()
export class Repo {
testAdd = (a, b) => {
return a + b;
};
}
// repo.test.js
const { Repo } = require("../repo");
const expect = require("chai").expect;
describe("testing the add function", () => {
it("addition worked correctly", (done) => {
const r = new Repo();
const res = r.testAdd(4, 5);
expect(res).to.equal(9);
done();
});
});
// tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"noImplicitReturns": false,
"noUnusedLocals": false,
"outDir": "lib",
"sourceMap": true,
"strict": false,
"target": "es2017"
},
"compileOnSave": true,
"include": ["src"]
}
// tsconfig.testing.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es6"
},
"include": ["**/*.spec.ts"]
}
If I get rid of the injectable() decorator then the tests work.
If I change the test ext from js to ts, then it works.
I tried creating a jsconfig.json and adding in
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
but it didn't help.
What am I doing wrong?
Update, I think the issue is that I needed to add the
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
in the tsconfig.testing.json file as well. So far looks to be working with the .js testing files.
I write a program with typescript in node js (not an API or web app) just a console app. I have configured the typescript configuration. but when I make a File Object it give me error
UnhandledPromiseRejectionWarning: ReferenceError: File is not defined
My Code
// .......
var file = new File(["asdasdsa"], 'metadata.json');
console.log(file.name);
// ......
ts config json content
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"strict": true,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true /* Enable strict null checks. */,
"strictFunctionTypes": true /* Enable strict checking of function types. */,
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
"importHelpers": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"sourceMap": true,
"outDir": "./dist/tsc/",
"types": [
"node",
"jest"
],
"lib": [
"ES6",
"DOM"
]
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.test.ts"
]
}
Anything I missed on typescript configuration.
I want to make an object of File from typescript
In nodejs, you don't have File type, but you can also use fs to create one
Something like this:
var fs = require('fs');
fs.writeFile('metadata.json', 'asdasdsa', function (err) {
if (err) throw err;
});
This will create a file in your root folder.
And you can read it and send like so:
fs.readFile("./metadata.json", (err, data) => {
if (err) res.status(500).send(err);
res
.contentType("application/pdf")
.send(
`data:application/pdf;base64,${new Buffer.from(data).toString("base64")}`
);
});
Is it possible to use the Utility types, such as Record when using JSDoc + tsserver in plain JavaScript?
(I'm trying to create a setup that gives me as many of the gains of TypeScript as I can stand, without the transpiling)
Sample Code
I happen to actually need a map, which I understand is defined in TypeScript as "Record":
let http = require("http");
/**#type {Record<string, Person>} */
let people = {
"1234": { name: "Bob" },
"abcd": { name: "Jane" }
};
http.createServer(function (req, res) {
res.end(JSON.stringify(people));
}).listen(process.env.PORT || 3000);
Error
However, when I try to use it I get this error:
[tsserver] Cannot find name 'Record'. [E]
Config
Version:
tsc --version:
Version 4.3.5
tsconfig.json:
{
"compilerOptions": {
"incremental": true,
"target": "ESNEXT",
"module": "commonjs",
"lib": ["DOM"],
"allowJs": true,
"checkJs": true,
"tsBuildInfoFile": "./cache",
"noEmit": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"moduleResolution": "node",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["**/*.js"],
"exclude": ["node_modules"]
}
For reference, this is how I set ale to use tsserver in my .vimrc:
let g:ale_linters = {'javascript': ['tsserver', 'jshint']}
Record type is defined in lib.es5.d.ts so you need to add at least es5 into your lib array.
If you don't set lib, it defaults to target, but if you set it, basic es libs must be manually included (commonly used for environments with polyfills, but don't support new language features).
I have an application developed in node and typescript, so to make this application more modular (something like libs in asp.net), I've been created two local packages to be more reusable and add them in my application as local package (npm i 'path-my-module').
But now I'd problems when I build my solution to make a deploy in test because this modules hasn't been included in the build.
I'm new in node and I didn't find much thing about how can I work in this case I would like to know how can I do in this case?
This approach it's right?
How can I use local dependencies and include them in my build to be deployed in test env?
More information:
My Package.json is more or less like this:
"dependencies": {
some imports ...
"my-local-package-a": "file:../LocalPackageA",
"my-local-package-b": "file:../LocalPackageB",
.... more imports,
},
I'm using Gulp.js to automate the pipeline.
My gulp file:
var gulp = require("gulp");
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");
var OUT_DIR = "dist";
var IN_DIR = "../MyAlexaApp";
// compile typescript
gulp.task("compile", function () {
return tsProject.src()
.pipe(tsProject())
.js.pipe(gulp.dest(OUT_DIR));
});
// copy json files (e.g. localization json)
gulp.task("json", function () {
return gulp.src(IN_DIR + "/**/*.json").pipe(gulp.dest(OUT_DIR));
});
gulp.task("default", gulp.parallel(["compile", "json"]));
My ts config:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": ["es6"],
"moduleResolution": "node",
"rootDir": "../DealerSearchSkill",
"outDir": "dist",
"sourceMap": true,
"allowJs": false,
"noImplicitAny": true,
"noUnusedLocals": true,
"noImplicitThis": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"resolveJsonModule": true
},
"exclude": [
".ask",
"coverage",
"skill-package",
"infrasctructure",
"/**/node_modules",
"lambda/local/",
"__tests__",
"**/__mocks__"
]
}