Get local system home directory path from server - javascript

I have a server running, I want to get the local system file path where aws server is running using node js

Use process.cwd() which is also documented here: https://nodejs.org/api/process.html

This might solve your problem:
__dirname
Check with a console log whether it's what you wanted or not.

There are multiple ways of achieving this. One of them is you can use this package,
app-root-path
Install the package with the command :
npm i -S app-root-path //install the package
Use package where you required :
var appRoot = require('app-root-path'); //require package
console.log(appRoot); //use variable as string
You can import the package and use variable where ever you require inside the app.
You can check this package documentation visiting this link.

Related

Download npm dist package without having to install npm

Is there a way I can download an npm package without having to do a npm view XXX ... or basically just not having to install node/npm? I'm trying to do this on a linux machine.
~EDIT~
I realize I should've added some clarification on what I'm trying to achieve here: all I need are the package's files with dependencies to be served as a static resource on cloudfront. I'm hoping npm provides a way to directly download the artefact like the way maven-central does.
You can access to the raw package using the NPM api.
https://registry.npmjs.org/{package-name}/{version}
The url above will give you a JSON response containing the package metadata. The package property dist.tarball contains an url where the package can be downloaded.
Keep in mind you have to install the dependencies of the package on your own
Example:
https://registry.npmjs.org/lodash/4.17.10
NPM API documentation
Download lib folder from github.com-get all files within it
Put all the files next to your node application in the same folder.
at the top of your file append it like sooo....
var "" = require("./""")

Aurelia js add npm package

I want to use the following npm package https://www.npmjs.com/package/poker-evaluator
It can also be found on github: github.com/chenosaurus/poker-evaluator
On the project folder I ran the following command line, npm install poker-evaluator --save
And it seems that I installed the library -> Package Json
I want to be able to run the functions from this module(poker-evaluator) and I can't insert this module.
tried multiple times and in different ways ...
Unfortunately, the package you are looking at relies on NodeJS APIs, so it cannot be used in the browser. If you look at its source, you'll see: https://github.com/chenosaurus/poker-evaluator/blob/master/lib/PokerEvaluator.js#L1
var fs = require("fs");
var path = require("path");
These are Node APIs that work with files. You'll have to use this package on your server and wrap it with an API that your Aurelia application will talk to.

Node.js, getting cannot find module 'ws' error

I have installed node.js on my Windows-7 PC. I am not able to create websocket connection to a remote server.
I tried to load modeule "ws" in my script :
var WebSocket = require('ws')
It gave an error :
cannot find module 'ws'
So I followed instructions over here : node.js websocket module installed but won't work in scripts
Execute cmd as Administrator (Right click cmd icon-> Run as Administrator) Then type in cmd:
c:\Node Instalation Dir\> npm install -g express
c:\Node Instalation Dir\> npm install websocket --force
Then run my script :--
D:\My Script Folder \> node myscript.js
Again same error. What could be the problem ?
cannot find module 'ws'
If you install websocket, you should require websocket, not ws:
npm install websocket
Then in REPL:
var websocket = require('websocket');
Alternatively, you can use ws module:
npm install ws
Repl/script:
var ws = require('ws');
Take a look at your node_modules directory (only the first level, the dirs right beneath it). You can require each of them by exact name.
require will actually look for a node_modules in current dir, then if not found, then the parent and again parent etc. If it doesn't find it, it will look for modules installed in global path pointed to by NODE_PATH. (And of course, native modules such as http and net.)
Try to install the package locally but not globally, i.e. without the -g option.
Have you installed the module with the -g option? I think that not every module is meant to be installed globally, instead, try installing it locally for the project you are creating (npm install), and check if the error persists. [...]
If you want to just require('something'); it is better to install it locally, otherwise, you have to require('{PREFIX}something'), where prefix is the path to where yo have installed it globally. Check out this blog post , and, as it says, generally the rule of thumb is to install things locally if you are going to use them in your app, and globally if you are going to use them from the command line.
node error cannot find module already installed.
Go to your project root directory and open the package.json file and edit
Add "type":"module";
Go to the top of your js file, delete the require and use the import statement to import 'ws' into your script.
I was also facing the same issue.
Error 1:
Just simply trying npm install ws to the same folder level where I had my server.js worked for me.
Error 2:
If you are getting the error WebSocket is not defined, then try the command npm install ws, after adding the below code. if ws doesn't work then try npm install websocket
Also, add this in your file:
const WebSocket = require('ws');
var conn = new WebSocket('ws://localhost:9090');

'require is not defined' in Meteor.js when including NPM package

I'm trying to use a npm package from Meteor.js (Release 0.6.6.3) using Meteor.require. However it throws an error saying that require is not defined. Why is this and how can we solve it?
mrt add npm
npm install github
packages.json
{
"github": "0.1.8"
}
github.js
var GITHUB = Meteor.require('github');
Error
ReferenceError: require is not defined
The npm package has lines such as
var https = require('https')
var url = require('url')
var crypto = require('crypto')
Must the package's code be manually edited to use Npm.require? Editing them manually got rid of the errors.
However theres a line:
module.exports = SOMETHING
How should we call module from within meteor?
Meteor.require is a function added by the meteor npm smart package, which actually doesn't do much for using npm other than wrapping some asynchronous callbacks. It's a few months old, so you might want to try using Meteor's Npm.require directly in case something broke.
The monkey-patching of the Meteor global by this package is misleading.
Making comments above an answer.
Is Meteor.require() a typo? That is what is in your code though your question text refers to the correct Npm.require().
I think module.exports is there for non-meteor use of the same file. Within meteor variables for export should be
declared as globals inside the package
exported with api.export() within the package.js file.
The documentation on this is a bit rough but look at namespacing and writing packages. Also looking into the various meteor packages on github is very useful.
Use Npm.require() in meteor.
Like this:
var fs = Npm.require("fs");
For that you need to have a Meteor package: meteorhacks:npm , npm-container
Make sure you're using the meteor-npm package.
https://www.npmjs.com/package/meteor-npm

can not find module express --on windows

I use "npm install -g express" on windows console.but when I try to "node app.js", it shows me the error"can not find module express",I had set the environment variable"NODE_PATH",but nothing happen ,I need your help,Thank you!
Globally installed modules aren't accessible without full path. You need to install express in your project directory or it parents. Check out documentation about module loading.
npm allows two options on how to install a module: locally and globally.
A global installation (done using npm install -g xyz) is for providing some tooling system-wide. Related to express this provides the global express bootstrapper that you can use to create an initial frame for your app by simply typing: express .. If you need help on what you can do with this command, check out its help parameter: express --help.
In contrast, a local installation of a module provides this module for a specific app. A local installation is always made to an app's node_modules folder. When you try to require a module, Node.js searches the this folder for the requested module.
Hence, it is perfectly fine to have express installed multiple times: Once globally for the bootstrapper, multiple times locally (once per app).
So, to cut a long story short: To make your app run, install express locally using npm install express and that's it :-).

Categories

Resources