Typescript module import with configuration file - node.js

I would to like forge the path to access node_modules with some configuration file. It seems it's not possible to do the thing below, how should I forge my path then?
The aim is to write absolute path to some node_modules ( cause some files are splitted so this is needed).
import someFileSettings from "./../../models/someFileSettings";
import * as request from JSON.stringify(someFileSettings .somePathIneed+"request");

No, it's not possible, because TypeScript modules follow the standard ES6 modules. To do that, there is a module loader API, based on promises.
Here is the explanation from the book of Dr. Axel Rauschmayer:
16.9.1 Can I use a variable to specify from which module I want to import?
The import statement is completely static: its module specifier is always fixed. If you want to dynamically determine what module to load, you need to use the programmatic loader API:
const moduleSpecifier = 'module_' + Math.random();
System.import(moduleSpecifier)
.then(the_module => {
// Use the_module
})
... but, notice this warning:
The module loader API is not part of the ES6 standard

Related

How to determine the module system from a node js library code for importing config file

I'm working on adding support for .js config file in a NodeJS library. NodeJS support two module system - ESM & CommonJS - and since javascript files with .js can be treated as an ES Module or a Common JS module based on the what is defined as type parameter in package.json, I'm not able to figure out, how to know that what module system is a user of the library is using. Based on the module system used by the user only I can decide whether to import config using require() or import()
There isn't a good way too know about the module type of the application code form a node module. One workaround that I can think of would be using something like parent-package-json –
import getParent from 'parent-package-json';
const parent = getParent();
const parentModuleType = parent ? parent.type : DEFAULT_MODULE_TYPE;

Default import index.js file

Hi, i have the following ES6 statement in a .js file(within vue.js project that uses webpack):
import my_value from 'path_to_folder1/folder2'; //after `from` keyword, I don't include index.js.
The above statement will be run in this first two steps:
step 1) webpack transpiles ES6 import syntax to require syntax,
for example:
import my_value from 'path_to_folder1/folder2';
will be transpiled to:
var my_value = require('path_to_folder1/folder2');
step 2) node.js will search and load (by default) an index.js file from folder2 folder.
step 3) is a node.js feature to default search and load index.js file, from a folder, in a require statement? is there any official documentation on this?
Can you tell me if first two steps are correct?
Then, I would like an answer for the step 3.
It's important. Thanks
Node.js will first check if the require's parameter is a directory or a file.
If:
it's a directory -> load index.js
if it's a file:
if the specified file exists -> load the file
if the specified file does not exist, add the extension .js to it and try to load. Often this is the approach you actually want to go with. Since you might have some TS files and then they are compiled to js... the compiler will figure this thing for you
Now, no, this is NOT necessarily how your import will be compiled. It depends on the configuration of your compiler. By default however, you're looking for the default export of the module. So by default if your import looks like this:
import my_value from 'path_to_folder1/folder2';
then it is actually interpreted as
const {default: my_value} = require('path_to_folder1/folder2');
now, if your path_to_folder1/folder2/index.js specifies export default blabla you're all set. If it doesn't, depending on the configuration of your transpiler you may end up with an error. If your transpiler allows default imports and your module does not have any default export, it will import an object containing all exports.

How do I import a library into node without a Typescript/TSD definition?

I'm trying to use a session helper called connect-session-knex which is obscure enough that it does not have a published typescript definition. So when I try to compile my typescript node project, I get the error,
error TS2307 Cannot find module 'connect-session-knex'
Is there a way to ignore TS for this module only? How do I import it without the TSD? I know knex has a tsd, but the wrapper does not. I'm asking this from a generic standpoint of what to do with libraries without type definitions.
For anyone looking: Compiling typescript when it does not have tsd. Missing tsd. Without tsd.
error TS2307 Cannot find module 'connect-session-knex'
Is there a way to ignore TS for this module only? How do I import it without the TSD?
Use var/require instead of import/require. i.e.
var csk = require('connect-session-knex');
Note you should have node.d.ts included for require to be declared.
Also : https://basarat.gitbooks.io/typescript/content/docs/node/nodejs.html
Another suggestion is to start you own .d.ts file as an empty definition file and export the module. Then if you want to get intellisense on the module you can add definitions to it.
e.g. connect-session-knex.d.ts:
// declare module
declare module "connect-session-knex" {
}

Import a javascript module as dynamic using typescript

I want to import a normal javascript module (e.g. vhost) into my node.js typescript file using CommonJS.
I can do this with the following line:
import vhost = require('vhost')
We assume that I can't find a .d.ts file on the internet, but I also don't want to write it by myself, so I just use the vhost variable without intellisense.
The compiler complains and complains:
How can I tell that I just want it to be 'dynamic' (like the C# dynamic keyword or 'var' in normal javascript) and use all of the things in the picture above?
I could create a vhost.d.ts file, but I don't know what to write in there:
declare module 'vash' {
// what to write here?
}
I found this out while typing the question, it was so easy that it is almost embarrassing, but maybe somebody has this problem too.
Just use var instead of import:

Use __dirname into class compiled in Typescript AMD

I'm looking for a solution to use __dirname (or equivalent) inside a TypeScript class compiled in AMD, __dirname doesn't exists there. And because it's typescript, I can't (or I don't know how) import module that contains uri, allowing me to get the uri.
I found the solution once, but I don't remember how and I can't find it again so far.
I took a look to this, but it's in pure javascript and I use typescript, I tried to import module, but I get a TS error.
https://stackoverflow.com/questions/9027429/how-to-use-nodejs-global-module-objects-in-requirejs-modules
One solution is to:
In app.js, create a global var:
__basePath = __dirname;
Then, in the AMD script, use __basepath + 'relativePathFromBasePath' to load the file.
I know I found another solution but I cannot remember how.
I do not use TypeScript, but on the basis of the TypeScript code I've seen and on the basis of what I know of RequireJS, I believe you should be able to access module.uri like this:
import module = require("module");
console.log(module.uri);
The name module is special. You could also use require.toUrl(<module name>) (that's Url as in URL whereas the variable above is uri as in URI) but it does not seem as useful as module.uri for this task. For one thing you'd have to include the module's name in the call.
module.uri may contain .., so it needs cleaning up. I understand your code to be running server-side, so in Node.js we'd call path.dirname(path.normalize(module.uri)).

Resources