NPM versions during development and production - node.js

From what I have read it is a good idea to use the same version of Node.js throughout the development and production phases of an app.
Is this also true of NPM? It looks as if NVM is keeping specific versions of NPM together with specific versions of Node.js inside the .nvm directory. However, although I can see from the NVM documentation how to make sure you run an app with a particular version of Node.js, I can not see how to make sure that a particular version of NPM is used for a particular app. For example if I run the command npm install package from the root directory of an app I think it will use the default version of NPM not the specific version associated with the Node.js version specified in the .nvmrc file of the app.
Do I need to be consistent in the version of NPM I use during app development and production? If the answer is yes how do I achieve that?

When we start a Project using Nodejs a file named package.json is created automatically and it keeps track of all the dependencies in the ongoing Project. So you do not need to worry about the versions, you just have to start the project and all the dependencies will be taken care of.
Suppose you need to share your codes(like Git) then you just need to share your codes and package.json file that will do the trick.

Related

Using local NPM dependencies in the PATH - is there a tool that does this?

Is there an NPM package/tool that can (automatically) add local NPM packages to the $PATH?
This would represent a local development env that was independent of other projects.
NVM allows us to switch Node.js versions, but that doesn't seem to be enough to create an independent development space for each project. By By putting the locally installed command line tools on the $PATH, and giving precedence to local NPM dependencies, this would allow for us to change their versions without affecting any other project.
NPX does this, which is bundled with NPM:
https://medium.com/#maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b
However, NPX looks like it does far too much.
I just wanted a simple tool that only adds the local executables to the path, if you are within an NPM project, so I wrote GMX:
https://github.com/ORESoftware/gmx

Webpack failed to compile react code after upgrade to node v8 (on Ubuntu 16)

After upgrading from node v7 to node v8, webpack failed to compile my react code.
It said that some node_modules are not found. Note: This issue does not happen on Windows.
I tried several ways from refreshing node cache & npm_modules to double-checking $NODE_PATH, but still failed to make it work. Looks like an compatibility issue of node v8 and webpack (or some ES6 syntax).
Temporarily I reverted back to node v7 to make it work.
Does anyone encounter this issue?
When you install a new Node version then you need to make sure that you install all modules that you want installed globally using that new version (you need to have both node and npm in your PATH with that new version first before any other version of node and npm that you might have in your PATH or this will likely not work as expected). Then you need to rebuild all locally installed modules (you may need to remove node_modules directory in some cases to make sure that everything is built from scratch).
One issue that is quite common when you have multiple Node versions installed from the binary packages (using either binary tarballs or nvm etc.) is that the npm that gets installed has a general shebang line #!/usr/bin/env node instead of a specific path like #!/opt/node-v8.0.0/bin/node which would be the case if you built Node from the sources yourself.
This means that you can sometimes go into trouble by running npm with global path that would use the wrong version of Node, leading to some issues that can be hard to notice.
The least problematic Node installation in my experience is building it from sources, installing in a versioned directory and explicitly adding it to PATH before installing the modules globally, before local npm install and before running the project that you want to be using that specific Node version. That way you can make sure that you're not using any files that could be left overs from older versions, like would be the case with installing under a common /usr/local prefix.
You can take a look at my tutorial for more details and other options to install Node:
https://gist.github.com/rsp/edf756a05b10f25ee305cc98a161876a
In general what you described is usually caused by clashing installations of Node.

How to zip a nodejs server app?

I have to create a zip file of my whole nodejs server app.
I should be able to unzip it and run it, without installing dependencies and apps.
It should not be a binary file.
The dependencies should be flattened.
How to do this thing ?
Generally, a Node.js app has its dependencies installed in the node_modules directory in the project root.
So, after running npm install (or npm install --production), you should be able to zip up the project directory and that should be all you need.
If any of your dependencies in node_modules are native addons, then you will not be able to install them on a different architecture or OS. If there are native addons, you will also want to make sure your target machine has the same version of node installed as the machine where you created the zip file. (It's a good idea anyway, if you can, to make sure the node version on the target machine is the same as the source machine.)
One obvious requirement of the target host if you do as I describe above is that node is already installed there. Not sure if that's OK for your use case or not, but sounds like it probably is?
Thank you, all of you for your help.
I got the solution for my question.
I am using npm pack to pack the nodejs app.
To pack nodejs app with some dependencies like morgan, express we need to use npm bundle it helps to include the other module required for node js app.
With this, we don't need to perform npm install.
We just have to install bundle and then include bundleDependencies field including the name of required module in package.json.
And then perform then run the command npm pack. It will create a tar file just copy this file in other folder and uncompress it and run the server starting file.
The place where you are going to run the nodejs file, there nodejs app should be installed
I think you might be looking for this: https://github.com/nexe/nexe
Nexe is a command-line utility that compiles your Node.js application into a single executable file.

How to set global node modules so that all application can use same

I am new to node, i have made few small application using node, but everytime i have to use npm install for every application which download the required dependencies in node_modules folder. There are many libraries which are common.
I tried installing using npm install express -g but i was not sure how to use this dependency in other application which is in some other folder.
Is there any way i can have only one folder like in D:\Users\User\AppData\Roaming\npm\node_modules from where my all applications can have the module which they need ?
Can anyone let me know how to do the settings for the same ?
Any help would be highly appreciated !!
Every node application that has a package.json has a specific set of rules for using specific versions of it's modules. You can install globally only one version of a specific module, but if you happen to have an application that needs an older / newer version that is not installed globally on your dev environment, then it will fail to work.
The recommended way of using node modules ( packages ) is to have a local directory inside your project, which contains all libraries that the project needs. This practice is everywhere and so you should follow it.
There are some ways to mitigate the slow npm install, though.
There is a new npm-replacement, created and maintained by Facebook, called yarn.
What yarn does is it creates a local cache of all installed packages and then symlinks them to your project folder from your local computer cache. This way the npm install procedure becomes very fast.

Can't build my web application when integrating bootstrap template

I'm totally new to Node.js meteor and all development outside of visual studio.
When I go in the console and add bootstrap like this :
npm install twitter-bootstrap
It gets installed and adds all the bootstrap files in my solution but when I run my application with meteor it says
Process finished with exit code 254
No more information. No errors. If I delete all the bootstrap files, it builds and run just fine. Any idea what might be causing this?
I've tried looking for the exit code meaning but I can't find it for my IDE and I'm a bit clueless as for why simply adding those packages without even referencing them anywhere in the project might cause my application not to run at all.
You can't add npm packages in your project folder like that. It will create a node_modules sub-directory that meteor will treat like any other project folder, i.e., it will interpret all the files in it. That's not what you want. Either do the npm install in a super-directory, or, better yet, use the meteor meteorhacks:npm package (https://atmospherejs.com/meteorhacks/npm):
meteor add meteorhacks:npm
and then add the npm dependency to your packages.json file.
{
"twitter-bootstrap": "2.1.1"
}
But the real question is: why do you need this package? bootstrap3 is already part of the standard meteor packages, i.e., you already have full access to bootstrap, incl. javascript.
You can use atmosphere meteor packages called mizzao:bootstrap-3 by running the commend
meteor add mizzoa:bootstrap-3
Alternatively if you want to use npm packages you must add meteorhacks:npm packages.
meteor add meteorhacks:npm
npm install twitter-bootstrap
You can specify all the required npm packages inside a packages.json file.
{
"gm":"1.16.0",
"twitter":"0.2.12",
"twitter-bootstrap":"2.1.1",
}

Resources