Installed dependency in node_modules is missing node_modules - node.js

I have 2 TS packages. project-1 is installed as a dependency in project-2. I am able to import and access all type definitions of project-1. But the dependencies (node_modules directory) in project-1 are not found. This is because the node_modules directory of project-1 is not present.
Do I really need to manually npm install inside that node_modules/project-1 directory? Or should the node_modules directory be published to npm as well? If so, should I remove it from tsconfig.json's exclude property? I think node_modules should be in .gitignore right?
Just to be clear:
./project-2/node_modules // this exists
./project-2/node_modules/project-1 // this exists
./project-2/node_modules/project-1/node_modules // this does not
/* only after manually running npm install in
* ./project-2/node_modules/project-1 the node_modules folder appears.
*/
project-1
** package.json**
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "commonjs",
tsconfig.json
{
"compilerOptions": {
"declaration": true,
"esModuleInterop": true,
"module": "CommonJS",
"outDir": "dist",
"baseUrl": ".",
"rootDir": "src",
"strict": true,
"target": "ES5",
"noImplicitAny": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "src/**/*.test.ts"]
}
.gitignore
node_modules
dist
logs
tsconfig.tsbuildinfo
.env
.npmignore
this file is empty.
How do I ensure that the node_modules directory is present when installing an npm package?

The node_modules directory should not be published to npm registry. It should only contain dependencies installed during the development of your project. To ensure that the node_modules directory is present when installing an npm package, you need to run npm install or yarn install in your project root directory after you have installed your dependencies. The node_modules directory should also be listed in your .gitignore file to avoid committing it to your Git repository.

Related

ts-node-dev runs into an error: "Cannot find module 'typescript'"

I am trying to set up a basic nest.js app.
main.js is in the src folder. I run this command:
npx ts-node-dev src/main.ts
I get this error:
Cannot find module 'typescript'
Require stack:
- /home/yilmaz/.npm/_npx/429895/lib/node_modules/ts-node-dev/lib/index.js
- /home/yilmaz/.npm/_npx/429895/lib/node_modules/ts-node-dev/lib/bin.js
I installed typescript globally, close the terminal and restart it. run tsc -v and I get "Version 4.3.5" but the error is not resolved.
This is the tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
After I installed typescript, I recreated this file with tsc --init
and enabled
"experimentalDecorators": true,
"emitDecoratorMetadata": true
I had this problem myself and even tough I didn't clearly understand the main cause, I did the following stuff and it worked out.
So basically I have installed the ts-node-dev package as a dev dependency(npm i --save-dev ts-node-dev), and after that was running into another error regarding rxjs, so I just installed that too as a dependency(npm i rxjs) and after that it worked ts-node-dev as well as npx ts-node-dev

TypeScript: Cannot use import statement outside a module (Node.js)

I've been developing a package in node using typescript. When I install the package locally (i.e. providing the local path to the directory of the package, $ npm install ../my_package) everything works fine.
But I've published the package in npm and now when I install the same package from npm it shows me the above-mentioned error:
Cannot use import statement outside a module
I could not find a solution to my problem anywhere. I would really appreciate it if anyone could help me out here.
Error Msg Image
Try adding the line below to your package.json.
"type": "module",
If anyone is having the same problem while developing in typescript here's how I solved it.
If you don't already have tsconfig.json file you can create one by $ tsc --init.
Add the following to the tsconfig.json:
"declaration": true,
"outDir": "./dist",
"rootDir": "./",
In your package.json file add
"types": "dist/index.d.ts",
"main": "dist/index.js",
Compile the project with $ tsc.
Ps: in the .gitignore file add node_modules and /dist. Now publish it using $npm publish.

Typescript error -- Error: Cannot find module 'typescript/tsc.js'

I cannot figure out how to make my 'tsc' command working:
tsc
module.js:472
throw err;
^
Error: Cannot find module 'typescript/tsc.js'
at Function.Module._resolveFilename (module.js:470:15)
My 'package.json' has:
"devDependencies": {
"#types/node": "^14.11.2",
"tslib": "^2.0.1",
"typedoc": "^0.19.2",
"typescript": "^4.0.3"
}
And my 'tsconfig.json':
{
"compilerOptions": {
"target": "es6",
"sourceMap": true,
"declaration": true,
"declarationDir": "dist/types",
"strict": true,
"noUnusedLocals": true,
"lib": [
"es2019"
]
},
"include": [
"src"
]
}
No error during 'npm install'.
When I check up the 'node_modules' folder, typescript exists and the 'tsc.js' file exists within the 'typescript/lib' directory.
node --version: 12.18.4
npm --version: 6.14.6
system: Ubuntu 16.04 LTS
I have tried many of the solution proposed for a similar issue without success:
delete the node_modules folder and then run npm install again
try to reinstall manually: npm install typescript --save-dev
using: npm install typescript-tools --save-dev
...
None of the above gets me any result.
Thank you very much for your help.
For those who gets stuck with that, the command that should be run with your local tsc is:
npx tsc
To make the tsc --version command working, try to install using npm install -g typescript command. It installs the typescript globally.

NestJS Create Reusable/Publishable Library

I want to create a Nest.js Package, so I can reuse a controller in multiple Nest.js projects.
I have checked out library but that doesn't seem to be what I was looking for because it seems to that it is only possible to import the library within the repo.
What the best way to have a library that could be published via npm so I can install and import it in other Nest Projects?
This is how I ended up doing it
Create Folder and initialize Node Package
mkdir PACKAGE_NAME
cd PACKAGE_NAME
npm init // Follow the steps to initialize npm package
install the following dependencies and dev dependencies
npm install #nest/common rxjs reflect-metadata
npm install -d #types/node rimraf typescript
Afterwards go to your package.json and change/add main, types, scripts to the following
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rimraf dist && tsc",
"prepublish": "npm run build"
},
Run npm install
Create tsconfig.json file in your folder
Add following code to tsconfig.json
"compilerOptions": {
"experimentalDecorators": true,
"target": "es2017",
"module": "commonjs",
"lib": ["es2017", "es7", "es6"],
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"noImplicitAny": false,
"strictNullChecks": false,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true
},
"exclude": [
"node_modules",
"dist"
]
}
Create src folder. Add your Nest code in this folder.
Add a index.ts file in your src folder. In this file you export your Nest Code, that you want to use outside of this package. For Example:
import { CpuUtilizationModule } from "./cpu-utilization-observer.module";
import { CpuUtilizationService } from "./cpu-utilization.service";
export { CpuUtilizationModule, CpuUtilizationService }
This exports a Module and a Service, wich you can import/provide in your Nest Project
Run npm run build. This will compile your code to into the dist folder.
Now you can install this package (locally) with npm i PATH_TO_PACKAGE.
one of the best ways to do it is by creating a dynamic module.
this topic will help you with click here.
for publish it via npm, this article will be your guide to do it with a clear way Publishing NestJS Packages with npm

Can't extract ng-xi18n ENOENT: no such file or directory

I'm trying to add internationalisation to my Angular 2 project, and following the intructions in the docs.
I started the project using angular-cli and my tsconfig.json is in /src.
Running the command:
./node_modules/.bin/ng-xi18n -p src/tsconfig.json
I get the following error:
Error: ENOENT: no such file or directory, open '/home/project/dist/out-tsc/src/app/_models/example.metadata.json'
at Error (native)
at Object.fs.openSync (fs.js:641:18)
at Object.fs.writeFileSync (fs.js:1347:33)
at MetadataWriterHost.writeMetadata (/home/project/node_modules/#angular/tsc-wrapped/src/compiler_host.js:164:22)
at MetadataWriterHost.writeFile (/home/project/node_modules/#angular/tsc-wrapped/src/compiler_host.js:143:19)
at Object.writeFile (/home/project/node_modules/typescript/lib/typescript.js:64240:132)
at Object.writeFile (/home/project/node_modules/typescript/lib/typescript.js:9020:14)
at printSourceFileOrBundle (/home/project/node_modules/typescript/lib/typescript.js:61204:16)
at emitSourceFileOrBundle (/home/project/node_modules/typescript/lib/typescript.js:61155:21)
at Object.forEachEmittedFile (/home/project/node_modules/typescript/lib/typescript.js:8986:17)
Extraction failed
And it's not wrong, the directory /home/project/dist/out-tsc/src/app/ does not exist, instead it is /home/project/dist/out-tsc/app/. But I'm not sure how to fix it.
I'm suspecting the outDir parameter in tsconfig.json
{
"compilerOptions": {
"baseUrl": "",
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2016",
"dom"
],
"mapRoot": "./",
"module": "es2015",
"moduleResolution": "node",
"outDir": "../dist/out-tsc",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/#types"
]
},
"exclude": [
"app/testing/*",
"app/**/*.spec.ts",
"test.ts"
]
}
If it is at all relevant #angular/compiler-cli and #angular/platform-server are both at version 2.4.10, and npm is giving me the following issues:
typescript#2.2.2 invalid
UNMET PEER DEPENDENCY #angular/compiler#2.4.10
UNMET PEER DEPENDENCY #angular/forms#2.4.10
Try with ng xi18n
I created a new angular-cli (version 1.0.0):
ng new i18n-test
cd i18n-test
ng xi18n
Works just fine.
If this does not work, make sure your package.json has dev-dependency "#angular/cli": "1.0.0"
then clean everything:
remove node_modules folder in your project.
remove #angular/cli global dependency npm uninstall -g #angular/cli
install it back npm install -g #angular/cli
install your project dependencies back npm install

Resources