Nodejs package.json defining scripts with the same name as a dependency - json

I've seen a lot of package.json files where there are scripts that doesn't do anything but call a dependency with the same name. For example :
{
"scripts": {
"lint": "npm run tslint \"src/**/*.ts\"",
"tslint": "tslint"
},
"devDependencies": {
"tslint": "~4.4.2",
"tslint-loader": "^3.3.0"
}
}
Here we have the script tslint that just calls the dependency tslint. I guess that is some kind of a way to make the lint script shorter but how would it look like if there was no script called tslint.

I don't believe NPM has this kind of functionality built in. Yarn (the third-party NPM client built by Facebook, Google, Exponent and Tilde) on the other hand, does - you can just use yarn run and it will pick up the executable from your dependencies, even if you don't have a script for it defined in your package.json:
yarn run tslint
yarn run tslint "src/**/*.ts"

Related

How to get React Project to work Because NPM Start Wont Work

Hello I downloaded a react project from https://github.com/fabiau/jc-calendar and when I do npm start I get error messages. I tried to do "NPM Fund" and "NPM Update" and none of them worked. Obviously i have no clue what I am doing so if anyone can point me in the right direction I would apprecaite it.
Before starting dev server you'll have to install the dependencies of the project using npm install , as generally node_modules/dependencies are not part of the repository.
This is mentioned in Readme.md of JC-calender.
Try deleting folder node_modules and package-lock.json if exists and run command npm install
Checkout scripts in package.json in usual case start will be as given below but as per the code you consider npm-run-all package is used so be sure to run npm install
"scripts": {
"start": "react-scripts start",
},
This may help you out https://www.npmjs.com/package/npm-run-all
most of the repositories on GitHub, don't have dependencies installed, after downloading a repository on your local machine, you need to run "npm install". The reason behind this is that npm will look for all the required dependencies of the specified project and install them on your machine, with node-modules, then run "npm start"

functions/lib/functions/src/index.ts does not exist, can't deploy Cloud Functions

When I try deploying my Firebase cloud functions I get the following error.
Desired behavior: Deploy functions successfully.
Error:
Error: There was an error reading functions/package.json:
functions/lib/index.js does not exist, can't deploy
Cloud Functions
Full log:
name#name-MacBook-Pro functions % firebase deploy
=== Deploying to 'newtiktok-21570'...
i deploying functions Running command: npm --prefix "$RESOURCE_DIR"
run lint
functions# lint /Users/name/Desktop/Yoveo/functions
eslint "src/**/*"
/Users/name/Desktop/Yoveo/functions/src/index.ts
186:67 warning 'timestamp' is defined but never used
#typescript-eslint/no-unused-vars 377:86 warning 'mediaNum' is
defined but never used #typescript-eslint/no-unused-vars 377:104
warning 'commentText' is defined but never used
#typescript-eslint/no-unused-vars 377:125 warning 'commentID' is
defined but never used #typescript-eslint/no-unused-vars 419:119
warning 'commentID' is defined but never used
#typescript-eslint/no-unused-vars 463:121 warning 'commentID' is
defined but never used #typescript-eslint/no-unused-vars 520:75
warning 'mediaNum' is defined but never used
#typescript-eslint/no-unused-vars 732:25 warning 'slap' is
defined but never used #typescript-eslint/no-unused-vars
✖ 8 problems (0 errors, 8 warnings)
Running command: npm --prefix "$RESOURCE_DIR" run build ✔ functions:
Finished running predeploy script.
Error: There was an error reading functions/package.json:
My p.json:
{
"name": "functions",
"scripts": {
"lint": "eslint \"src/**/*\"",
"build": "",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "12"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0"
},
"devDependencies": {
"#typescript-eslint/eslint-plugin": "^4.8.1",
"#typescript-eslint/parser": "^4.8.1",
"eslint": "^7.14.0",
"eslint-plugin-import": "^2.22.0",
"firebase-functions-test": "^0.2.0",
"typescript": "^3.8.0"
},
"private": true
}
cd into your functions folder and run this command
npm run-script build
This will create the lib/index.js file that is missing
firebase uses main field in package.json as program entry point,
set it properly, probably like this.
"main": "lib/src/index.js",
For some reason recently the build flow of firebase functions changed.
It used to be:
npm --prefix ./functions install ./functions
firebase deploy --only functions
now it is:
npm --prefix ./functions install ./functions
npm --prefix ./functions run build
firebase deploy --only functions
I have not researched what caused this change, but adding this as build step fixed the problem for me.
functions/lib/index.js does not exist
In case you are working in firebase project that contains a frontend or is structured as a monorepo, this error may also stem from having accidentally imported a frontend file in the functions backend part of the project. For all files that are not within your functions project scope, the typescript compiler will refuse to compile ts files referencing them. So in this case, the solution is to search for any imports containing /src/ (or any other paths pointing outside) and remove (or correct) them within your functions project.
you just have to change the main inside package.json file from lib/index.js to your index file which is usually under the src folder
Solved:
I was able to solve the problem by removing everything associated with Firebase functions. And running: firebase init again. After I cd functions run npm install. Then I was able to deploy successfully after fixing an error with:
3:26 error 'express' should be listed in the project's dependencies. Run 'npm i -S express' to add it import/no-extraneous-dependencies
Changing the firebase.json file to the following fixed my issue:
{
"functions": {
"predeploy": ["npm --prefix ./functions run build"],
"source": "functions"
}
}
I was able to fix this same issue by following Felix K indications, answered on Apr 28, 2021.
In case you are working in firebase project that contains a frontend or is structured as a monorepo, this error may also stem from having accidentally imported a frontend file in the functions backend part of the project. For all files that are not within your functions project scope, the typescript compiler will refuse to compile ts files referencing them. So in this case, the solution is to search for any imports containing /src/ (or any other paths pointing outside) and remove (or correct) them within your functions project.
In my case, I've accidently imported an interface from the frontend. When updating this import I was able to successfully deploy my function.
Solution from Edward Amoah Idun:
cd into your functions folder and run this command
npm run-script build
This will create the lib/index.js file that is missing
Yes, but it will create the index.js file that is missing in the wrong folder. Still necessary to check that you don't have imports from another projects.
The lib folder is for your built functions code, so you haven't built it. This can be done automatically by adding redeploy code to your firebase.json config file:
{
"functions": [
{
"predeploy": ["npm --prefix \"$RESOURCE_DIR\" run build"],
// rest of config...
}
]
}

package.json know if there are any dependencies that are not used

I have on a dependency project, I would like to know which of these dependencies is never used.
Example I have the following dependencies:
"dependencies": {
"axios": "^0.19.2",
"d3": "^5.16.0",
"d3-scale": "^3.2.2",
...
}
Let's say that in my project I have never used the dependency that I had installed the d3, I would like it to be reported to me.
How can I get a list of all dependencies installed but not used?
There is a package for that.
npm install -g depcheck
Run in your project directory
depcheck
See the results
Unused dependencies
* chalk
* express
Unused devDependencies
* nodemon

How to invoke multiple calls in pre/postinstall scripts in package.json

I want to run a couple different scripts during preinstall, and a few postinstall, but the examples on npm where they split the calls using semi-colons doesn't work, and ends up throwing an error. Was hoping to be able to do something like:
"scripts": {
"preinstall": "composer install; php artisan key:generate; grunt build:app",
"install": "bower install",
"postinstall": "bin\\post_install.sh git#bitbucket.project/project.git"
},
I wanted to have "bower install" in postinstall, but didn't work so since install is essentially the same I put it there, now that I want a couple preinstall calls invoked I don't have the same solution available since only one option. I didn't want to split them all out in bash scripts for each if possible.
Separate commands using '&&' like this:
"preinstall": "composer install && php artisan key:generate && grunt build:app"

How can I use npm to download and update modules from a list in a package.json file?

I am using windows with node.js downloaded. I created this package.json.
{
"version": "0.0.0",
"name": "abc",
"devDependencies": {
"del": "^1.1.0",
"gulp-uglify": "^1.0.2",
"gulp-sourcemaps": "^1.2.8",
"gulp-typescript": "^2.3.0",
"less-plugin-clean-css": "^1.2.0",
"typescript": "^1.3.0"
}
}
Is there an npm command line task that I can run to fetch all of these modules and install / update these into a node_modules directory? If needed I can change my package.json so I would appreciate advice on that also.
Thanks
Doesn't
"npm install"
do the trick? (from your folder that contains the package.json)
It works on linux, haven't a window machine to try right now
try
npm install
npm update --save-dev
If you already had the package.json file (with dependencies entries), then you can use npm install to install dependencies on node_modules directory.
npm install
or you can update the dependencies to use latest version (will override currently installed dependencies on node_modules directory) using npm update
npm update
You don't have to put the dependency you want manually to package.json, you just use command npm install {package-name} with additional option like --save / --save-dev / --save-optional.
In example you want to add dependency of node-q to your application, you can do this:
npm install q --save
Juggling into different option --save-prefixed value will act as follows,
--save will add dependency to dependencies attribute of your package.json, it will be installed mainly for your application
--save-dev will add dependency to devDependencies attribute of your package.json, it will be installed for development phase of your application (usually it is testing dependencies)
--save-optional will installed for optional (nice to have) dependencies. I rarely used it for my applications or libraries anyway.
Don't forget you MUST run commands from your application directory (the one that node_modules directory resides).