Typescript Definitions from Node Modules - node.js

I am trying to configure typescript with my tsconfig.json so it does find definitions inside node_modules.
Ive read that it should work if i set the module to commonjs despite excluding node_modules.
I tried to remove the exclude. Tried to include manually but nothing works.
How to configure typescript to find definition files installed with the source npm package?

Typescript#2.0 (Recommended)
You may use npm install. Example:
npm install --save #types/<your package name>
Typescript#1.x
Use tsd package: https://github.com/DefinitelyTyped/tsd
I also have node.js + typescript#2.0 + mongodb + passportjs example right there: https://github.com/thanhtruong0315/typescript-express-passportjs.

Related

Loading module installed directly from github repo with npm

I installed a public github repo using the syntax provided in the repository's readme:
npm install https://github.com/user_name/proj_name.git
However, I'm not sure how to load this module into my js code. I've tried:
const my_lib = 'proj_name';
Unfortunately, this is not working for me. How can I load a module that was installed directly from a github repository?
Currently you're just defining a constant as a string with value 'proj_name'.
To load a module from node_modules, you have to do the following:
npm install <package_name> --save, where --save write the package and version in your dependencies of package.json. You can also use --savedev to write the package in your devDependencies (both optional).
Use const packageName = require('packageName'); in your e.g. app.js to use the package in your code.
See here for more details about npm install in general, specifying-dependencies and here for ecma script require and import.

npm use local modules

I've just installed the babel-cli module locally to my project but when I run babel test.js in the terminal it outputs that the *command babel is uknown*.
What I figured out is that npm is trying to find the module in my globally installed modules instead of my project modules, how can I fix it?
Sorry for the silly question but I'm not finding a solution.
You can either
Use the babel installed in you project - node_modules/.bin/babel test.js
Add a script in your package.json and run it via npm - npm run <script_name>. npm will use the version of babel installed in your project
When you run babel. It looks for global npm directory and can't find it there.
2 ways but one is redundant.
You can link your local path to global npm directory which is
redundant and wont work for the next project. Don't never do this.
Or install it globally. That how npm works for now.
There is a discussion on that. And here is a good article. http://www.joezimjs.com/javascript/no-more-global-npm-packages/

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.

How to get my module to work globally? Is there any config in package.json?

I have a very simple module that I want to use globally. I have already published it to npm as gammarouter-api.
I would like to know how can I test the module globally before publishing to npm (is npm link the answer?)
I noticed that all the modules that I install globally (I have no problems using any third party modules globally) goes to the folder /user/local/lib/node_module BUT my path does not contains this folder but /usr/local/bin, where I can find some files related to the global installed modules.
When I install my module with npm install -g gammarouter-api the folder gammarouter-api is created at /user/local/lib/node_module but nothing goes to /usr/local/bin, thats why its unreachable. Is there any setting/configuration/trick for this to work?
Use npm install . -g in the root of a module to install it globally. See the docs.
The bin property in you package.json is used to specify which executables should be added to the PATH. See the docs.
For example:
{
"name": "my-module",
"bin": "./bin/script.js"
}

cannot run a node.js example

I am trying to run a pre developed example in node.js environment. But i keep on getting the following error:
Cannot find module '/build/default/validation'
Am I missing some module installation from npm here...? I have installed all the other modules like socket-io, websocket, websocket-server, websocket-clien, etc.
-Parag
Check the package.json file for all required modules. Run npm install and npm update to automatically install all required modules and update them.
Usually, npm modules are in a folder *node_modules*, so /build/default/... looks like some custom module that should be shipped with the example.

Resources