Install local node package from file system. - node.js

I have created Angular library with reference from https://medium.com/#SirMaxxx/angular-6-creating-a-shareable-control-library-6a27f0ebe5c2.
Now my dist/ars-coponent library is stored in my file system at "D:\arsprojects" location. But I have changed destination of package and stored it in root directory i.e. "D:\". Now I changed package.json and set package path as
"ars-components": "file:../dist/ars-components",
But it isnt working and throws error as package.json not found.
I want to have this package as global and it should be possible to use this anywhere on my file system.
Any solutions?

Related

Module installed from npm can't find it's local files

I published a small package to npm which consists of several files.
The index.js of the package just imports and re exports the public classes and APIs/
When I install the package from NPM I get the following error when I try to use the custom package:
Error: Cannot find module './openapi'
Require stack:
- .../node_modules/.../dist/index.js
I checked dist folder in the module dir in node_modules and the files are there.
I assume there is an issue with the relative path inside the node_modules
One option to fix this is to bundle everything into a single file, but I would prefer to keep multiple files.
What options are there to fix this ?
.gitignore files are ignored. There were after all some ignored local files that needed a to be pushed.

Node Package not accessible in Sails.js

I installed node package via npm install and the Data is located in the node_modules directory as own directory.
But if re-lift and afterwards access a function of this new module, the browser told me, function not defined.
Do I need to add the path of the .js file in the layout or anywhere else?

Accessing cli global files with NodeJS

I'm developing a CLI module that can be installed either locally or globally. It references local templates to be used during the CLI process (e.g. copy some of them in the current folder, ...).
How can I retrieve the path where those templates are installed (in the current node_modules folder or in the global npm/yarn node_modules folder) to use them?
So the scenario is that a user installs your tool via npm install, and then that tool copy some templates -- from your project folder -- into the user current working directory (among other things).
So your template files may be installed in a local node_modules installation or in the global one.
In both cases, you can access the absolute path to a resource given a relative path from your script file, via the path module:
const pathToResource = path.resolve('../templates/myTemplate.txt');
pathToResource will be the absolute path and will change from some users to others, depending upon they installed the module locally or globally.
I don't know if it's the best solution but I manage to retrieve the binary path by doing : path.resolve(require.resolve('my-cli'), '../', 'templates')
Source: https://github.com/nodejs/help/issues/388#issuecomment-264731257

node.js require function not finding module

I have a server.js file that I downloaded from someone's website. The first line is: var express=require('express');
When I try to run this server with "node server.js" I get the following error: "Cannot find module 'express'." The express module is installed in the default node install location:
C:\Users\myname\node_modules\express\
I'm able to successfully run express by executing "node express.js" from the express install location in node_modules. I also tried copying over the express folder and file into my c:\node-testing\ directory where my server.js file is located but I still get the error. Any idea what the problem might be and how to fix?
You can set the NODE_PATH environment variable to tell nodejs to search other paths for globally installed modules that are not in the project directory.
See http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders for details.
On Unix installations there are some built-in default locations, but on Windows, it appears you have to set this environment variable manually to support a global location.
FYI, if you want require to load a module from the project directory, then you have to use
require("./filename");
with the ./ in front of it. That's why it didn't work when you copied it to the project directory. node makes a distinction between loading from the project directory vs. loading from the node_modules directory below and thus requires a different syntax to specify which one you want. Express.js is also not a stand-alone module because it depends on a bunch of other modules so you could not copy only it. I'd recommend using the NODE_PATH option or install express into your project directory (it will end up in a node_modules sub-directory).
Node.js will only search for modules in from the current (and parent) directories. Unlike npm, Node has no concept of "global" modules.
You need to run npm install to install your modules into the directory containing your code.

How can I test my module locally using a symlink?

I am trying to run a module that I am developing on my local machine. So I have create a test nodejs application and within this test application I created the 'node_modules' director. The folder structure looks like it would if I had used npm install and specified my module as a dependency. Within the node_modules folder I have create a symlink to the directory where my module under test is.
When I attempt to run my test application node complains: Cannot find module 'my_module'.
I can figure out why this wouldn't work. Can this be done in this way?
Thanks!
The recommended way for doing this is:
In your my_module directory, do npm link .. npm will tell you it has made my_module available for linking.
Then, in your test_app directory, do npm link my_module. npm will now make my_module available to your app.
Note that you can still get a a "cannot find module" error this way, but this is then most likely because your my_module module is structured wrongly. By default, Node.js will look for an index.js file in the module's root directory. Otherwise, you need to specify a main entry in the module's package.json, containing the path to your main js file, relative to the module's root directory.

Resources