Is NPM dependent on the OS of a computer? - node.js

Is it possible to copy a set of NPM installed files and associated files from a Mac computer to a Windows computer, and for all those files to work?
For example, transfering Node.js files with some other NPM files from Mac to Windows, then running node app.js in that directory (on the Windows Command Prompt).
Thanks! :)

The binary, npm, that you install is platform dependent, as is node.js. That's why there are different releases for each platform available on the download site.
For the most part, your project files are platform independent. For example, most of your JavaScript files will all be used by node.js and work just fine without having to worry about what platform you are on because the system details will be dealt with by node.js itself.
However, some modules are platform dependent. For example, anything that uses node-gyp will try to compile on your platform whenever the module is installed by npm. You do not have to worry about that though because it is handled by npm, that's why you're using a package manager.
Copying node_modules can be done; but it's more often than not better and easier to just run npm i on whatever machine is going to be running your application. You can avoid having to worry about version problems using something like npm shrinkwrap which will lock down the version of a package that your module depends on.

NPM packages that contain native addons/dependencies are tied to the OS + NodeJS version and have to be rebuilt specifically for the system you intend to use them on. This is why you're seeing the error mentioning bson.node, it is a native addon that has to be rebuilt, this can be done with the npm rebuild command.

Related

Install mongo by CLI and for a project only inside project directory

Is there a way to install MongoDB only inside a directory through some sort of a script?
An idea is to use it as a dependency in a node.js project. After npm install on the project, npn would download mongo binaries, and install them into project's folder. Later this binaries could be accessible from node.js script which would control them like start the database. This should run on standard platforms (Win, Linux, Unix) and should be hidden from user.
I found a way for unattended install. But this would be installed for whole system and I'm not sure what changes It'll make to windows. Also this is platform dependent solution. I'd welcome some sort of unified node, like npm package.
This runs MongoDB servers, and is used for driver testing.
You'd need to download the binary which you could do via m. You could also do this by hand.

node-v59-linux-x64/grpc_node.node is missing

I am trying to use Firebase admin SDK in my server. When I deploy I get the error I am missing file node-v59-linux-x64/grpc_node.node in firebase-admin node_module map. I added "grpc": "1.7.1" in my package, but I still do not get that file after NPM update. I get an older version, node-v57. I also checked this path https://registry.npmjs.org/grpc/-/grpc-1.7.1.tgz, but I could not locate the file. I deleted my node_modules map and ran npm install again, still no node-v59.
How/where can I download that file? Is there any one who can put the file here so I can manually add it?
Error: Cannot find module
'/data/app/node_modules/grpc/src/node/extension_binary/node-v59-linux-x64/grpc_node.node'
This kind of problem is usually caused by installing the library on one system, then deploying and running it on a different system that requires a different binary file.
The simplest solution to this problem is to run npm rebuild after deployment on the system you deployed on.
Alternatively, if npm rebuild is not an option, you can pre-install the binary for the system you are deploying on by running npm install with some extra options. The --target argument allows you to install for a different version of Node. An argument of --target=9.0.0 installs the binary for Node 9 (only the major version has to match). The --target_platform argument allows you to install for a specific operating system: windows, linux, or darwin (Mac). The --target_arch argument lets you install for a different processor architecture: ia32, x64, or arm. And finally, the --target_libc argument lets you select binaries built for a different libc: glibc or musl (for Alpine Linux).
So, in your case, you should be able to get that binary by running
npm install --target=9.0.0 --target_platform=linux --target_arch=x64
I had the same problem. You can download the file here: https://storage.googleapis.com/grpc-precompiled-binaries/node/grpc/v1.7.1/node-v59-linux-x64.tar.gz
This helped in my case, based on #murgatroid99’s answer:
npm rebuild --target=8.1.0 --target_platform=linux --target_arch=x64 --target_libc=glibc --update-binary
It downloads the required binary to your node_modules/grpc directory.
I run macOS X on my dev machine and I’m deploying to AWS Lambda; this keeps both runtime versions installed, which means I can develop and test locally and then deploy to Lambda.

How do I force the download of Windows modules

I am in a unique situation where I can run npm install only on a linux based machine even though I plan to run my electron/node application on an offline Windows machine.
So, is there a way to tell npm to perform an install and "trick" it to npm install the windows version of each module?
I understand most modules are based on javascript and are not native, but a few are dependent on operating system, such as electron itself.
Perhaps I could modify any header information npm sends out which tells the servers which operating system I am running?
So, is there a way to tell npm to perform an install and "trick" it to
npm install the windows version of each module?
No. If you want NPM to install for windows, then you have to run NPM on windows.
For pure Javascript modules, you could "probably" install for Linux and then just copy the directories over to windows, but you'd have to know that these were pure Javascript modules that contained no native code and did not use any binaries or compile any native code and copying directories behind the back of NPM is asking for it to be confused about what you have.
It sounds like perhaps you should explore fixing whatever issue is keeping you from running NPM on your windows box directly.

If a node module is written in C, and I install it on Mac, do I have to reinstall it to deploy to Linux?

I frequently hear from experienced node developers that checking in node_modules is good practice. But most developers write on Mac/Darwin x64, but deploy on Linux x64.
If a node module happens to be written in C, and I install it on OS X, wouldn't I have to rebuild it on Linux?
The answer is: it depends on the package
Most packages do require reinstall, as node gyp the compiler does not cross compile by default - thanks #tkone.
Some packages like node-sass dynamically download prebuilt binaries for the relevant platform (previously node-sass used to include binaries for all platforms, however this has changed recently).
The now current node-sass 3.4.2 doesn't include the binaries in the originally downloaded npm package (as found in the npm package cache under ~/.npm).
What it does is that its install.js script will download the platform specific binary and store it under vendor/{platform}-{arch}-{process.versions.module}.node. Once installed, the install.js script normally isn't invoked again by npm.
So when you check in node_modules, it will contain the binary only for your initial platform.
For the fun of it I moved away the downloaded binary, which should be the same as someone else checking out your node_modules on a different platform. When then run, node-sass is so smart to detect that the required binary doesn't exist. It exits gracefully, recommending to run npm rebuild node-sass (also hinting that this is usually necessary when you change the node version). This will download the binary for the current platform, and then all is fine.
However, that's totally specific to node-sass, other packages with binaries may behave entirely different.
(See Node.js release versions for an explanation of process.versions.modules aka. NODE_MODULES_VERSION that determines the last number in the binary download URL. The binary name is constructed in function getBinaryName() in ~/.npm/node-sass/{version}/package.tgz/lib/extension.js )

How to compile lesscss using node.js

Hi,
I have finished to code my website using lesscss client side and now want to compile less so I have donwloaded node.js. My website is running on localhost and I want first to know:
where I have to install node.js
what I have to do next ( commands lines tools, commands lines etc. ).
If someone can help me because I'm a newbie in this field.
Thanks.
You can install the LESS compiler directly from npm.
Install node.js. Go to this page and download the installer for your platform.
If you're on Windows, download the .msi; if you are on OSX, download the .pkg file. Whenever possible, download the 64-bit version (unless your system is running only 32-bit hardware and software). If you are on Linux and you want to use package managers, see this page.
Once you have node.js installed, you should also have npm, which is node.js Package Manager. You can open a terminal/console and run npm -v to make sure everything is installed correctly.
Eventually, you can install the LESS compiler by simply executing:
npm install -g less
(note: on OSX and Linux you may need to run this with sudo: sudo npm install -g less).
The LESS compiler will then be available as the lessc command. See examples here.
PS: Some GUIs also exist for simplifying working with lessc. Google "less gui windows/mac/linux" to see many results, like this one for Mac.
it's not mentioned what to do after installing node.js and installing less compiler on your node.
Go to your folder where you are hosting your project locally and then type styles. less styles.css. This should initiate the conversion from less to CSS at the node command prompt.

Resources