How to deploy a production meteor server in 2015? - node.js

Meteor has changed, and is missing a production deployment in docs.meteor.com.
I have a pretty nice working meteor app in dev mode. So I bundle it with the new command since bundle has been deprecated:
meteor build ./build/ --architecture os.linux.x86_64
On the production server, I install the latest version of nodejs (currently 0.12), copy and decompress the build.
The Mongo DB is on an other server, so I just redefine PORT, ROOT_URL, MONGO_OPLOG_URL and MONGO_URL environment variables.
But quickly end-up with the too frequently seen fibers missing error:
module.js:338
throw err;
^
Error: Cannot find module 'fibers'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
...........
So, here is what I tried:
npm install fibers#1.0.1 -g # but it fails.
npm install fibers -g succeeds and installs the version 1.0.5
Here is the situation:
root#server:~# npm version
{ npm: '2.5.1',
http_parser: '2.3',
modules: '14',
node: '0.12.0',
openssl: '1.0.1l',
uv: '1.0.2',
v8: '3.28.73',
zlib: '1.2.8' }
root#server:~# npm ls -g | grep fibers
├── fibers#1.0.5
root#server:/opt/meteor/authmonitor-src# meteor list-platforms
browser
server
But I still have the same : Error: Cannot find module 'fibers'
Questions:
Is there an up to date manual on how to deploy meteor applications on local production server?
Why / how should I install fibers module, and which version?
export NODE_PATH=/usr/local/lib/node_modules/ partly helped, but after installing with npm install xxx -g the required modules such as underscore and semver, it ends with an other fiber error: "Error: Module did not self-register."
What would you recommend?
Thanks,

I would use Meteor Up which automates lots of things. Here is a video tutorial from Sacha

Is there an up to date manual on how to deploy meteor applications on local production server?
No, there is no official documentation. The community is waiting for MDG to release galaxy, which will be a paid hosting service for meteor.
Why / how should I install fibers module, and which version?
Based on what you wrote, there are a couple of things I see that could be problems:
After you untar the bundle you need to:
$ cd bundle/programs/server && npm install
You should not need to install any node modules globally in order for your app to work.
It's also recommended that you run the version of node appropriate for your meteor version. Have a look at the changelog and search for 'node'. At the time of this writing, the recommended version is 0.10.33.
hosting
If you are hosting somewhere fairly bare-bones like DigitalOcean or EC2, I'd recommend using Meteor Up for your deploys. If you prefer to do the sysadmin tasks yourself, I suggesting reading my related answers here and here.
Another popular hosting choice is modulus, becuase it's more full-service. You can read some tutorials here and here.

Related

This version of Node.js requires NODE_MODULE_VERSION 70 [duplicate]

I am running a node application on terminal. Have recently upgraded to node v8.5.0, but am getting this error:
Error: The module '/tidee/tidee-au/packages/tidee-au-server/node_modules/bcrypt/lib/binding/bcrypt_lib.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 51. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
at Object.Module._extensions..node (module.js:653:18)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Module.require (module.js:568:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/tidee/tidee-au/packages/tidee-au-server/node_modules/bcrypt/bcrypt.js:6:16)
at Module._compile (module.js:624:30)
at Module._extensions..js (module.js:635:10)
at Object.require.extensions.(anonymous function) [as .js] (/tidee/tidee-au/packages/tidee-au-server/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Module.require (module.js:568:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/tidee/tidee-au/packages/tidee-au-server/server/helpers/encryptPass.js:1:16)
Any idea how to solve this?
You need to remove the module folder (bcrypt) from the node_modules folder and reinstall it, use the following commands:
$ rm -rf node_modules/bcrypt
$ npm install
// or
$ yarn
I had the same problem and nothing mentioned here worked for me.
Here is what worked for me:
Require all dependencies you need in the main.js file that is run by electron. (this seemed to be the first important part for me)
Run npm i -D electron-rebuild to add the electron-rebuild package
Remove the node-modules folder, as well as the packages-lock.json file.
Run npm i to install all modules.
Run ./node_modules/.bin/electron-rebuild (.\node_modules\.bin\electron-rebuild.cmd for Windows) to rebuild everything
It is very important to run ./node_modules/.bin/electron-rebuild directly after npm i otherwise it did not work on my mac.
You have to rebuild the package and tell npm to update it's binary too. Try:
npm rebuild bcrypt --update-binary
#robertklep answered a relative question with this command, look.
Only rebuild haven't solved my problem, this works fine in my application.
Simply run:
npm uninstall bcrypt
Followed by:
npm install bcrypt (or npm install, if bcrypt is declared as dependency in your package.json file)
Be sure you only have one version of NodeJS installed. Try these two:
node --version
sudo node --version
I initially installed NodeJS from source, but it was the incorrect version and 'upgraded' to the newest version using nvm, which doesn't remove any previous versions, and only installs the desired version in the /root/.nvm/versions/... directory. So sudo node was still pointing to the previous version, whilst node was pointing to the newer version.
you can see this link
to check your node verison right. using
NODE_MODULE_VERSION 51 means that your node version is nodejs v7.x, requires NODE_MODULE_VERSION 57 means you need upgrade your node to v8.x,so you need to upgrade your node. and then you need run npm rebuild command to rebuild your project
Most likely you have this issue due to the package-lock.json. Somehow it seems to block you from recompiling or rebuilding your dependencies, even if you explicitly run npm rebuild. I ran all the following to fix it for me:
rm package-lock.json;
rm -rf node_modules;
npm install;
I deleted the node_modules folder and run npm install and my application started without any errors.
I got the same error but I was trying to run a node application using a Docker container.
I fixed it by adding a .dockerignore file to ignore the node_modules directory to make sure that when the docker image builds, it builds the native packages for the image I wanted (Alpine) instead of copying over the node_modules compiled for my host (Debian).
Turns out my problem was user-error: make sure the version of node you are using for running is the same that you are using when running an npm install or yarn.
I use NVM for versioning node and was running yarn via a terminal, but my IDE was set to use an older version of node when running and it was throwing the error above. Matching my IDE's version of node in the run config to node --version fixed the issue.
Here is what worked for me. I am using looped-back node module with Electron Js and faced this issue. After trying many things following worked for me.
In your package.json file in the scripts add following lines:
...
"scripts": {
"start": "electron .",
"rebuild": "electron-rebuild"
},
...
And then run following command npm run rebuild
I got this error when running my app with systemd:
ExecStart=/usr/local/bin/node /srv/myapp/server.js
But I was using a different version for npm install in the shell:
$ which node
/home/keith/.nvm/versions/node/v8.9.0/bin/node
If this is your setup, you can either hardcode the node version in the service file or follow a workaround like this one.
I had the same problem and none of these solutions worked and I don't know why, they worked for me in the past for similar problems.
Anyway to solve the problem I've just manually rebuild the package using node-pre-gyp
cd node_modules/bcrypt
node-pre-gyp rebuild
And everything worked as expected.
Hope this helps
you need just run this below commands:
$ rm -rf node_modules
$ rm -rf yarn.lock
$ yarn install
and finally
$ ./node_modules/.bin/electron-rebuild
don't forget to yarn add electron-rebuild if it doesn't exist in your dependencies.
For Electron modules, install electron-rebuild.
Format:
electron-rebuild -o <module_name> -v <electron version>
Example:
electron-rebuild -o myaddon -v 9.0.0-beta.6
Specify the same version that you have installed in the current directory
You might have this experience where a standard node-gyp build would report as 64, then a basic electron-rebuild would report 76, not until you add -v with exact version it bumps to actual version 80 (for 9.0.0-beta.6)
I had a similar problem with robotjs. There were some deprecated code that required node v11, but I had already compiled electron code on v12. So I got basically the same error.
Nothing here worked as I was basically trying to rebuild electron and my other dependencies into node v11 from v12.
Here is what I did (part of this is based on chitzui's answer, credit where credit is due):
Back up package.json
completely delete the node_modules folder
completely delete package_lock.json
delete package.json (will reinit later)
Close any open editors and other cmd windows that are in the project's directory.
run npm init to reinit package, then missing data with old backed up package.json
run npm i
fixed
After trying different things.
This worked.
Delete your node modules folder and run
npm i
I faced the same issue with grpc module and in my case, I was using electron and have set a wrong electron version in the env variable "export npm_config_target=1.2.3", setting it to the electron version I am using resolved the issue on my end. Hope this helps someone who set env variables as given here (https://electronjs.org/docs/tutorial/using-native-node-modules#the-npm-way)
You could remove bcrypt entirely and install bcryptjs. It is ~30% slower, but has no dependencies, so no pains installing it.
npm i -S bcryptjs && npm uninstall -S bcrypt
We've installed it successfully for our applications. We had issues with bcrypt not compiling on AWS instances for Node v8.x
Potentially, inconsistency of the node JS versions is what causes the problem. As stated in the documentation. Be sure to use one of the lts release. E.g. specify this in your Dockerfile:
# Pull lts from docker registry
FROM node:8.12.0
# ...
Check the Node version you're using, might be a mismatch between what it is expected.
I just got this error running kadence the installed "kadence" script checks for nodejs first and only runs node if there is no nodejs. I have the latest version of node linked into my ~/bin directory but nodejs runs an older version that I had forgotten to uninstall but never caused problems until just now.
So people with this problem might check if node and nodejs actually run the same version of node...
In my case, I was in my office proxy which was skipping some of the packages. When I came out of my office proxy and tried to do npm install it worked. Maybe this helps for someone.
But it took me several hours to identify that was the reason.
In my case I was running nodejs instead of node. Due to nodejs being installed by the package manager:
# which node
/home/user/.nvm/versions/node/v11.6.0/bin/node
# which nodejs
/usr/bin/nodejs
run npm config set python python2.7 and run npm install again the party is on.
I have hit this error twice in an electron app and it turned out the problem was that some modules need to be used from the main process rather than the render process. The error occurred using pdf2json and also node-canvas. Moving the code that required those modules from index.htm (the render process) to main.js (the main process) fixed the error and the app rebuilt and ran perfectly. This will not fix the problem in all cases but it is the first thing to check if you are writing an electron app and run into this error.
I came here because I was getting this error for the quokka.js ext in vscode.
My solution:
(on a mac via the terminal)
1- I went to ~/.quokka
2- I ran nano config.json
3- I copied the code from config.json into a separate file
4- I deleted the code in config.json
5- I stopped and restarted Quokka.
6- Once I confirmed that Quokka was working without errors, I deleted the config.json file code.
this is occoures because you currently change your node js version,
just run in terminal in your project
$ rm -rf node_modules/bcrypt
then reinstall
$ npm install
you can start it. ok

Node - was compiled against a different Node.js version using NODE_MODULE_VERSION 51

I am running a node application on terminal. Have recently upgraded to node v8.5.0, but am getting this error:
Error: The module '/tidee/tidee-au/packages/tidee-au-server/node_modules/bcrypt/lib/binding/bcrypt_lib.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 51. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
at Object.Module._extensions..node (module.js:653:18)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Module.require (module.js:568:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/tidee/tidee-au/packages/tidee-au-server/node_modules/bcrypt/bcrypt.js:6:16)
at Module._compile (module.js:624:30)
at Module._extensions..js (module.js:635:10)
at Object.require.extensions.(anonymous function) [as .js] (/tidee/tidee-au/packages/tidee-au-server/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Module.require (module.js:568:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/tidee/tidee-au/packages/tidee-au-server/server/helpers/encryptPass.js:1:16)
Any idea how to solve this?
You need to remove the module folder (bcrypt) from the node_modules folder and reinstall it, use the following commands:
$ rm -rf node_modules/bcrypt
$ npm install
// or
$ yarn
I had the same problem and nothing mentioned here worked for me.
Here is what worked for me:
Require all dependencies you need in the main.js file that is run by electron. (this seemed to be the first important part for me)
Run npm i -D electron-rebuild to add the electron-rebuild package
Remove the node-modules folder, as well as the packages-lock.json file.
Run npm i to install all modules.
Run ./node_modules/.bin/electron-rebuild (.\node_modules\.bin\electron-rebuild.cmd for Windows) to rebuild everything
It is very important to run ./node_modules/.bin/electron-rebuild directly after npm i otherwise it did not work on my mac.
You have to rebuild the package and tell npm to update it's binary too. Try:
npm rebuild bcrypt --update-binary
#robertklep answered a relative question with this command, look.
Only rebuild haven't solved my problem, this works fine in my application.
Simply run:
npm uninstall bcrypt
Followed by:
npm install bcrypt (or npm install, if bcrypt is declared as dependency in your package.json file)
Be sure you only have one version of NodeJS installed. Try these two:
node --version
sudo node --version
I initially installed NodeJS from source, but it was the incorrect version and 'upgraded' to the newest version using nvm, which doesn't remove any previous versions, and only installs the desired version in the /root/.nvm/versions/... directory. So sudo node was still pointing to the previous version, whilst node was pointing to the newer version.
you can see this link
to check your node verison right. using
NODE_MODULE_VERSION 51 means that your node version is nodejs v7.x, requires NODE_MODULE_VERSION 57 means you need upgrade your node to v8.x,so you need to upgrade your node. and then you need run npm rebuild command to rebuild your project
Most likely you have this issue due to the package-lock.json. Somehow it seems to block you from recompiling or rebuilding your dependencies, even if you explicitly run npm rebuild. I ran all the following to fix it for me:
rm package-lock.json;
rm -rf node_modules;
npm install;
I deleted the node_modules folder and run npm install and my application started without any errors.
I got the same error but I was trying to run a node application using a Docker container.
I fixed it by adding a .dockerignore file to ignore the node_modules directory to make sure that when the docker image builds, it builds the native packages for the image I wanted (Alpine) instead of copying over the node_modules compiled for my host (Debian).
Turns out my problem was user-error: make sure the version of node you are using for running is the same that you are using when running an npm install or yarn.
I use NVM for versioning node and was running yarn via a terminal, but my IDE was set to use an older version of node when running and it was throwing the error above. Matching my IDE's version of node in the run config to node --version fixed the issue.
Here is what worked for me. I am using looped-back node module with Electron Js and faced this issue. After trying many things following worked for me.
In your package.json file in the scripts add following lines:
...
"scripts": {
"start": "electron .",
"rebuild": "electron-rebuild"
},
...
And then run following command npm run rebuild
I got this error when running my app with systemd:
ExecStart=/usr/local/bin/node /srv/myapp/server.js
But I was using a different version for npm install in the shell:
$ which node
/home/keith/.nvm/versions/node/v8.9.0/bin/node
If this is your setup, you can either hardcode the node version in the service file or follow a workaround like this one.
I had the same problem and none of these solutions worked and I don't know why, they worked for me in the past for similar problems.
Anyway to solve the problem I've just manually rebuild the package using node-pre-gyp
cd node_modules/bcrypt
node-pre-gyp rebuild
And everything worked as expected.
Hope this helps
you need just run this below commands:
$ rm -rf node_modules
$ rm -rf yarn.lock
$ yarn install
and finally
$ ./node_modules/.bin/electron-rebuild
don't forget to yarn add electron-rebuild if it doesn't exist in your dependencies.
For Electron modules, install electron-rebuild.
Format:
electron-rebuild -o <module_name> -v <electron version>
Example:
electron-rebuild -o myaddon -v 9.0.0-beta.6
Specify the same version that you have installed in the current directory
You might have this experience where a standard node-gyp build would report as 64, then a basic electron-rebuild would report 76, not until you add -v with exact version it bumps to actual version 80 (for 9.0.0-beta.6)
I had a similar problem with robotjs. There were some deprecated code that required node v11, but I had already compiled electron code on v12. So I got basically the same error.
Nothing here worked as I was basically trying to rebuild electron and my other dependencies into node v11 from v12.
Here is what I did (part of this is based on chitzui's answer, credit where credit is due):
Back up package.json
completely delete the node_modules folder
completely delete package_lock.json
delete package.json (will reinit later)
Close any open editors and other cmd windows that are in the project's directory.
run npm init to reinit package, then missing data with old backed up package.json
run npm i
fixed
After trying different things.
This worked.
Delete your node modules folder and run
npm i
I faced the same issue with grpc module and in my case, I was using electron and have set a wrong electron version in the env variable "export npm_config_target=1.2.3", setting it to the electron version I am using resolved the issue on my end. Hope this helps someone who set env variables as given here (https://electronjs.org/docs/tutorial/using-native-node-modules#the-npm-way)
You could remove bcrypt entirely and install bcryptjs. It is ~30% slower, but has no dependencies, so no pains installing it.
npm i -S bcryptjs && npm uninstall -S bcrypt
We've installed it successfully for our applications. We had issues with bcrypt not compiling on AWS instances for Node v8.x
Potentially, inconsistency of the node JS versions is what causes the problem. As stated in the documentation. Be sure to use one of the lts release. E.g. specify this in your Dockerfile:
# Pull lts from docker registry
FROM node:8.12.0
# ...
Check the Node version you're using, might be a mismatch between what it is expected.
I just got this error running kadence the installed "kadence" script checks for nodejs first and only runs node if there is no nodejs. I have the latest version of node linked into my ~/bin directory but nodejs runs an older version that I had forgotten to uninstall but never caused problems until just now.
So people with this problem might check if node and nodejs actually run the same version of node...
In my case, I was in my office proxy which was skipping some of the packages. When I came out of my office proxy and tried to do npm install it worked. Maybe this helps for someone.
But it took me several hours to identify that was the reason.
In my case I was running nodejs instead of node. Due to nodejs being installed by the package manager:
# which node
/home/user/.nvm/versions/node/v11.6.0/bin/node
# which nodejs
/usr/bin/nodejs
run npm config set python python2.7 and run npm install again the party is on.
I have hit this error twice in an electron app and it turned out the problem was that some modules need to be used from the main process rather than the render process. The error occurred using pdf2json and also node-canvas. Moving the code that required those modules from index.htm (the render process) to main.js (the main process) fixed the error and the app rebuilt and ran perfectly. This will not fix the problem in all cases but it is the first thing to check if you are writing an electron app and run into this error.
I came here because I was getting this error for the quokka.js ext in vscode.
My solution:
(on a mac via the terminal)
1- I went to ~/.quokka
2- I ran nano config.json
3- I copied the code from config.json into a separate file
4- I deleted the code in config.json
5- I stopped and restarted Quokka.
6- Once I confirmed that Quokka was working without errors, I deleted the config.json file code.
this is occoures because you currently change your node js version,
just run in terminal in your project
$ rm -rf node_modules/bcrypt
then reinstall
$ npm install
you can start it. ok

Error with 'babel-runtime' while trying to serve a Meteor app

I have recently been getting off the ground with Meteor and React. I followed the tutorial on the Meteor website with no problem.
Then I tried creating another project, trying to follow another tutorial (for an older version of Meteor). Somewhere along the line, Meteor started throwing up obscure errors. After trying to start fresh again, I ended up reinstalling Meteor and it stopped complaining.
However, I'm getting the same problems, again. Specifically, after creating a brand new project:
meteor create myproject
cd myproject
meteor
The server throws up the following error:
[...]
W20161109-03:53:42.862(1)? (STDERR) Error: The babel-runtime npm package could not be found in your node_modules
W20161109-03:53:42.862(1)? (STDERR) directory. Please run the following command to install it:
W20161109-03:53:42.863(1)? (STDERR)
W20161109-03:53:42.863(1)? (STDERR) meteor npm install --save babel-runtime
[...]
I do what I'm told and attempt to install babel-runtime package. The server manages to successfully get off the ground, but it then throws up the following error at run-time:
Uncaught Error: Cannot find module 'babel-runtime/helpers/slicedToArray'
at Function.require.resolve
This is all still working off a freshly created project, with no extra packages installed or code changed. I have tried looking online for solutions but, while there are lots of references to bugs that have been fixed, I haven't found anything. A couple of (questions)[Babel - Error: Cannot find module 'babel-runtime/helpers/typeof' on StackOverflow have suggested the following:
Reinstalling the NPM modules
rm -rf node_modules
meteor npm install
Updating NPM
meteor npm update -g npm
I am using Meteor 1.4.2, NPM 3.10.9, Ubuntu 16.04.1.
I was having the same issue. After a little digging, found this: https://github.com/meteor/meteor/issues/8019
* Installing the `babel-runtime` npm package in your application
`node_modules` directory is now required for most Babel-transformed code
to work, as the Meteor `babel-runtime` package no longer attempts to
provide custom implementations of Babel helper functions.
Consider trying it out by using the latest release candidate (not officially released yet):
meteor update --release 1.4.2.1-rc.1
And then:
meteor npm install --save babel-runtime
After upgrading to 1.4.2.1-rc.1 (and then subsequently upgrading to 1.4.2.1 this morning 11/9), the error was resolved.
Try adding package.json file inside your project directory and then run
meteor
npm install --save babel-runtime
in the command line.
You may have whitespace in your project name. All that you have to do is create another project without whitespace in the name. I know that it's quite difficult to detect the cause from the error message, but it is what it is.
I recently faced this issue with meteor 1.11.1 and the solutions here did not work. Turns out I had whitespaces in my project name (Ghughu Server V2) and all I had to do is create another one without the spaces (GhughuServerV2).
P.S. It works fine even if you have whitespace in the path to your project, you just can't have whitespace in the project name. Also, just renaming the directory doesn't work. Either you have to create a brand new project or change some configuration inside (which seems impractical if you're working in a newly created project).

I've installed meteor - where is npm?

I've installed meteor on my machine:
curl https://install.meteor.com/ | sh
My understanding is that it meteor runs off of node.js and automatically installs it. And node automatically installs npm.
I'm working through the Discover Meteor tutorial and it has me run:
npm install -g mup
But I get the following output:
-bash: npm: command not found
Do I need to run it from a different directory? Or download / install something extra onto my machine. Add it to my PATH?
When you install Meteor, it does not automatically install node && npm. When installing Meteor, it will download something called dev_bundle which has NodeJS and all the NPM modules needed by Meteor. All these modules are pre-compiled for your platform but are not "installed" per-se.
Check out this post if you want to use the node distribubtion included with Meteor: https://meteorhacks.com/how-meteor-uses-node. I suggest you just install Node on your own, though.

var mqtt = require('mqtt'); resulting in error

I have installed 'node.js' and then executed 'npm install mqtt' from 'node.js' command line to install 'mqtt.js'. Now to test 'mqtt client' I am trying to execute :
var mqtt = require('mqtt');
which results in error saying:
"Error: Cannot find module 'mqtt'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at repl:1:12
at REPLServer.self.eval (repl.js:110:21)
at repl.js:249:20
at REPLServer.self.eval (repl.js:122:7)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)"
First, you need to add the MQTT library.
If you have npm package manager installed on the server, you should run npm install mqtt --save
For detailed information: https://www.npmjs.com/package/mqtt
For requiring Node.js module, refer to this tutorial which is pretty detailed.
http://www.bennadel.com/blog/2169-where-does-node-js-and-require-look-for-modules.htm
By the way, createClient() has been deprecated by mqtt module, use connect() instead.
If you ever want to test your MQTT client with a ready online broker, try http://www.robomq.io.
This is a common issue that Node developers face. While working on Unix systems, sometimes it may not allow you to install such packages. For that, you will need sudo permissions. Sometimes, the package is installed but only in your local modules, and when you try to import it from outside of the directory, the error occurs. Sometimes, your compiler read your dependencies, but not able to find this package in that, at that time also you face this error.
Anyways, don't worry. You just have to follow some steps below.
A best practice is to initialize your project using npm init before starting development. This will initialize your project and generate package.json file.
Then, if you want any library as dependencies, try --save with npm install command. This will save your dependency in package.json file.
e.g. npm install mqtt --save
If any package is not found after installing, install it globally by -g flag.
Globally installed packages will be accessible within your system. e.g. npm install mqtt -g.
Note: Unix system needs SUDO permission for installing it globally.
I hope this will help you.
When you use the command line for node.js, it searches for node modules that are installed globally which is usually in the directory /usr/lib/node_modules in Linux machines.
When you run npm install, the node modules are installed locally in the same directory where the node terminal was launched.
If you want to access node modules in the node terminal, you need to run npm install mqtt -g where -g stands for global installation.
Usually Linux machines don't allow normal users to access /usr/lib/node_modules so it'll be better to run it as sudo npm install mqtt -g

Resources