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.
Related
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 ..
I recently created an NPM package called Project_1 (which I installed in another of my Node.js projects called Project_2) using the command:
npm install --save ./path/to/Project_1
(Is there a better method to install a local package inside one other locally?)
So the packaje.json of Project_2 is as follows:
{
"name": "Project_2",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"dependencies": {
"Project_1": "file:../Project_1",
}
}
In the npm package I created (Project_1), the JSON package is as follows:
{
"name": "Project_1",
"scripts": {
"custom-serve": "http-server ./website --port 8888 -o"
}
}
When I am in the root of Project_1 I can launch from the terminal:
npm run custom-serve
And in this way I can execute my custom command.
I need to know how to call npm run custom-serve inside Project_2 (also from the command line) after installing the Project_1 package as an npm dependency in Project_2.
So, in the root of Project_2 I would like to be able to run npm run custom-serve so that the command written in the Project_1 library is triggered.
How can I do this? How should I set the JSON packages? Is there a best practice for doing this? Do I need to add special scripts in .js?
This is because I have noticed that always when installing npm packages they provide commands that can be launched from the root of the project in which they are installed.
NB: The command custom-serve is just an example. I would like to create some custom commands inside the Project_1 and i want to be able to call them inside the Project_2 after the npm install of the Project_1 package.
I have already tried to create a script inside the Project_2 like so:
"scripts": {
"custom-command": "cd ./node_modules/Project_1 && npm run custom-serve",
}
But it doesn't works.
Firstly, to answer your question if there's a better way to install a local dependency, there is via npm link.
Assuming the name in Project 1's package file is project-1, you can link it in Project 2 as follows (obviously using the paths that correspond to your setup):
cd ~/projects/project-1
npm link
cd ~/projects/project-2
npm link project-1
Secondly, if you want your package to expose a runnable command, you should configure this in the bin section of its package.json. The path should point to an executable file, so to reuse the npm script, you could use execute as follows
custom-command.js
const {exec} = require('child_process');
exec('npm run custom-serve');
And then add the bin section to the package.json of Project 1:
"bin": {
"custom-command": "./custom-command.js",
}
If you have an index file inside your Project_1, and you had Project1 as a dependency for Project2, you can just call the file and it will run the default start command:
In Project_1 package.json file:
{
"name": "Project_1",
"scripts": {
"custom-serve": "http-server ./website --port 8888 -o",
"start": "npm run custom-serve"
}
}
In Project_2 package.json file:
{
"name": "Project_2",
"scripts": {
"custom-command": "node Project_1"
}
}
I'm trying to install my custom made project globally across my system so I can call it anywhere in my computer from the cmd, but it doesn't quite work.
I tried:
npm install -g .
and more but I can't remember. This is my package.json:
...
"name": "nodejs-insta-reddit-cli",
"version": "1.0.0",
"description": "",
"main": "./build/index.js",
"bin": {
"instapostwouter": "./build/index.js"
},
...
The reason the main path is in ./build/index.js is because I'm using typescript.
So my question is: How would I install my project globally so that I can use it on my workstation in any directory?
Use npm link in the directory of local module you want to access globally.
Then you add it using npm link package_name.
OR you can publish the module (Private or Public) and use install it globally and use it.
More information on Npm Link
NOTE: if you are working with cli package then make sure you have a bin folder and entry inside the package.json file.
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.
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.