Why does "require" compile but "import" does not? - node.js

I am new to TypeScript/JavaScript and Node.js and writing a simple script to run in command line.
I installed archiver dependency and added import archiver from 'archiver'; to my script.
When I compile the script with tsp -p . the import does not compile:
error TS7016: Could not find a declaration file for module 'archiver'. <my script path> implicitly has an 'any' type.
Try `npm install #types/archiver` if it exists or add a new declaration (.d.ts) file containing `declare module 'archiver';`
6 import archiver from 'archiver';
~~~~~~~~~~
However const archiver = require('archiver'); does compile. Now I wonder what's wrong with the import.

You installed the npm packages without the types, meaning the pure JS package. You would need to run npm install #types/archiver, provided this package supports Typescript. The standard for JavaScript (ES6/2015) suggests to use import and not the require.

Related

Convert old CJS module into ESM and import into TS files

I am wanting to convert an old module, https://github.com/capaj/object-resolve-path, into ESM so I can use it via an import statement, in order to move all my NodeJS Lambda functions to ESM.
I have forked the repo, and changed the 2 main .js files to .mjs, updated the exports, as well as update the main property in package.json to point to the object-resolve-path.mjs file.
In my NodeJS Lambda function, I have then installed the fork via NPM from my private repo, which pulls the new code in.
However, when I try to import the package in my code now, using import * as resolvePath from 'object-resolve-path'; I get an error:
Could not find a declaration file for module 'object-resolve-path'.
What am I missing? The module isn't written in TS, so why is it asking for a declaration file?

How to resolve TypeScript declaration file error with observablehq/plot

I am using react and Next.js, I am following this guide https://github.com/observablehq/plot with the goal to render/visualize some data from a heroku postgres DB. I used npm install #observablehq/plot.
import * as Plot from '#observablehq/plot'
This import line is raising a ts7016 error:
Could not find a declaration file for module '#observablehq/plot'. '/Users/alejandrozapien/bexar-dash/node_modules/#observablehq/plot/src/index.js' implicitly has an 'any' type.
Try `npm i --save-dev #types/observablehq__plot` if it exists or add a new declaration (.d.ts) file containing `declare module '#observablehq/plot';
I am new to learning Typescript and creating declaration files. How should I go about resolving this error properly? I read I should create a global.d.ts file but I am unsure of where to place that file or how to structure it

How to require a node package that wants to be a module, without type: module

I need to import a module to my project.
When I do const gifsicle = require('gifsicle'); it tells me:
ReferenceError: require is not defined in ES module scope, you can use import instead
When I try to do import, it says
SyntaxError: Cannot use import statement outside a module
Other threads say to fix this I have to add "type": "module" to my package.json
But doing so gives me this error:
ReferenceError: require is not defined in ES module scope, you can use import instead
How do I load in an ES module package into my current non-module package which uses require?
First of all add "type":"commonjs" to package.json.
Downgrade your package version. If you are using let suppose version 8.0.0 then install older version like npm i package-name#7.1.0.
That's all now enjoy coding...

Why am I getting a "Cannot find module" err when compiling Typescript?

I am just getting started using typescript on the server and I'm pretty much stuck trying to import 3rd party npm modules. Here is what I have declared:
import mongodb = require('mongodb');
import assert = require('assert');
import Q = require('q');
... and I am getting the following errors when compiling:
src/Databse.ts(1,26): error TS2307: Cannot find module 'mongodb'.
src/Databse.ts(2,25): error TS2307: Cannot find module 'assert'.
src/Databse.ts(3,21): error TS2307: Cannot find module 'q'.
What is the correct way to import 3rd party modules?
It doesn't feel the correct way to import 3rd party's library. You can simply use only import
import "library";
OR
import {module} from "library";
Note:- when you are using above syntax then you need to make your that
the module you are importing exist otherwise you will get the error
that it can't find the module you are trying to import.
If you want to use webpack then
require("library");
Or like this
var module = require("library");
Have you installed the typing declarations ?
typings install mongodb --save
typings install dt~assert --save
typings install dt~q --save
Then you will need to reference the typings.
Either using the triple slash-directive
/// <reference path="..." />
More about it here Triple-Slash Directives
Or add the typings index.d.ts file in files array in tsconfig.json
"files": [
"./typings/index.d.ts",
]

Why is typescript failing to import a module?

Typescript is not able to import the js-yaml package. It is actually happening with more packages for me, but this is an easy way to reproduce the problem.
In a new directory, type:
npm install js-yaml
Then in that same directory add the following ts file:
import * as y from 'js-yaml';
console.log(y);
When I compile using this command:
$ tsc --version
message TS6029: Version 1.7.5
$ tsc --module commonjs file.ts
file.ts(2,20): error TS2307: Cannot find module 'js-yaml'.
And if I change import style to commonjs, like so:
declare var require: any; // need to declare require, or else tsc complains
let y = require('js-yaml');
console.log(y);
All is compiled happily. Furthermore, I see that even though tsc had a compile failure, it does output a file. And in this file, there is exactly the same require call as in the version that compiles properly:
var y = require('js-yaml');
console.log(y);
Is this a bug, or am I doing something silly?
Silly of me. With help from the Typescript gitter room, I realized that I was missing the typings file. So, I ran this:
tsd install js-yaml
And then added the typings reference at the top of the ts file, like this:
/// <reference path="./typings/js-yaml/js-yaml.d.ts"/>
import * as y from 'js-yaml';
console.log(y);
And compilation worked.

Resources