Cross platform support Sharp Node.JS - node.js

I was having trouble using Sharp (npm i sharp) in two different computers, a Mac and a Linux.
I was getting the following error:
Error:
Something went wrong installing the "sharp" module
Cannot find module '../build/Release/sharp-darwin-x64.node'
Possible solutions:
- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
- Install for the current darwin-x64 runtime: "npm install --platform=darwin --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install
at Object.<anonymous>
The cross platform topic in the official documentation (https://sharp.pixelplumbing.com/install#cross-platform) didn't help. Tried to follow what they suggested, but kept getting the same error.

What did I do to solve it?
The Sharp package was originally installed in the Linux computer.
So, to add support to MacOS:
In the MacOS computer, ran npm remove sharp
In the MacOS computer, ran npm i sharp
That left me with 10 changed files: 5 of them removing Linux support, 5 of them adding MacOS support. I then discarded the removal of the 5 Linux files, and kept the addition of the 5 MacOS files, as the following image:
This way, I ended with support in the two platforms.
Hope this helps someone who needs it.

Related

How to get node-sqlite3 working on Mac M1?

I'm using Rosetta 2 with Homebrew and have sqlite3 installed.
I added these to my ~/.zshrc so that the node compiler can find the brew installs:
export PATH="/usr/local/opt/sqlite/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/sqlite/lib"
export CPPFLAGS="-I/usr/local/opt/sqlite/include"
I'm using installing using npm install sqlite3, building from source with --build-from-source flag, I'm also specifying the homebrew version of sqlite with --sqlite=/usr/local/opt/sqlite/
node-gyp goes to its fallback build node-pre-gyp install --fallback-to-build
After installation, I'm rebuilding it's native dependencies with electron-builder and install-app-deps
It even rebuilds to the platform and arch I'm looking for, darwin and x64. Which is promising.
• electron-builder version=23.0.3
• loaded parent configuration preset=react-cra
• rebuilding native dependencies dependencies=sqlite3#5.0.8 platform=darwin arch=x64
However, when run the app with npm run dev which runs concurrently " cross-env BROWSER=none npm start" "wait-on http://localhost:3000 && electron ."
It still is trying to find the arm64 version of it:
Error: Cannot find module '[..]/node_modules/sqlite3/lib/binding/napi-v6-darwin-unknown-arm64/node_sqlite3.node'
Even though I can confirm, it did build the x64 version because I have one located at:
/napi-v6-darwin-unknown-x64/node_sqlite3.node
Any help to get this working would be greatly appreciated, thanks!
After 4 days of digging, I have finally gotten it to work!!!
For anyone that might stumble on this:
The reason why sqlite3 won't behave is threefold:
When node sqlite3 is installed using npm install sqlite3, it fetches all dependencies and installs it. It then fetches the precompiled binary binding file for the target arch and platform. In my case we would want napi-v6-darwin-unknown-arm64 for ARM64 and darwin for Apple M1. There is no precompiled binary available yet for this Apple ARM64 and even if there is, the next paragraph will detail why it still won't work.
The problem is that it determines the the system's platform and architecture using the binary compiling package node-pre-gyp and this very savior of a Github issue details how node-pre-gyp is not handling ARM architecture detection properly and basically mixing everything up. Because it's not detecting properly, even if we build our own binding with --build-from-source when installing, it still won't work because it is compiling the wrong binding file for the wrong architecture. To make matters worse, if we don't use --build-from-source, it just simply fetches the Intel precompiled binding file. napi-v6-darwin-unknown-x64
Now for some reason, during runtime, it now detects the architecture properly and tries to look for the ARM64 version of the binding file, it won't find it and will throw the feared error 'module not found' for napi-v6-darwin-unknown-arm64. It obviously won't find it and even if it does, it will throw wrong architecture error because we have the Intel version on board napi-v6-darwin-unknown-x64.
So finally after digging at this for 4 days, here's how I got it working:
Uninstall sqlite3: npm uninstall sqlite3
Install a fresh sqlite3, build it from source, specify the target arch and use fallback build with node-pre-gyp just to be safe: npm install sqlite3 --build-from-source --target_arch=arm64 --fallback-to-build
Now the correct binding file is compiled for the correct platform and architecture, on runtime it will find it and will run!

Truffle failing to install on a Mac M1

I am trying to install Truffle on my mac M1.
I started by following the instructions on the truffle web site in the Pet Shop tutorial I got node installed node successfully, but then I get this error...
sudo npm install -g truffle
...
npm ERR! xcrun: error: unable to load libxcrun (dlopen(/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib, 0x0005): tried: '/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/lib/libxcrun.dylib' (no such file)).
I did a bit of research and was pointed at trying it in homebrew instead. I had to update homebrew first because of my M1 chip, which I did successfully. But I still get what appears to be the same or similar error...
/opt/homebrew/bin/brew install truffle
...
xcrun: error: unable to load libxcrun (dlopen(/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib, 0x0005): tried: '/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/lib/libxcrun.dylib' (no such file)).
truffle: The x86_64 architecture is required for this software.
I can't find out much about this online.
Does this mean that truffle only runs on intel chips and I need to use something like rosetta?
OK, so I finally figured this out and thought I would post here for posterity.
The issue was the version of the xcode command line tools that I had installed on my M1 Mac. I have no idea how, but it was an out of date version. The fix was to remove the command line tools and reinstall them. I think (I cannot say for certain) that this replaced them with M1/Arm versions and then the compile tools needed were in place.
To update I followed the steps in this Apple Developer Forum post
https://developer.apple.com/forums/thread/694283
Basically...
junk the existing folder
sudo rm -rf /Library/Developer/CommandLineTools
re-install
sudo Xcode-select --install
After doing this I re-ran
sudo npm install -g truffle
and it completed successfully (albeit with a bunch of audit warnings)

Argon2 is difficult to get working with Angular 8 on MacOS: actually not working at all

I am working with:
MacOS Mojave
Angular 8
node v12.12.0
npm v6.13.4
and trying to make Argon2 to work in my Angular 8 app.
In order to use Argon2 it is required to install gcc and node-gyp globally.
I did install them as indicated on the npm pages of Argon2. GCC v9 was installed. But, I had repeatedly problems executing:
CXX=g++-9 npm install
I kept getting errors about stdlib++ . I tried using Apple's CLang++ and got a successful build with:
CXX=clang++ npm install
I imported argon2 in my angular project in order to do password hashing and verification. Hashing seems to be working well (no errors) but as soon as I import argon2.verify() and use it in my code (by making a verify-call) I will get an error when trying to execute npm run start. Just trying to start the httpserver secured (ssl/tls) with corresponding private key and certificate.
The error I get is some mysterious error:
ERROR in ./node_modules/node-gyp-build/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/[username-deleted]/WebstormProjects/my-app/node_modules/node-gyp-build'
I have seen comments and complaints on probably similar problems on internet and people referring to some adjustment in a Webpack config file but at this point I dont have this config file in my project. I tried other versions of node-gyp-build but not resolved.
Apparently argon2.hash() (its usage) is working without causing problems, but as soon as I introduce argon2.verify() in my code and run my app with npm run start I get the above error.
Any one with similar problem and how this problem was resolved?
I had the same issue and fixed it by installing the Xcode Command Line Tools:
xcode-select --install
npm upgrade
npm install argon2 --save-dev
Hope it helps.
Installing a new version (0.28.0 and up) fixed it for me

Upgrade from node6.x to node8.x gives "node-saas" binding errors

On windows 64 bit platform, upgrade from Node6.x to Node8.x gave the following
Error: Missing binding C:\bisbor1\src\main\webapp\node_modules\node-sass\vendor\win32-x64-57\binding.node
Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 8.x
It also asks to build the node-saas library for which it is mandatory to install Python. For a front end developer, it seems a bit bizarre to install Python to run a simple "Hello Angular" app. And funnily, i don't even need node-saas. Its not a direct dependency for my application.
After breaking my head over installing Python to reinstalling "node_modules" several times (never mind the "download" of the internet that 'npm install' demands), I finally found peace with the following solution.
"Downloaded the appropriate platform specific binding file (note the version of binding in the error thrown) from the git repository
https://github.com/sass/node-sass/releases
And updated the node-sass binding in my npm cache which is located at C:\Users\bisbor1\AppData\Roaming\npm\node_modules\ng-packagr\node_modules\node-sass\vendor\win32-x64-57"
The Primary step is to ensure that you are having Python 2.7 installed, because node-gyp only supports python2, as of writing the post.
To do so:
check whether Python Version is 2.7 or lower using python --version
downgrade using sudo apt-get install python
then rebuild node-sass using npm rebuild-node-sass
If everything else is correct, then a npm start would launch your App.

NodeJs: no response to NPM install

I'm trying to install the express framework for node.js. However, whenever i run "sudo npm install express", i never get a response. It just sits there. Im seriously frustrated at this point.
Using npm 1.0.22 on OSX Lion
I had this issue on lion as well. It was because Lion removed xcode from my path and at some point, the express install triggered gcc.
Try this to pinpoint your problem, it will turn on verbose npm output and is a great way to debug npm packages:
npm config set loglevel info
If your problem does turn out to be a lack of gcc, first see if it's already installed in: /Developer/usr/bin and if not, use the 'app store' to reinstall.
Here's more info on the gcc issue: http://www.quora.com/What-is-the-path-for-gcc-in-Mac-OS-X-Lion
You may be running node v0.5.5-pre which doesn't fall within the required version numbers for the npm package. I found some "answers" here.
To fix I downloaded the 0.4.11 tar.gz from the node.js main page and did a manual build/install: (./configure ; sudo make install) and ran npm install express again, which worked fine this time.

Resources