I understand the command initialises a package.json file in the CWD, I’m just curious what happens behind the scenes if the package.json file already exists.
I couldn’t find it in documentation and didn’t notice anything different when I ran it. Does the code stop once it detects a package.json file to stop overwriting what’s already there?
I'm just trying to learn more about the inner workings of this command.
Running 'npm init' will just reinitialize the package.json based on the new inputs you perform. Therefore, you're doing basically the same you did when first calling 'npm init'
See the npm docs for more information about creating a package.json file.
When you run npm init twice then it's update the package.json file as per your new init data you enter in last npm init call.
see this,
now npm init again
It will run through and overwrite the Stuff you can edit with npm init. The rest stays untouched.
Related
I am quite new to programming and today decided to attempt and create a node.js and puppeteer project with the purpose of scraping website into a .txt file. I ran into issues straight away since for the most part I have no idea what I'm doing. After installing node.js and puppeteer, I was guided by some videos and articles I found to create my first project. In the command prompt using mkdir and later cd I was able to create and access the new directory, but I started running into problems with npm init. It only places the file package.json in the repository, but there isn't a package-lock or node_modules file anywhere. No idea what they do but thought this was a problem. When I open cmd and try to run the app by typing node app.js it returns Error: Cannot find module 'C:\Users\emili\app.js' along with some other gobble. What should I do, to be able to run the simple application I wrote?
It seems that you are missing some key knowledge on how NodeJS works, but in order to fix your issue (for now), you will need to take a few steps.
First, in your working directory (where the package.json is), you'll need to install your modules.
Run npm install puppeteer. This will do two things, create the node_modules folder and create the package-lock.json file.
Create a file named app.js (either manually or by running the command touch app.js) in your working directory, and put the following content inside of it:
console.log('Hello, World!');
Save the changes to app.js and then run node app.js in your terminal. You should see Hello, World! output to the terminal.
The reason npm install puppeteer created the node_modules folder and the package-lock.json file is because they weren't needed beforehand.
When you run npm install PACKAGE_NAME, you're installing a module (otherwise known as a package), thus it creates the node_modules folder so that it will have a place to put the module so that your code can access it. It also creates the package-lock.json file, which is used to track the module versions inside of your project.
With this information, I request you go back to the tutorial you were originally following and try going through it again and attempting to understand each of the core concepts before writing any real code.
I am trying to create a new stenciljs project using the provided command npm init stencil
After running the above command, i get this error
Can someone please help me to find out what exactly is going wrong.
One observation is that after running the command, a new folder named Rohan is created under users directory. Here is my users's directory:
I was able to figure put the solution. Thanks to Thomas for giving me a hint in the comment :)
The issue was related to the npm-cache path. Since i have space in the username, the cache path was not taken properly. I fixed the path by running this command(set the path for the respective folder only):
npm config set cache "C:\Users\Rohan~1\AppData\Roaming"
After this, the command npm init stencil will ran properly and was able to create the project.
npm init -y
I'm using above line to create node package.json file for my node project using Node Package Manager. But I want to know about -y.
What does it do?
If you will run init command with this flag you will get default package.json file. Take a look at documentation.
If the initializer is omitted (by just calling npm init), init will fall back to legacy init behavior. It will ask you a bunch of questions, and then write a package.json for you. It will attempt to make reasonable guesses based on existing fields, dependencies, and options selected. It is strictly additive, so it will keep any fields and values that were already set. You can also use -y/--yes to skip the questionnaire altogether. If you pass --scope, it will create a scoped package.
read doc here. here already mentioned all the things about npm init
npm init doc
I am currently working on a project at a large company, and according to the project I am working on, every time I want to quickstart the app, I would need to first run the command npm install and then run all the additional compiling instructions, but the problem is that running npm install can take a long time, and that is why I am wondering if it is necessary to run this command every time I make a change to the code, and then want to compile and run it.
What exactly does npm install do? If you could explain to me in terms of how we compile and run java code i.e. javac bob.java && java bob and try to make analogies on that basis, that would greatly help me understand the concept. The way I am currently thinking about it right now is that npm install kind of runs like how javac runs, but I am not sure if that is correct. Thanks in advance!
NPM Install
npm install simply reads your package.json file, fetches the packages listed there from (usually) https://www.npmjs.com/, and sometimes engages in the build steps for those packages.
So you only have to run npm install when you change your package.json file, and need to fetch new dependencies.
Keep in mind that npm install --save <packagename> (or npm install -S <packagename>) will update your package.json and run npm install all in one line!
You can view the results of your npm install inside ./node_modules/.
To compare to java
This might be a helpful resource if you're trying to get stuff done: Getting Started with Node.js for the Java Developer
Javascript is not a compiled language, unlike java. When you invoke javac, the java compiler reads in all your .java files, compiles them to java bytecode, and then writes them to .class files, which can then be bundled together into a .jar for execution.
Javascript doesn't do any of this! When you invoke node foo.js, the node executable wakes up, reads foo.js, and gets to work invoking it line by line**. Node does other cool things, including maintaining an event loop (which allows it to operate "asynchronously", and allows it to be very efficient as a webserver-- it doesn't sit around waiting for requests to complete, it carries forward with the next event in the queue.
Node also performs JIT and optimization, these details allow it to improve the performance of sections code it notices are running "hot".
Note also that node.js uses the V8 javascript engine (also used in Google Chrome). Really everything I've said above is handled by V8.
(** Technically there is a syntax checker which is run first, before execution. But this is not a compile step!)
It is not necessary to do "npm install" each time you want to compile. You just need to do it when you change the dependencies of your project.
NPM basically is the package manager for node. It helps with installing various packages and resolving their various dependencies. It greatly helps with your Node development. NPM helps you install the various modules you need for your web development and not just given you a whole bunch of features you might never need.
When you start an app, it comes with a package.json file. That package contains the list of node_modules you are gonna need. Whenever you enter npm install, what you are doing is to download that list of node_modules. So yeah, you have to download the modules all over again.
#NOTE: In your project, you have a file called package.json. this file is responsible for holding track of your project's dependencies. That's why you have to install it every time#.
Is package.json file used by node when the application starts or is it used only by the npm for installing dependencies?
What I really need to know is this: when I start the app using
node myapp
Is the package.json file read or ignored?
package.json is actually used by node itself. Here is the code: https://github.com/joyent/node/blob/master/lib/module.js#L101 Basically, when you require a directory, it checks if the directory has package.json and if does uses file from it's main property.
otherwise package.json is used only in npm, but nothings stops you from reading it in your code.
Of course it reads package.json ! You can define your application starting point (file) that will be called when you type node "appName".
To define that and other parameters (dependencies..etc) type : npm init and follow the console wizard.
You can check this guide : http://package.json.nodejitsu.com/