How to run NPM using a specific Node interpreter? - node.js

I have an Electron project that uses native libraries, some of which have prebuilds that get downloaded during npm install. This grabs the prebuilds for the global Node version running NPM, which isn't quite what I need. Is there a way to execute NPM explicitly using the Electron binary in my project?

Related

How npm packages are run in browser without node?

I am using rusty_v8 to create a local runtime in my cargo project and I want to integrate an npm package into the v8 runtime I am using.
since npm packages need to node runtime. and node runtime is based on v8 correct me if I am wrong. how can I make it possible to run npm packages in my v8 runtime? or if I ask another way how does google chrome's v8 use node packages without using node in its runtime.

What does Prebuilt node builds mean?

I am trying to install the this node-printer npm package. There is a pre built node build section under which there is:
npm install printer --target_arch=ia32
npm install printer --target_arch=x64
Googling pre build node built did not hep me much. Searching for arget_arch=ia32 took me electron github issue page but I could not understand what it meant either
target_arch option seems to be coming from node-pre-gyp which node-printer runs as install script after it is installed.
By "prebuilt", that means you won't have to build the C++ code yourself. It is pre-built (== built in advance) by the node-printer package.

node printer.node is not a valid win32 application

I have developed a node API for my angular application, My node application uses node-printer package for printing pdf files generated by node, when i tried to run my application using nodemon i am getting an error
node printer.node is not a valid win32 application
The same application is working on the other machine without any error. both the machines are of X64 bit architecture.
also i have also tried to install node js 32 bit then too i am getting same error.
This worked for me, installing printer as such:
npm install printer --build-from-source
After so many attempts i have deleted node_modules folder from an application and fire an npm install command to add all modules again that resolves the problem.
Hi there let me go through what's happened on my site here in 2022.
So if you're going to use npm install printer for your Electron application, you're using a native module as deemed by electron.
Since Electron has a different application binary interface (ABI) from a given Node.js binary that you used to install your printer . Therefore, we need to rebuild the native module (in this case printer) for Electron.
For more detail check this out.
First, install electron-rebuild for your project. npm i electron-rebuild.
Second, install a native module using e.g. npm i printer
Third, after installed native module, execute ./node_modules/.bin/electron-rebuild to rebuild the native module.

What is the difference between node.js runtime and npm package manager options while installing node.JS?

I am trying to install node.js by downloading the .exe file, I am confused and stuck on the Node.js setup where in it asks to install node.js runtime or npm package manager so I want to proceed through the installation after knowing fully the difference between the two.
My question is what is the difference between node.js runtime and npm
pacakage manager and what are all the features do I get on the two
options.
My basic purpose of installing node.js is to compile Typescript, Please help me to understand the features of the two package
First of all, it does not ask you to install Node.js runtime OR npm package manager, it offers you to install them both (if you want)
Now, Node.js runtime is basically what will understand your javascript code and execute it to produce a result.
Npm package manager is a tool which will allow you to install third party libraries (other people's code) by using the command line.
npm install express
will install the framework called express for example.
Node JS
Node.js is a platform built on Chrome's JavaScript runtime for easily
building fast, scalable network applications.
Real-Time services (Chat, Games etc)
NPM
Npm is a package manager. Typically this software is installed to
build Node applications.
It let's you install software (libraries, plugins, frameworks and
applications).
Node.js or Node is an open-source, cross-platform, JavaScript runtime environment(JSRE) that executes JavaScript code outside of a web browser.
npm is a package manager(like Nuget package manager in .NET -Microsoft ) for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js.
You can differentiate them by finding their version using below code.
node --version
npm --version
node is a framework that can run JavaScript code on your machine while npm is a package manager. Using npm we can install and remove javascript packages also known as node modules. Now, it is not a rule that you should use npm to install and remove node modules. We can use yarn also. npm and yarn are capable of the following:
Read and understand package.json file
Download and put javascript modules in node_modules folder.
Run scripts mentioned in package.json such as starting a node server, running in dev and production mode, running scripts for unit testing etc.
npm by default run node server.js; if you didn't specify scripts in the package.json
"scripts": {
"start": "node your-script.js"
}
Which means npm run node
if you need use yarn, you'd better choose node.js runtime, usually the default choice is a better one for beginner //TAT

Using NodeJS plugins in Electron

I am new to Electron (Atom-shell), and I am trying to load a NodeJS plugin into the application I am building, but I don't know how. The documentation is not clear on that.
For instance, I am trying to use sqlite3 plugin in my app, I used npm install sqlite3, and it was successfully installed. But the application throws and error when I try to call it var sqlite = require('sqlite3'). Are there any further steps I am not aware of ?
Thanks.
For pure JS (i.e. not native) modules you need the following:
Have the module listed in your package.json dependencies
Let electron know where to find the module (e.g. export NODE_PATH=/PATH/TO/node_module)
The first requirement is obvious and the second has its roots in this issue.
For native node modules (such as sqlite3) which use C++ bindings, you need to build them against electron headers to work. According to electron docs, the easiest way to do that would be:
npm install --save-dev electron-rebuild
# Every time you run npm install, run this
./node_modules/.bin/electron-rebuild
To install the npm modules correctly you should go into the folder of your electron app and install the module via npm.
npm install --save sqlite3
The flag --save is important, because npm will install the module inside your app.
Afterwards the require should work.

Resources