I am developing npm module. I have following index.ts
export * from './src/A/index';
At this moment, in app import statement looks as follow:
import {something} from 'myModule';
I would like to add some extra logic to my module, and organize it like:
export * from './src/A/index';
export * from './src/B/index';
export * from './src/C/index';
What should I write in index.ts to make my module to be used in this way:
import {something} from 'myModule/A'
import {something2} from 'myModule/B'
etc.?
What should I write in index.ts to make my module to be used in this way
Given that you want to use import {something} from 'myModule/A' implies that myModule/A folder should have an index.ts that exports something.
This conflicts with the fact that A/index is actually located in myModule/src/A/index.
Move the file and it should work.
Related
Hi i have something like this:
I would like to import everything in directory (expect index.ts) to index.ts and then export it
I don't want to import everything like this:
import * as Network from './network';
import * as FirewallRule from './firewall-rule';
export { Network, FirewallRule };
Because it could be a lot of imports and export. Is it possible to do this automatically with fs npm module ?
Is it possible for a module to import "itself" by its own name in typescript?
For instance, lets say there is a module my-module with a bunch of test.
Is there a chance to import it inside the tests using import ... from "my-module" instead of using local path (e.g. import ... from "./my-module")?
As I know it can be done using the require, but it seems that import does not support this.
You can use the TypeScript tsconfig paths option e.g.
{
"paths": {
"module": ["src/path/to/module"]
}
}
More
https://www.typescriptlang.org/docs/handbook/module-resolution.html
I'm using Typescript in my Node.js project. So generally I'm using import * as bla from 'bla';. Our app is served in DigitalOcean and we faced problems in setting up environment variables (tried both ecosystem and .env, none is consistent). Finally we decided to hard code the variables in a env.js file and require it at the beginning of app.ts. After that here is how my app.ts file looks like:
require('./env.js');
.
.
.
import indexRouter from './routes/index';
Interestingly enough, variables defined in env.js were shown undefined in indexRouter. So I've done some console.log. And this is what I got:
here in route
here in env.js
Any idea why env.js was not loaded first? How to solve this?
In TypeScript you can import a module for side-effects only. This is normally discouraged, but seems to match your use case of setting some global state...
Though not recommended practice, some modules set up some global state that can be used by other modules. These modules may not have any exports, or the consumer is not interested in any of their exports. To import these modules, use:
import "./env.js";
I'm new with TypeScript. I'm currently learning NodeJS Loopback 4 framework which use Typescript language. And my question is how to import some function, class which has been exported in JS file into my TS file. After search several way but it still doesn't work with me.
Here's example:
// /src/index.ts
import {plus} from './lib/test';
console.log(plus(1,2));
// /src/lib/test.js
export function plus(x, y) {
return x + y;
}
I also try using definition typescript like this
// /src/lib/test.d.ts
export declare function plus(x: number, y: number): number;
But still got error when import this function in index.ts file
Error: Cannot find module './lib/test'
at Function.Module._resolveFilename (module.js:543:15)
It looks like the tsconfig.json doesn't have 'allowJs' enabled because it exports declarations.
Is there a reason you are not wanting this to be a typescript file? If you change test.js to test.ts, by making it a .ts should allow it to be recognised in your index file.
UPDATE
Full chat history to get to this point can be found here.
Repository used to test found here
OK so easy solution as #maaz-syed-adeeb mentions will work:
import { path } from './lib/test.js'
The reason the extension is important is because the definition file takes priority over a javascript file in a typescript environment. That is why the module import was blowing up.
To avoid specifying the .js extension you can also setup your directory structure like this:
src
|- index.ts
|- lib
|- test.js
|- test.d.ts
|- index.[js|ts]
in ./lib/index file export all from test:
//./src/lib/index.[js|ts]
export * from './test'
and then import all from lib:
// ./src/index.ts
import { path } from './lib'
// or
import { path } from './lib/test.js'
If you are using a blend of javascript and typescript (say you are moving over to typescript with an existing code base), you will need to update your tsconfig.json to include so you don't get the warnings in your IDE:
{
"compilerOptions": {
"allowJs": true,
"declaration": false
}
}
This is so your javascript files will be transpiled to your destination directory along with the typescript files.
Is it possible to import from something other that what "main" points to?
In my library that is installed into node_modules, I have the main set to
lib/index.js
so (using es2015 imports - source was ts compiled js), I can do
import { FunctionA, FunctionB } from 'MyTestLibrary';
This works because these functions are exported inside of index.js under libs.
I also have an index inside a directory which exports functionC and functionD, the structure is here
/lib/otherdir/index.js
so if I do an import like so
import { FunctionC, FunctionD } from 'MyTestLibrary/otherdir';
my IDE does not complain but running the application I get a
Cannot find module MyTestLibrary/otherdir
Everything is exported as it should be.
You can access the directory directly like this:
import { FunctionC, FunctionD } from 'MyTestLibrary/lib/otherdir'