Installing package by adding package to node package managers package.json file? - javascript

Sorry for such a trivial question, but I'm new to node.
So can one install a package using only the npms package.json file? If yes how can we achieve this? I know I could install it using npm install packageName command, but my project is running on windows azure mobile service and it just allows to modify the package.json file and not to run the npm commands.
So if this is possible, then where do I specify the package name to load and install in the package.json file?

Here is an example of using package.json, imagine you want to add package express as dependency:
{
"name": "TAP",
"description": "Simple package by TAP",
"author": "The amateur programmer",
"dependencies": {
"express": ">= 1.2.0"
}
}
After making changes to the package.json, you need to run npm install command for changes to take effect.
For more information, please refer to the Documentation.

you will get dependencies & devDependencies fields in package.json file, modify them accordingly.
Let's assume you have following content in package.json.
{
"name": "node-app",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "~4.2.0"
}
If you want to add underscore as dependency modify dependencies add another key as package-name
{
"name": "node-app",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "~4.2.0",
"underscore" : "~1.0"
}
But make sure adding ,(commas) properly.

Related

Is there a way to get npm install to install "bin" executables in the local node_modules directory (the .bin directory)?

I have a Node project, and I can npm pack it into a tarball and install it in another directory for testing. When I do that, the set of files in the "bin" clause of my package.json are correctly symlinked into the node_modules/.bin directory there. Thus I can use npx something to run the "something" script and it works as I would expect.
From the actual project directory, however, npm install doesn't do that. The scripts are there in my top-level "bin" directory of course, but npx doesn't find them. Obviously I could just run them directly, but it would be nice if I could run the same shell commands to run them in other installation directories and in the project home directory.
I don't want to install the package globally; I want to keep everything local, at least for now. So can I get npm to make those symlinks for me, or should I just bite the bullet and do it myself?
Here's my package.json:
{
"name": "#foo/plazadb",
"version": "0.1.0",
"description": "Basic database setup with upload/download from CSV",
"main": "lib/plazadb.js",
"author": "Pointy",
"license": "ISC",
"dependencies": {
"arg": "^5.0.1",
"cls-hooked": "^4.2.2",
"csv-parser": "^3.0.0",
"csv-stringify": "^6.0.5",
"neat-csv": "^7.0.0",
"pg": "^8.7.*",
"sequelize": "^6.16.*"
},
"bin": {
"conntest": "./bin/conntest.js",
"download": "./bin/download.js",
"upload": "./bin/upload.js"
}
}
The "bin" files all exist (of course, otherwise they would never work). What I'm trying (out of ignorance) is a simple npm install from the project directory.
One way to do is by
specifying a local prefix by using npm config set prefix <path> or one of these methods linked here.
generating symlinks using npm link
As a word of caution, to undo npm link executing npm unlink . may not work. Instead one may have to run npm uninstall -g ..

Error installing package using npm instead of yarn

I have created git repository which will be used as npm package in other project. Lets say that sharable repository name is genesis-service-broker.
I am using this shareable repository inside one of the service(activation service). In this project, I am installing package using yarn. Its running perfectly fine here.
"dependencies": {
...
"genesis-service-broker": "git+https://${key}:x-oauth-basic#git.my_project.com/${user}/genesis-service-broker.git#create_service_broker_for_genesis",
...
}
When I am trying to use genesis-service-broker package inside onother service(partner service) its not able to install the dependencies. In this project, I am installing dependencies using npm. If I install dependencies using yarn its working perfectly fine.
I am not getting any errors in npm install command. I am just not able to find genesis-service-broker folder inside node_modules, when I am installing dependencies using npm.
package.json file inside genesis-service-broker repository. (for reference purposes)
{
"name": "service-broker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git#git.my_project.com:amol.barewar/service-broker.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"config": "^3.2.5",
"form-data": "^3.0.0",
"node-fetch": "^2.6.0",
"request": "^2.88.0",
"uuid": "^3.4.0"
}
}
there is a difference in behaviour here, between yarn and npm
yarn add retains the name of the git project in dependencies, and creates a folder with the same name in node_modules.
So, yarn add git+https://${key}:x-oauth-basic#git.my_project.com/${user}/genesis-service-broker.git#create_service_broker_for_genesis will lead to a module getting installed as node_modules/genesis-service-broker
On the other side, npm install gets the name from the name property in package.json; and it will lead to module getting added as node_modules/service-broker in your case... and also the dependencies map will be like
"dependencies": {
...
"service-broker": "git+https://${key}:x-oauth-basic#git.my_project.com/${user}/genesis-service-broker.git#create_service_broker_for_genesis",
...
}
Because of this difference, the requires(...) might fail.
As, in this case, with yarn that module will be available through -
require('genesis-service-broker')
And for npm through -
require('service-broker')
So, all in all, it will help to keep the name property in package.json same as the project name.

Is packaging an Electron-builder via npm (not yarn) possible?

I have create a python3 app using the Electron environment (itself based on Node.js).
For this app, the dependencies have been installed using npm and my app launches using the npm start command.
Following the documentation of Electron, it seems that Electron-builder is the best option for distributing my app for all platforms. Unfortunately, the installation guide recommends using yarn, not npm as npm might not be working properly : see related ticket
Now my question is: As the ticket is rather old (2017), is it now possible to make it work with npm ? So yes how ?
If it is not possible to make it work with npm, how do I transition my code to yarn ?
For info:
The reason I ask is because I get a BIIIIIG error message when I run the command npm run dist.
If needed I can edit the post to include it (alongside my package.json file)
Thanks in advance.
EDIT: adding error and package.json
{
"name": "APP_NAME",
"version": "1.0.0",
"description": "The first version of APP_NAME's GUI.",
"main": "index.js",
"homepage": "https://docenhance.com",
"author": "DocEnhance <nootaku#gmail.com>",
"license": "MIT",
"scripts": {
"postinstall": "electron-builder install-app-deps",
"start": "npm install && electron .",
"pack": "electron-builder --dir",
"dist": "electron-builder"
},
"build": {
"appId": "com.docenhance.APP_NAME",
"productName": "APP_NAME - PROJECT_NAME",
"linux": {
"target": "deb",
"icon": "build/icon.icns",
"category": "Office"
}
},
"devDependencies": {
"bootstrap": "^4.1.2",
"electron": "^4.0.7",
"electron-builder": "^20.39.0",
"jquery": "^3.3.1"
},
"dependencies": {
"bootstrap": "^4.3.1",
"jquery": "^3.3.1",
"popper.js": "^1.14.7",
"python-shell": "^1.0.7"
}
}
The error file can be found HERE.
Yes, you can use npm. The yargs issue is explained here.
Your error messages tell you . . . what the errors are.
error output:[ERROR] Unknown input file format:
/home/MYUSERNAME/Documents/APP_DIRECTORY/dist/.icon-set/icon_ICN#.jp2
Known file formats are *.j2k, *.jp2, *.jpc or *.jpt
I don't use or work on Linux so I don't know for sure if this is the case, but you are specifying a .icns file format for your icon. That is an OSX icon file format. Your error message says that Electron Builder doesn't know what to do with that (on Linux).
"linux": {
"target": "deb",
"icon": "build/icon.icns",
"category": "Office"
}
The rest of the scary error text likely just cascades from that.

Error when building my electron app

I'm stuck with a problem when building my Node JS/electron app with electron-builder. It works perfectly when running npm start. But, when I execute the command build -w (for windows) it fails with this log.
Here is my JSON file:
{
"name": "Munshiiii",
"version": "1.0.0",
"description": "This is a short Description of the project",
"main": "index.js",
"scripts": {
"start": "electron .",
"dist": "build -w"
},
"author": "Hicham Dbiza",
"license": "ISC",
"devDependencies": {
"electron": "1.7.8",
"electron-prebuilt": "^1.4.13"
},
"dependencies": {
"asar": "^0.13.0",
"cradle": "^0.7.1",
"fs": "0.0.1-security",
"git": "^0.1.5",
"jquery": "^3.2.1",
"jsdom": "^11.3.0",
"loke-ipc": "^1.0.5",
"mongodb": "^2.2.33",
"node-couchdb": "^1.2.0",
"pouchdb": "^6.3.4",
"pouchdb-replication-stream": "^1.2.9",
"scanner.js": "^1.0.0"
},
"build":{
"appId": "com.hicham.dbiza.faizan",
"win":{
"target": "nsis",
"icon": "build/Munshiiii.ico"
}
}
}
for this project Im using:
fs
pouchdb
electron
jquery
....
I already used electron-packager and it works almost fine with one problem: See this picture, which means all links inside the js files (e.g: fs.readFileSync('./assets/state','utf8')) won't work.
I have also added some native js click and keypress listeners... could that be a problem?
My electron version is 1.7.8.
I appreciate your help.
Yarn is strongly recommended instead of npm.
yarn add electron-builder --dev
if you are using Npm
do just simple steps terminal:
1 npm install yarn -g
2 yarn
3 yarn pack
Read this Blog (reactJS in electronApp with .exe file)
Have you installed electron-builder? You don't have it in your package.json. Then I would propose to use the electron-builder command, as recommended by the authors.
Run npm install electron-builder --save-dev and change your dist command to run just electron-builder. Since electron-builder per default build for the current running OS, it's not necessary to send the -w flag. If you still experience problems, try to set the following env variable to get a better stack trace:
DEBUG=electron-builder,electron-builder:*
Edit after getting more information from the comments:
According to this issue at GitHub your first issue seemed to be caused by permission errors, and was solved with running as administrator.
From electron-builders README:
Yarn is strongly recommended instead of npm.
yarn add electron-builder --dev
Try to remove your node_modules folder and run
npm install yarn -g && yarn && yarn pack
First I installed electron-builder with following line
npm install -g electron-builder
then I created a build folder in the root of my project, containing the .ico file with the app logo. then I wrote following parts in my package.json
"build": {
"appId": "your.app.id",
"productName": "Your final product name"
}
and
"scripts": {
"start": "electron .",
"pack": "build --dir"
}
That was everything I needed. I opend my root folder in the terminal and executed
build -w
That created an folder called dist, with an unpacked version of my app plus an installer for the app.

Not able to install "node_module"s

I am a node.js beginner..!!
I am trying to install node_modules but getting following error.
I am on Windows and using node.js v5.7.0
Also, getting the same error on Mac OS X.
First this is not an error but a warning saying you don't have a package.json file. This file could look like this for a beginning.
{
"name": "project name",
"version": "0.0.1",
"private": true,
"main": "main.js",
"start": "node main.js",
"dependencies": {
"colors": "^1.1.2"
}
}
Check this to why using a package.json file.
Edit
You can also run npm init (suggested by #Nicklas Ridewing)
You need to create a package.json file before you run npm install colors
Just run
npm init
And follow the guide to create one :)
Use npm init to setup you project package.json.
Use npm install <package-name> --save to save it to your dependencies.
Use npm install <package-name> --save-dev to save it to your development dependencies.
Those will be saved in to your package.json which will be your project main configuration file.

Categories