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
Related
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.
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
I have a problem with typescript compilation. Has anybody else received this error?
node_modules/#types/node/index.d.ts(20,1): error TS1084: Invalid
'reference' directive syntax.
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./app",
"target": "es6",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"listFiles": false,
"skipLibCheck": true
},
"include": [
"./app/**/*.ts"
]
}
typescript version in package.json: "typescript": "^2.6.1"
Had the same issue. Open the file ../node_modules/#types/node/index.d.ts
and remove the third slash
// <reference lib="es2015" />
compile again
I got the same error. I used the pinned version "#types/node": "7.0.7" in my package.json and got it working.
I have faced the same problem and found the solution is to update typescript to the latest version from the current version.
Make changes in the package.json file like below:
"devDependencies": {
"typescript": "^3.9.7"
}
Now "npm install typescript" , it will upgrade to latest version. Then run "ng serve" and it will compile successfully.
Its worked for me
ckeckout your tags for versions of TypeScript.
run npm dist-tag ls #types/node and look for your currently typescript version.
Then install the #types/node version supporting for typescript
in my case I had to install 14.0.1 version
reference: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/47250#issuecomment-687701880
update TypeScript
npm install typescript#latest --save-dev
Had the same error and the fix was to use the previous version, check the version that you have in package.json for #types/node and see if you have the latest or not.
You can check here: https://www.npmjs.com/package/#types/node as it seams that there was a new update.
My solution is to change typescript version to '>=2.7.3'.
I think the version might depend on other packages.
probably need to try a couple of times to get the right version.
And don't forget to run install.
Try downgrading the #type/node.
I had the same issue with
"devDependencies": {
"#types/node": "12.20.42",
}
I downgraded it to lower version and it compiles successfully
"devDependencies": {
"#types/node": "12.19.12",
}
I have faced the same problem and found the solution is to update typescript to the latest version from the current version.
Make changes in the package.json file like below:
"devDependencies": {
"typescript": "^3.9.7"
}
Now npm install typescript, it will upgrade to the latest version. Then run "ng serve" and it will compile successfully.
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
So I started working on a TypeScript project in Visual Studio 2017 community edition. I setup a new NodeJS TypeScript Console project. Everything was going great. I was figuring out little idiosyncrasies in TS. Things would build and I could debug them fine.
I then found out about #types. This was cool, I thought. I can now get intellisense for lodash! I ran:
npm install -D #types/lodash
It installed and I had all the info right in the ide! Sweet. I made some changes and then went to build and run.
The build failed! About 80 some errors about basic stuff. I thought it was my changes but as I backed them out nothing changed. I then by happenstance uninstalled the #types for lodash. It built fine. Installed it again and it failed.
Note, I also tried saving it to dependencies:
npm install -S #types/lodash
No go. :(
The same goes for any #types/* that I install.
I don't have a tsconfig.json in my project as it seems like the project itself is setup to pass that configuration. I also assume that MSBuild is handling this somehow. Finally, I must also assume that I have some version of TS 2.x installed with VS 2017.
What gives?
Other Troubleshooting
I installed the typescript package globally (hadn't done that, just been compiling from VS2017)
npm install -g typescript
This installs v2.2.2 but makes no difference when attempting to build from inside VS2017.
Errors
Configs
I added in tsconfig.json to try to exclude #types from the compiler. Now I can't build at all.
tsconfig.json:
{
"compilerOptions": {
"lib": [ "es2015", "es2015.promise" ],
"noImplicitAny": false,
"module": "commonjs",
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es2015"
},
"exclude": [
"node_modules"
]
}
package.json
{
"name": "my-stupid-project",
"version": "0.0.0",
"description": "my-stupid-project",
"main": "index.js",
"author": {
"name": "Mike"
},
"dependencies": {
"backblaze-b2": "^1.0.0",
"lodash": "^4.17.4",
"mkdirp": "^0.5.1",
"moment": "^2.18.1",
"nedb3": "^3.0.0",
"winston": "^2.3.1"
},
"devDependencies": {}
}