I developed an application in NodeJS which use external npm which are not published on npm.
I've actually separated my code in components, so the app have this structure:
/app
index.js
/components
/componentA
index.js
/componentB
index.js
so essentially when I publish the app on my server, I did:
npm i
npm i ../components/componentA
npm i ../components/componentB
as you can see this could be problematic if the app is a big project and have hundreds of components. So my question is, how can I execute a script that check automatically if the components are installed and automatically install it?
I want avoid service like bit.dev or something like. I also used npm link but unfortunately when I install a new package my own npm are automatically removed.
If you have a package.json in your project, you can run npm i -S ../components/componentA which will add it as a dependency in your package.json.
Your package.json should look like
"dependencies": {
"componentA": "file:../components/componentA",
"componentB": "file:../components/componentB"
}
On server just run npm i to get these components installed
Related
I was installing node by using the command on cmd npm install and I got following message
enter image description here
Please help me that how I can solve this issue.
npm stands for node package manager which is used to install external packages or npm modules or libraries hosted on NPM. To get started with a Node JS Project you will need index.js and hit npm init, this will initialise your Node JS Project with auto genreated package.json.
Majorly Node JS, React JS or Angular contains following files:
index.js/server.js(Entry File)
package.json (modules/packages/libraries used in project and script can be written to run your project in different envs)
node_modules (which is auto generated and contains all installed modules listed in package.json)
According to your error message you don't have a package.json file so you need to initialize npm before installing node in your directory.
Initialize it with npm init.
Npm documentation
Step 1- First install Node from https://nodejs.org/en/download/.
Step 2- Now go to your project file cd project.
Step 3- Type the command npm init in console.
Step 5- Now you can see package.json file in your project.
Finish- Now you can install any package by using "npm install packageName" command.
I want to install a package called tone so I can use this code
//create a synth and connect it to the master output (your speakers)
var synth = new Tone.Synth().toMaster();
//play a middle 'C' for the duration of an 8th note
synth.triggerAttackRelease("C4", "8n");
Here's what I've tried so far (as well as completely reinstalling Node):
C:\Users\HP\Desktop\tone-js>npm install tone
C:\Users\HP
`-- tone#0.8.0
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\HP\package.json'
npm WARN HP No description
npm WARN HP No repository field.
npm WARN HP No README data
npm WARN HP No license field.
So I'm guessing I need to create a package.json file. I've tried npm init, and it asks for a point of entry, which I don't understand.
I just want to be able to use the code above. I probably need to add a require statement at the top - what should it be please?
I've also tried installing tone globally with npm install tone -g but don't know how to require the module. I'm also guessing local is better practice.
Also, why when I run npm install tone is it looking for package.json at 'C:\Users\HP\package.json' when I'm in a different folder?
All very confusing! Any help appreciated.
When you run npm init, the entry file it is asking for is the main file of your application. For instance, if the starting point of your application is in app.js then your entry file would be app.js. By default, it will be set to index.js if you don't provide one.
Another trick to using npm init is, if you don't want to setup the package.json to be specific to your project at this point in time, use the -f flag which will force the use of all defaults for your package.json.
Without initializing npm in your project you cannot save any npm packages since there is no package.json which is mandatory to save installed packages.
There are a few things you should know when using Node.js and third party modules.
The package.json file describes your project. It lists all modules you've installed with the --save tag (so npm install --save tone)
You should also know that the package.json file is used to tell others who the author of the project is, optionally what dependencies it has during development (devDepenencies, mainly used for testing modules etc) and NPM uses it when you publish your own NPM package)
To use a module, you need to require it and assign it's exported object to a variable (const Tone = require('tone');)
The entry point is the same file you call node on, so if you normally execute node app.js, your entry point is app.js. It's for NPM to know which file it make public when you've published a module. (Tip: You can now also run npm start instead of node app.js
For quick tests, just executing npm install note still allows you to require('tone'); without having a package.json
I was wondering how to include the Bitcoin JS Library in my electron project. I tried running npm install inside the project directory and creating a var to use it but it does not work for some reason.
Do you have a package.json in your project directory? If so, does it have Bitcoin JS inside the dependencies:? If not, run npm init and fill out the fields that it asks for or just hit enter several time to leave them blank. After that you should have a package.json file. Now run npm install bitcoinjs-lib --save. You should now have BitcoinJS in your node_modules folder and it should be included as a dependency in your package.json.
Firstly, I understand that since version 4 (express), the boilerplate HTML generator has been splintered off. However, does executing the command...
npm install -g express-generator
... and not...
npm install -g express
... automatically install the core express engine as well? I am asking this because, surprisingly, I was only instructed to install the first.
Secondly, an online tutorial instructs something similar to the following...
npm install -g express-generator
express --ejs pineapple-news
cd pineapple-news
npm install
However, on the contrary, I found that the official express documentation recommended this instead...
mkdir pineapple-news
cd pineapple-news
npm init
... hitting return for all the defaults and then ending with...
npm install express --save
As a newbie, I need to understand the differences between the two approaches and the reason for the differences. Any pointers would be highly appreciated.
Let me explain the two cases.
First case :
npm install -g express-generator
express --ejs pineapple-news
cd pineapple-news
npm install
Here, with the first line, you're installing express-generator globally. That is, after installing it globally, you can use it like any other program that can be invoked from the terminal. In the second line, you are invoking the express generator and specifying ejs as the template engine and pineapple-news as the project name. So, the express generator creates a folder named pineapple-express with the necessary sub folders, a default app.js file and a default package.json file. The package.json file thus created will have all the dependecies including express and ejs, the template engine to be used listed in it. Then, when you execute npm intall ,all the dependencies will be installed automatically.
Second case:
mkdir pineapple-news
cd pineapple-news
npm init
With the first and second line, you're creating a folder with the name as pineapple-express and entering inside it. The npm init command creates the package.json file after prompting you to specify some parameters such as project name, version, repository etc. The package.json file thus created will not have any dependencies listed in it. When you do npm install express --save, it will add express as a dependency in the package.json file and install express in the pineapple-express folder. I hope things are clear now.
In first case, all the necessary folder structure and and app.js file with the all the necessary settings to start an express application is also generated. It contains the configuration related to template engine, static file path, cookies configuration etc. Some of the dependencies like morgan, jade template engine etc are also installed by the express-generator.
In the second case, you will have to create the app.js file and write the configurations manually. This includes creating the express app.
The app.js(you can name it anything like server.js or engine.js) file contains the configuration for starting a node js server. This file is executed with node to start the server.
npm install -g install the package globally on your system. express-generator is a module that setup a directory to contain your express app. Creates a directory structure and adds a package.json file with express as a dependency plus other common used modules. So you just can start to write your app. Express-generator install express just locally on your app directory. that's why you have to run npm install on the created directory after creating the directory with express myapp
As I started to develop my first nodejs express application, I added many packages with npm.
I would like to know if there is a way to generate a package.json file containing all the current dependencies or the list of the current packages under the nodes_modules directory.
Just run npm init in the same directory as your project.
You'll be asked a list of questions (name, version, description, etc.) and once complete, it will generate a package.json file with all of the dependencies as currently installed in the node_modules directory.
Run npm list to see what you have installed. Run npm shrinkwrap to build a npm-shrinkwrap.json file, which you can use as a starting reference to build a proper package.json. My workflow is always to update package.json and then run npm install. I never run npm install foo to get some package because it creates risk of forgetting to add it to package.json and then having your application fail to start on deployment.
Updated to add: These days I do run npm install --save foo or npm install --save-dev foo since I have now decided the ~0.4.3 version numbers it adds to package.json are better than my former preference for 0.4.x since the ~ gives you a more precise minimum version number.