Typescript import ts file from node module - node.js

Maybe it's a duplicate but I've searched for an hour and haven't found the answer.
I have a node module named a-module which contains some .ts files (for example a.ts)
I have another node module b-module which has a-module among its dependencies.
I want to import some .ts file from a-module to b-module.
In some file within b-module I write:
import a = require('a-module/a');
console.log(a);
When then I'm trying to compile b-module with tsc, is says
Cannot find external module 'a-module/a'.
What am I doing wrong?
P.S. I have ArcticTypescript plugin for SublimeText, and seems that it is enough intelligent to find a-module/a. Why then tsc doesn't manage to locate my file?
P.P.S My file structure looks like that
b-module/
node_modules/
a-module/
a.ts
b.ts
I'm trying to import a.ts to b.ts.

import a = require('a-module/a');
You need to either use relative paths i.e. ../a-module/a or declare it for TypeScript explicitly i.e. declare module "a-module/a".

Related

Convert old CJS module into ESM and import into TS files

I am wanting to convert an old module, https://github.com/capaj/object-resolve-path, into ESM so I can use it via an import statement, in order to move all my NodeJS Lambda functions to ESM.
I have forked the repo, and changed the 2 main .js files to .mjs, updated the exports, as well as update the main property in package.json to point to the object-resolve-path.mjs file.
In my NodeJS Lambda function, I have then installed the fork via NPM from my private repo, which pulls the new code in.
However, when I try to import the package in my code now, using import * as resolvePath from 'object-resolve-path'; I get an error:
Could not find a declaration file for module 'object-resolve-path'.
What am I missing? The module isn't written in TS, so why is it asking for a declaration file?

How to import module from node_modules?

Inside node_modules there is directory:
data/lib/
With files:
index.js
index.ts
Data.js
Data.ts
How to use this module using import?
I have tried:
import import * as d from 'data/lib';
I says that:
`index.d.ts' is not a module
File `index.d.ts' is empty
You need an index.d.ts beside the index.js. You need to generate a build from your .ts files to be able to import it in other projects. There is a property on tsconfig.json called declaration that you can set to true, and then when you call tsc to generate your build, it will create the .d.ts files automatically for you. Take a look.

ModuleNotFoundError: cannot import local file

I have a module with multiple files structured like this:
/bettermod/
├── __init__.py
├── api.py
├── bettermod.py
├── errors.py
└── loggers.py
From bettermod.py, I'm trying to import two things:
a class called API from api.py
the whole errors.py file
For the first thing, it is quite easy, I just have to do this:
from .api import API
However, for importing the whole errors.py file, I'm encountering a problem; I'm trying to do like this:
from . import errors
which should work, according to this python documentation, but it's raising the following error:
File "/path/to/bettermod/bettermod.py", line 10, in <module>
from . import errors
ModuleNotFoundError: No module named 'bettermod'
Edit: when debugging, I found that __name__ was equal to bettermod.bettermod
From docs:
Note that relative imports are based on the name of the current module.
I cannot tell you what is wrong with certainty, but there is a smell: bettermod package has a bettermod module. You want to do from bettermod.bettermod import MyBetterClass? I doubt it. In Python files ARE namespaces, so choosing your file and directory names is also an API design. Just keep that in mind.
I suspect the problem is masked by the name collision. Try this. If you run python in bettermod directory and say import bettermod you are importing bettermod\bettermod.py. And . is relative to the bettermod.py module. Now try to run python in directory above bettermod package directory. It will work because now . resolves to bettermod package.
Try:
import mymodule
mymodule.__file__
This will tell what mymodule is. For packages, it will show path to __init__.py. This will help you to orient yourself. Also look up PYHTONPATH and how you can use it to make sure you are importing from the right path.

Is it not possible to import modules from another module if they are in the same dir? (Haskell)

To demonstrate my question, I've created a test project like this ->
The directory "Data" has only 2 modules, namely "Test3.hs" and "Test4.hs". They are empty modules for test purposes. (implementation: "module Data.Test3 where" )
"Test1.hs" imports all the modules like this ->
which results in an error ->
I am using WinGHCi to import the modules, which automatically changes the directory to "cd: ~\.hs".
I also tried to import the modules by using GHCi and by manually changing the dir. But ended up with the same result as above.
So I come to conclusion that there is no way of importing your own modules from the same directory and you have to always create sub dirs only for this purpose.
Is that right?
You need to decide where your root directory is, run GHCi from that directory, and then consistently name all your modules relative to that same directory.
You need to name your modules consistently both in the module declaration (module Foo where...) and in the import statements (import Foo).
So you need either:
module Test.Test1 where
import Test.Data.Test3
import Test.Data.Test4
import Test.Test2
or run GHCi from inside Test and remove all the Test prefixes. But you can't have the prefix on some but not others. You have to be consistent everywhere. Each module name is basically a file path from the current directory to where the source file is.

Is there a "default" file name that can be used for importing modules?

What I mean is if there's some standard name that can be used for the primary file in a local Nim module so that when importing by path, we can simply reference the directory?
For example, it seems right now I need to specify both the directory and file name of the local module like this:
import my_module.main
Whereas I was hoping to simply be able to reference the directory if the expected file name was found:
import my_module
Aside from using Nimble or creating a separate --path flag for every module in a nim.cfg file, is there anything that will allow this?
Also, in general, is there a conventional name to use for the main file of a module?
If I have a simple app like this:
myapp--+
|
+--myapp.nim
|
+--sub--+
|
+--sub.nim
In myapp.nim I have to use import sub.sub to load the sub-module. With import sub, I get Error: cannot open sub.
Likewise if I have modules outside the app and set the --path to the parent of those modules, I have to use import my_module.my_module.
The idea is to have a my_module.nim and sub-modules in a my_module directory. Then you can just import my_module.

Resources