I am on freeCodeCamp, have reached this challenge. I have completed installing NPM, started a project and installed a module too, but I am stuck at NPM TEST which says:
Now you've installed something, and used npm ls to show what's going on. If you look at the package.json file, it has this rather odd bit in it:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
npm can be used as a task runner, and almost every module and project
will have a test script that runs to make sure everything is good. In
order to help remind you to do this, npm puts a "always failing" test
in there by default.
First, create a file called test.js. It doesn't have to do anything,
really. (This is npm class, not testing class.) But it has to exit
without throwing an error, or else the test fails.
Then, edit your package.json file to make your scripts section look like
this instead:
"scripts": {
"test": "node test.js"
},
Once that's done, run how-to-nom verify to check your work.
I tried couple of things I got online to create test.js, but I am not sure how to proceed further.
All you have to do is create an empty test.js file and edit your package.json file. Chances are you missed this step.
In order to pass the test, you need to update the echo \"Error: no test specified\" && exit 1 line with node test.js. As it is an empty file, it won't throw any error and once you run the how-to-npm verify command it will pass.
Related
I try to run npm test in Github Action, but it just stuck there forever. I can see that the tests are completed successfully in very short time, so I have no idea why it is not terminating
This is the test script in package.json
"test": "react-scripts test --watchAll --coverage --forceExit --transformIgnorePatterns \"node_modules/(?!axios)/\"",
You can try removing the --watchAll flag from the test script to see if that helps. The script should then run the tests once and exit when they are completed.
It's possible that the --watchAll flag is causing the test script to stay running indefinitely.
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...
}
]
}
I used nodejs on my vserver to make a tiny script to manage users in a db.
In package.json I added the "bin" and set it to my script. My attempt was to make a command available on the whole server so I dont need to go to the directory where the script lies and write "node usermanager.js".
I used npm link and it seemed to work fine:
/home/sl4yer/bin/cl9wnhook -> /home/sl4yer/lib/node_modules/cl9wnhook_usermanager/usermanager.js
/home/sl4yer/lib/node_modules/cl9wnhook_usermanager -> /home/sl4yer/cl9wnHook/usermanager
package.json btw is:
{
"name": "cl9wnhook_usermanager",
"version": "1.0.0",
"description": "User manager for cl9wnHook",
"main": "usermanager.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"cl9wnhook": "./usermanager.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.9.0",
"js-sha512": "^0.2.2",
"readline-sync": "^1.4.5"
}
}
so using the command "cl9wnhook" should work.
But when I call it, I get:
[sl4yer#lynx usermanager]$ cl9wnhook
: No such file or directory
Any idea?
sudo npm link
I did it, and successfully.
Try adding
#!/usr/bin/env node
on the top of your usermanager.js file. It should work.
Pack the package
npm pack
Then install it globally to run it from any folder.
npm install --global <package_file>.tgz
your bin in your package.json like this:
{
"bin": {
"app": "bin/app"
},
}
but first in your app file you should add at the top this #!/usr/bin/env node
Note
This will help you
Really things can mess up from time to time when working with NPM or Yarn. In these cases first do a complete clean install.
rm -rf ./node_modules
yarn install
npm install
If that doesn't solve your problem then check the bin property in package.json, if it is mapping to the cli file correctly?
You can check what is packed by running yarn pack.
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"
I've gotten some NPM package from a third party who is developing under Mac OSX. Their build can split into either development or production using "scripts" object in package.json. For example:
"scripts": {
"build": "NODE_ENV=dev node make.js --build",
"build-prod": "NODE_ENV=prod node make.js --build",
}
Under Unix, one can run either "npm run build" or "npm run build-prod" to build either directory (naturally, there are some conditional statements in make.js).
Of course, it does not work under Windows - I had to change the commands similar to this:
"scripts": {
"build": "set NODE_ENV=dev&& node make.js --build",
"build-prod": "set NODE_ENV=prod&& node make.js --build",
}
(Please note that it was important not to put a space before the '&&' - otherwise the environment variable was created with an extra white space in it which ruined all those comparisons in make.js).
However, I would like to have some universal source tree which would work under either Unix or Windows without editing. Could you please give some ideas on how to conditionally split the build depending on the OS?
The question is pretty old, but for these who faces the problem nowadays, npm starting from version >=5.1.0 supports setting shell for processing scripts. By default on Windows, npm internally uses cmd.exe for running scripts, even if npm command itself is typed in git-bash. After setting git-bash as shell, scripts that use bash syntax work normally on Windows:
npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe"
Here one needs to substitute his correct path to git-bash executable.
I have been thinking for a while, but I doubt there is any aesthetic solution using these tools, to get the desired effect.
If you are able to influence the change in make.js, I would rather change this file to accept prod or dev as argument, example: node make.js --build=dev. With default value, to ensure backwards compatibility.
Using only npm and not modifying make.js, I could think of only running another JavaScript code, which would change environment variable, and then call make.js.
That will look something like:
"build": "node middleman.js"
Middleman.js file could then use child_process or another module to set variable and execute node make.js file.
If you do not want to create an extra file, you can then embed all the JavaScript inside the package.json using:
"build": "node -e 'my code'"
Be warned, that running "node -e 'process.env[\'NODE_ENV\']=\'dev\' && node make.js" will not work, as process.env sets variable in local process, not global (i.e. does not export to the system).
Not the direct solution, but for sake of best practices, make it work different.