Zipping up Node App and having dependencies work out of box - node.js

I created a Node app that relies on a dependency (Jest) that is just for running tests.
I want to zip this app up and run it and its tests on another machine that has node installed.
My package.json includes
"scripts": {
"test": "jest"
},
and jest is included in the app's npm_modules folder.
The tests work perfectly on the machine that the app was developed on. However, upon extracting the zip and running on another computer, I get an error that jest is not found.
I then reinstalled jest without '--save-dev' and installed it for production use, thinking this would resolve the error, but it did not.
I thought the point of having npm_modules and using 'npm install' was so that the app's dependencies can all be localized and portable i.e. not having host machine have to install modules separately.
Is there any way to resolve this? Thanks!

According to "scripts": { "test": "jest" }, you have jest installed globally on source machine. Change it to "scripts": { "test": "node_modules/.bin/jest" },

Try using pkg
Github Page
npm
If you just need to run it on the second machine, pkg should let you package it as a standalone executable.

I'm guessing the problem is either that Jest installs differently based on the OS. It may also be a problem with import-local, one of the packages that Jest depends on.

Related

How can I deploy a Vite Node.js app on Heroku? Everything I do seems to build but fails to run

I am deploying using Github (automatic fetching and building from the repository). The project version there is the dev version, but since Heroku builds it first, it shouldnt be a problem.
First Heroku fetches and builds the project, which always succeeds:
See here
But, right after that, when Heroku tries to run and deploy the build, it says "vite" is not found:
See here
If i run it locally it works. If i build and preview it locally it also works.
Does anyone know what I should do to make this work?
I tried checking my package.json for "dependencies" vs "devDependencies" differences and added "vite" to the "dependencies" section:
See here
The Procfile they advise to use I've tried running "npm run start", "npm start", "npm i && npm start" and "npm i && npm run start", but all of them give the same exact error. Also tried it with "npx" but it fails to install vite.
After i contacted their support, they just said it falls outside of their "support policy".
I just want this to be able to run, since it works when i run it locally and when i build and preview it.
EDIT: After moving both vite and vite plugins to the dependencies instead of the devDependencies, i got this message See here

Node.js is causing error: Missing script: "start"?

I just started studying front-end development and I'm struggling with a node.js error.
Typing 'npm start' in my VSCode terminal used to work fine for simple tutorial projects with just an index.html, script.js, and style.css file. (without a package.json file)
However after trying out React for the first time, 'npm start' now doesn't work anymore in my other non-React projects. At first it was giving me an error that it was missing the package.json (which it didn't need before?) but after trying to fix it with help of googling I now got to a point where it's giving me the error: Missing script: "start".
How can I run node without creating package.json files for every small tutorial project I've made previously, or without turning them into React apps? Also why is this happening? Did installing React-native create dependencies of some sort?
Thanks in advance!
I already tried reinstalling node.js and tried different versions. Also tried deleting package-lock.json. It still works for React apps, just not with simpler native javascript apps.
A package.json file is required if you want to install any packages or run scripts in your terminal. In your package.json file, make sure you have added scripts property. This is an example of how you can use it:
{
...
"scripts": {
"start": "react-scripts start"
}
}
Remove ... from the snippet if you're copying, this has been added to indicate that there are one or more fields in this JSON file.
After you have added this to your package file, you will be able to run the start script by typing npm run start in the terminal or if you use Yarn: yarn start.
Edit:
You said that running npm start in your React project is running fine, but on your simpler projects with only a simple HTML, CSS and JS file is not working when using the script.
You are probably confusing npm start with node file.js. Where node file.js doesn't require a package to be in your project to run a JavaScript file, using npm start requires you to have a JSON file present in your project folder with the JSON code as in my answer.
So long story short: Using npm start requires package.json with the script property available. While node file.js doesn't require you to have this file in your project.
if you are using react-native you can do the following
First you have to build your project with the command
npx react-native run-android , npx react-native run-ios
Once your project has build successfully and app is installed on your device then you your development server is started already. for some reason if your server is closed then you can run it with the command given below.
adb reverse tcp:8081 tcp:8081 this will send a signal to your device and after this run npx react-native start

Cannot deploy firebase cloud functions, because functions/lib/index.js does not exist

I think there is a bug in firebase cloud functions setup.
I did:
npm install -g firebase-tools
firebase init functions
I have configured it for typescript, everything installed, yet I cannot deploy the functions because I am getting this error message:
Error: There was an error reading functions/package.json:
functions/lib/index.js does not exist, can't deploy Cloud Functions
I know that it does not exist - there is no lib folder at all, but what can I do to run the functions?
Why I can't run functions, if I done everything that needed to be done?
I understand you might be following the instructions from the Getting started guide or the Use Typescript for Cloud Functions one, during this section the wizard helps you choose Typescript as your language to write Functions. Please be sure the language and its dependencies are correctly installed. And in this last guide for the Using an existing Typescript project, it asks you to edit the package.json to add a bash script to build your typescript project:
{
"name": "functions",
"scripts": {
"build": "npm run lint && tsc"
}
...
and the firebase.json to add a predeploy hook to run the build script:
{
"functions": {
"predeploy": "npm --prefix functions run build",
}
}
but in this case, you need to check if this configuration was made during the installation.
You can check this answer where the user used sudo npm install typescript to install them, and as Doug mentions try to install it only in your project, not globally; and it must be defined in your package.json as well.
Another example that fixed a similar issue where:
The user removed everything related to Firebase Functions
Entered in the project directory using cd functions (in this case replace the ‘functions’ name for your project)
Ran npm install
Running again firebase init
Let me know if you were able to solve this problem to further assist you, trying to add more information as steps and documentation followed and the logs.
Open a terminal and cd to functions
Then run npx tsc --watch
And try serving and deploying the project

How to make npm scripts refer to local packages?

I am working on a project and I want to use JSdoc for documentation. I have listed it in devDependencies and created a script to run it. My package.jsonlooks like:
"scripts": {
"doc": "jsdoc -c ./conf.json"
},
"devDependencies": {
"jsdoc": "^3.4.3"
}
Cloning my project on another machine and typing npm install installs jsdoc to the node_modules folder.
However, when I run npm run doc, I get a large error, the center of which is: 'jsdoc' is not recognized as an internal or external command
This is because JSdoc is installed in node_modules not my local path.
What is the best way to point npm-run commands to my locally installed modules? Also, what is the point of having dependencies such as JSdoc listed in devDependencies (thus installed in node_modules) when they can not be used from there?

NPM: Change the `bin` output directory for the node modules

Currently, if you are using a package.json file to manage your project's dependencies (whatever project it is, may it be a ruby, php, python or js app), by default everything is installed under ./node_modules.
When some dependencies have binaries to save, they're installed under ./node_modules/.bin.
What I need is a feature that allow me to change the ./node_modules/.bin directory for ./bin.
Simple example:
A PHP/Symfony app has a ./vendor dir for Composer dependencies, and all binaries are saved in ./bin, thanks to the config: { bin-dir: bin } option in composer.json.
But if I want to use Gulp to manage my assets, I create a package.json file, require all my dependencies and then run npm install.
Then, my wish is to run bin/gulp to execute gulp, but actually I have to run node_modules/.bin/gulp which is not as friendly as bin/gulp.
I've looked at package.json examples/guides on browsenpm.org and docs.npmjs.com, but none of them works, because they are here to define your own project's binaries. But I don't have any binaries, because I want to use binaries from other libraries.
Is there an option for that with NodeJS/NPM ?
You might consider adding gulp tasks to your package.json.
// package.json
{
"scripts": {
"build-templates": "gulp build-templates",
"minify-js": "gulp minify-js"
}
}
You can run any scripts specified in package.json by simply running the following:
$ npm run build-templates
$ npm run minify-js
You get the idea. You can use the gulp command inside the string without doing ./node_modules/.bin/gulp because npm is smart enough to put all scripts from ./node_modules/.bin/ into the path for that script execution.

Resources