Access rights for node_modules folder at MacOS/Linux and Windows - node.js

I'm developing some nodeJS applications on a mac machine. For testing purpose I'm using Parallels to get a virtual windows machine (win 10).
If I'm running npm install for a project on my mac, I can't run the project on the windows machine, as I'm getting an access denied error for the node_modules folder.
So I deleted the folder an run npm install on the windows machine. With that I can run the app. But then on my mac machine I do get e.g. sh: /Users/project/node_modules/.bin/nodemon: Permission denied.
What can I do to set the correct access rights to the node_modules directory to get the app running on both OS?

That is because the binaries (.bin) compiled in macOS need not necessarily work with windows too.
For your scenario, use YARN. Because YARN offers offline install.
Reference link: https://yarnpkg.com/blog/2016/11/24/offline-mirror/
In either of your systems install YARN.
npm install yarn
Inside your project folder in a cmd or a terminal just type yarn
-Yarn will start resolving your packages.
Once done, create a .yarnrc file by executing the following commands (same for both windows and macOS).
yarn config set yarn-offline-mirror ./npm-packages-offline-cache
yarn config set yarn-offline-mirror-pruning true
A .yarnrc file will be created in your home directory (macOS => ~/.yarnrc || windows => C:\{user}\.yarnrc).
Move that file into your project to make it specific only to your project.
Now do a yarn install -> Results in node_modules folder and a yarn.lock file. Also note that in the home directory under the folder npm-packages-offline-cache you will have all the dependencies in tarball format.
All you have to do is Commit this tarball directory and the yarn.lock to a repository common to both the environments, setup yarn in other environment by repeating the same 1-5 steps.
Finally run yarn install -offline, you will have the dependencies loaded.
Long story, Short! You can't just copy paste node_modules and get it working between windows and unix.
Hope it helps you..

Related

Into which folder does npm install installs

I installed an application via npm install and I can open the application in my webbrowser via http://localhost:3000/without any problem. What folder do I have to open to see what is displayed when I open the URL http://localhost:3000/ in my webbrowser?
by default npm install will install packages into node_modules which resides in your project root where package.json is located.
It seems you have a script which will spin up a webserver, in most cases it will not expose the node_modules directory on that webserver (for good reason).
The point is that you should be including the packages you installed within the script you use for your application that is served.
edit: you can read up on how to use nodejs packages here: https://docs.npmjs.com/using-npm-packages-in-your-projects

npm- module not recognized even though it's installed

I am running on windows.
My module is install via npm install. It's located in the node_modules which was locally created at my project.
For some reason, all of a sudden node has stop recognizing my module, even though it is installed and located in the root/node_modules location!!
I have tried every thing I could find online. What is going on???
tried clearing npm's cache
kill node.js process
re-open my command-line
even restarted my PC!!
path is configured correctly
One more thing:
if i manually install the package via: npm install --save my-module
its recognizing my package.
EDIT
I have noticed that when I run npm install it does not create the node_modules/.bin folder as of when I run npm install --save my-module it does create it.
EDIT 2
When manually placing my packages in the .bin folder than it works
Note, that you can also execute an installed npm module using the npx (Node.js Package Runner), which is part of npm.
npx my-module
You should see the module executable in ./node_modules/.bin
Please, at first re-install your node and shutdown your PC for 5 at least mins.
You can follow the following steps:
Search environment variables from start menu's search box.
Click it then go to Environment Variables
Click PATH
click Edit
Click New and try to copy and paste your path for 'bin' folder [find where you installed the node] for example according to my machine 'C:\Program Files\nodejs\node_modules\npm\bin'
If you got any error. try the another step:
Click New, then browse for the 'bin' folder

Installing Node packages globally without internet connection

I am working on Windows Server 2012 R2, but external internet connection is restricted in my Org. So i can not install any NPM packages on server directly.
I want to install pm2 OR forever packages globally so that all the user profiles(admin and users) can access that from any directory.
Please guide me in this case.
From another pc with internet connection, you can download the source code from github. For example, for pm2 it is : https://github.com/Unitech/pm2
After downloading the source code, you need to look into the package.json file and download all the dependency source code too. ( One easy way to do all of above is to use npm to fetch pm2 in a local directory then copy the pm2 along with all dependencies to a USB)
Transfer files into a directory of your server workstation using USB or any other suitable means.
Add the pm2/bin directory to your PATH.
Test with pm2 -v from command line to ensure PATH addition is correct.

Installing npm modules in a VM shared directory and grunt issues

I'm trying to put together a development environment and npm is causing me problems. Here is my scenario:
I have a development machine running Windows and VMWare Player. I have a Ubuntu Server VM (no UI) which is configured with Apache, PHP, NodeJS etc. As the VM has no UI I want to use the host OS for development. I set up a shared directory which in the VM is accessed as /mnt/hgfs/source/<project name>.
The problem comes when I attempt to run npm install within this directory. I see a lot of errors like Error: UNKNOWN, symlink '../requirejs/bin/r.js'. I know that my package.json file is OK because if I copy all files out of the share and into a regular unix directory (/var/www/<project name>) npm install works fine. So npm has a problem installing modules in the shared directory.
I thought I could get around this by installing the node packages globally but, for whatever reason, the GruntJS enthusiasts don't like that and it must be present locally. I then tried to create an npm link from global to local but that just results in a new error: Error: May not delete: /usr/lib/node_modules/grunt. I have full permissions on the /usr/lib/node_modules directory and all sub-directories.
I really don't want to write the entire project using a command-line text editor in the VM but it looks like I cannot have my code-base in a directory available to both the host and guest OS through VMWare.
I would very much appreciate any suggestions on how to either 1) allow npm modules to be installed in my shared directory, 2) run Grunt globally, or 3) solve the npm link error I'm seeing.
EDIT: Shortly after posting this I realised the fundamental issue here - it's not possible to create symbolic links within a VM shared directory when the host OS is Windows. As npm install uses symlinks by default it didn't work, and this is why the accepted solution does work.
Try the following:
npm install --no-bin-links
Grunt should be local since the plugins and gruntfile.js may require a certain version of Grunt in order to run your tasks. If another developer would like to run your tasks, they could just issue an npm install and they are set. (See this for more info.) grunt-cli is global which is used to run the local version of grunt

Building syntaxhightlighter with nodejs in windows 7

I need to build Syntaxhighlighter from here: https://github.com/alexgorbatchev/SyntaxHighlighter
I have read these:
https://npmjs.org/doc/faq.html
Getting started with NODE.JS in windows x86
Get started with Node.js in Windows
Installing node packages / dependencies using npm on Windows Server 2008
It seems that installing Nodejs from here http://nodejs.org/download/ should be enough.
Step 3 in the syntaxhighlighter Building instructions says:
3.Then ./node_modules/.bin/bower install to download dependencies
I have this path on my machine:
C:\Program Files (x86)\nodejs\node_modules\npm\bin
and I can see a bower.json in the syntaxhighlighter zip file.
I have googled how to download dependencies for windows, but I cant find anything.
There is a lot of info here:
How do I get started with Node.js
but I think this is not for windows env.
Can you assist?
EDIT:
Ok, seems to work now.
Installed Git-1.8.4-preview20130916.exe and restarted.
Step 8 ok now.
Step 9 gives "grunt not recognized".
Tried
npm install -g grunt-cli
from here: https://github.com/gruntjs/grunt/issues/774
Now I have a pkg folder
Thank you sir
Install NodeJs for Windows per instructions
Install Git for Windows, select option to Add Git to your path.
Open a new command prompt in Windows. Ideally, open as an Administrator. Do not reuse existing command prompts. The path will only be set in a new command prompt properly.
Download the code for the highlighter or use git to clone
If you got the zip file, unzip it
From the command prompt, change the active directory to be the folder of the code you cloned or unzipped
Type npm install -g grunt-cli, then Enter after each of the following...
Type npm install -g bower
Type npm install
Type bower install
Type grunt build
Done.
You have to install git package in this way:
sudo apt-get install git
It's working fine!

Resources