How to use a git+https dependency correctly - node.js

I am trying to use a git+https dependency (on github) to make a typescript library. I pared it down to a single file as an example and it is still failing. Using a file dependency works perfectly. Switching to a git+https dependency causes me to get an error:
export const Greeter = (name: string) => `Hello ${name}`;
^^^^^^
SyntaxError: Unexpected token 'export'
The two dependencies, nothing else changes in either project:
"#dgmyers/greeter": "git+https://git#github.com:dgmyers/typescript-greeter-library.git#v0.1.1",
"#dgmyers/greeter": "file:../typescript-greeter-library",
Files:
typescript-greeter-library/src/hello.ts
export const Greeter = (name: string) => `Hello ${name}`;
typescript-greeter-library/dist/hello.d.ts (tsc generated)
export declare const Greeter: (name: string) => string;
typescript-greeter-library/package.json
{
"name": "typescript-greeter-library",
"version": "1.0.0",
"description": "",
"main": "src/hello.ts",
"types": "dist/hello.d.ts",
"scripts": {
"clean": "rm -rf dist/*",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dgmyers/typescript-greeter-library.git"
},
"author": "",
"license": "UNLICENSED",
"bugs": {
"url": "https://github.com/dgmyers/typescript-greeter-library/issues"
},
"homepage": "https://github.com/dgmyers/typescript-greeter-library#readme"
}
typescript-greeter-library/tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"lib": ["es2020"],
"target": "es5",
"declaration": true,
"moduleResolution": "node",
"emitDeclarationOnly": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"baseUrl": "./node_modules",
"outDir": "./dist",
"module": "commonjs",
"typeRoots": [
"./types"
]
}
}
consumer/package.json
{
"name": "consumer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"run": "ts-node src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "UNLICENCED",
"dependencies": {
"#dgmyers/greeter": "git+https://git#github.com:dgmyers/typescript-greeter-library.git#v0.1.2",
"#types/node": "^16.3.1",
"ts-node": "^10.1.0",
"typescript": "^4.3.5"
}
}
consumer/index.ts
import { Greeter } from '#dgmyers/greeter'
console.log(`${Greeter('dgmyers')}`);

When installing via a source repo, ie. via the github: or git+https: protocol, npm will run the prepare lifecycle script and pack it like publishing it.
Generally, even your source repo is not in pure js (e.g. written in typescript) it's still fine because the script for compiling the source (e.g. tsc) is usually configured in prepare and npm will compile it when you install the repo.
However, there're still 2 scenarios it will fail.
missing the compilation script in prepare
missing the .npmignore file in the source repo so npm fallbacks to .gitignore which generally excludes the folder containing the compiled code
Mayne you've one of them.

Related

Accidentally running TypeScript code directly with `node` without `ts-node`, why does it work?

I have set up a very simple node.js project with TypeScript. My package.json, from which you can see what packages I have installed (there is no ts-node), looks like this:
{
"name": "mydemo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"lint": "eslint . --fix",
"test": "echo \"Error: no test specified\" && exit 1",
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#types/node-fetch": "^2.6.1",
"#typescript-eslint/eslint-plugin": "^5.23.0",
"#typescript-eslint/parser": "^5.23.0",
"eslint": "^8.15.0",
"node-fetch": "^3.2.4",
"ts-node": "^10.7.0",
"typescript": "^4.6.4"
}
}
Project structure:
The src/main.ts:
const sayHi = () => {
console.log('HI!12345');
}
sayHi();
My tsconfig.json:
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"outDir": "dist",
"sourceMap":true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
With the above setup, I can run node src/main.ts on terminal. Whenever I update code in main.ts I directly run node src/main.ts also shows the result with latest code.
I could have installed ts-node, I tried and it works as well. But I wonder why without ts-node my setup works just fine. I mean I can already run TypeScript code by node command without the need to compile to and run JS.
Could someone please explain to me? Seems ts-node is not needed at all.
TypeScript syntax is a superset of JavaScript. Your code does not use any TypeScript specific syntax (like type declarations or type annotations), so it's also a valid JS file, which plain node can execute. If you add some type annotations, you'll get an error with node:
const sayHi = (n: number) => {
console.log('HI!12345');
}
sayHi(123);

Top-level await and Import in Typescript

I'm studying Typescript and running into a problem. I want to use the import and top-level await but currently, I can only use one at a time.
Here is my config in tsconfig.json which allows me to use import
"target": "ESNext",
"module": "ESNext"
And this one allows me to use top-level await:
"module":"commonjs"
I added
"type":"module"
to my package.json
Is there any way that I can take advantage of both features? Many thanks.
I'm using Nodejs 16 and Typescript 4.5.5
Yes, you can use both. Here's info to help you reproduce a successful test:
Node.js (LTS) version:
$ node --version
v16.14.0
./package.json:
{
"name": "so-71099311",
"version": "0.1.0",
"private": true,
"description": "",
"type": "module",
"scripts": {
"compile": "tsc",
"start": "npm run compile && node dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"#types/node": "^17.0.17",
"typescript": "^4.5.5"
}
}
./tsconfig.json:
{
"compilerOptions": {
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"isolatedModules": true,
"lib": [
"esnext"
],
"module": "esnext",
"moduleResolution": "node",
"noUncheckedIndexedAccess": true,
"outDir": "dist",
"strict": true,
"target": "esnext",
"useUnknownInCatchVariables": true
},
"include": [
"./src/**/*"
]
}
./src/module.ts:
export function greet (name = 'world'): void {
console.log(`Hello ${name}`);
}
./src/main.ts:
import {greet} from './module.js';
const name = await Promise.resolve('Nguyen');
greet(name);
In your console:
$ cd /path/to/the/directory/where/you/created/these/files
$ npm install
$ npm run start
> so-71099311#0.1.0 start
> npm run compile && node dist/main.js
> so-71099311#0.1.0 compile
> tsc
Hello Nguyen

Build npm package with desired file structure

I have created a simple typescript project to export ENUMS, I successfully compile the project. tsconfig.json and package.json below.
I use the npm command to pack it and npm publish it also. I install the same package to a new project and I import the same with the code
import Numbers = require('#hk18/export/dist');
while I want to be like
import Numbers = require('#hk18/export');
It is including the dest folder in the package why so ?
{ "compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "./dist",
"moduleResolution": "node",
"declaration": true }, "include": [
"src/**/*.ts",
"index.ts" ] }
___________________________________________________________
{ "name": "#hk18/export", "version": "2.0.0", "description": "test project to create npm package", "main": "index.js", "scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
"lint": "tslint --project \"./tsconfig.json\"",
"test": "mocha --reporter spec",
"docs": "typedoc --out docs src",
"gh-pages": "rimraf docs && npm run docs && gh-pages -d docs" }, "author": "HK", "license": "ISC", }
Your package.json is missing a types entry. It should match your main entry. Also your main should point to the dist folder as well.
Fixed Version
"main": "./dist/index.js",
"types" : "./dist/index.d.ts",

Elastic Beanstalk "error TS2688: Cannot find type definition file for 'node'."

I'm trying to deploy an app to elastic beanstalk's NodeJs Platform (10.15.3) but as it builds (the Node commmand is npm start --production) I'm seeing the following errors in logs:
npm run build
app#1.0.0 build /var/app/current
tsc -p .
error TS2688: Cannot find type definition file for 'node'.
My package.json is the following (it includes #type/node:10.14):
{
"name": "app",
"version": "1.0.0",
"private": true,
"main": "dist/app.js",
"scripts": {
"build": "tsc -p .",
"prestart": "npm run build",
"watch": "tsc -w -p .",
"start": "node .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"#types/morgan": "^1.7.35",
"#types/node": "^10.14",
"aws-sdk": "^2.411.0",
...
},
"devDependencies": {
"#types/express": "^4.16.1"
}
}
You should add types to your tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist-api",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"types": ["node"]
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}

ts-node ignore custom type declaration

Problem is as the topic states it. Interesting thing - i have another project with a very similar config and types working well there.
package.json
{
"name": "untitled",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node test.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#types/node": "^12.0.4",
"fs": "0.0.1-security",
"jszip": "^3.2.1",
"ts-node": "^8.2.0",
"typescript": "^3.5.1"
}
}
tsconfig.json
{
"compilerOptions": {
"lib": [
"es6",
"dom"
],
"typeRoots" : [
"./node_modules/#types",
"."
],
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": false
},
"exclude": [
"node_modules",
"**/*.spec.ts",
"**/*.test.ts"]
}
index.d.ts
declare interface Array<T> {
random() :T;
}
declare namespace NodeJS {
export interface Global {
__stack: any;
__line: any;
__function: any;
sleep: Function;
}
}
It's a simple project for test purposes - index.d.ts located in project's root dir(tried to move index.d.ts -> typings/index.d.ts) as well (surely tsconfig.json too with new paths).
every attempt ends up with
TSError: тип Unable to compile TypeScript:
polyfills.ts:1:17 - error TS2339: Property 'random' does not exist on type 'any[]'.
polyfills.ts
Array.prototype.random = function () {
return this[Math.floor((Math.random()*this.length))];
};
these errors.
Please help :)
Update:
tsc && node build/test.js is working properly

Resources