Meteor Shell, can't import files from any folder: Error: Cannot find module ‘/imports/api/donuts/collection.js’ - node.js

I am having some trouble importing modules into the meteor shell.
Simple example:
1.create new project (meteor create myproject)
2.create file /imports/api/donuts/collection.js and paste content:
// file: /imports/api/donuts/collection.js
import { Mongo } from 'meteor/mongo';
const Donuts = new Mongo.Collection('donuts');
export default Donuts;
3.Run meteor shell and import the file by:
import Donuts from '/imports/api/donuts/collection.js'
than this error hits up:
Error: Cannot find module '/imports/api/donuts/collection.js'
at Function.require.resolve (packages/modules-runtime.js:129:19)
at Module.resolve (packages/modules-runtime.js:81:25)
at Module.Mp.import (/home/ec2-user/.meteor/packages/modules/.0.7.7.mccaq7++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:61:29)
at repl:1:-37
at packages/shell-server/shell-server.js:458:25
at /home/ec2-user/.meteor/packages/promise/.0.8.8.i94065++os+web.browser+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:32:39
What's wrong? File permissions are ok, I start the meteor shell from project root.
Thanks!

Meteor originally loaded all of the source files using its default load order.
In more recent versions (circa v1.3), it treats special directories differently. One of those directories is imports.
Any directory named imports/ is not loaded anywhere and files must be imported using import.
(from the Meteor docs)
When using the shell, you can only import resources that were included in the build. If the module (file) you are trying to import is not included in your import tree (chain of imports starting somewhere outside of the /imports directory), it will not be available for import.

Related

How to import a module from parent directory within a subdirectory

I was working on a simple project, bike_rentals. I thought all was well until I began writing test files for the project.
I created a new directory and name it TestBikeRental and saved it within the BikeRentals directory, which contained the scripts for my project. I created the file init.py within the BikeRentals and TestBikeRentals directory to make them each be a package. Within the top directory is the script run_script.py that I use to run the modules\scripts contained in the package BikeRentals.
Directory structure
The problem comes up when I try to import the py script db_connection (underlined in yellow), within the Test_bike_rentals.py.
This is how tried to imported db_connection.
Importing db_connection
After numerous and numerous attempts, the errors that I kept receiving were these two:
from BikeRentals.BikeRentals.db_connection import Connect
ModuleNotFoundError: No module named 'BikeRentals'
from..db connection import Connect
ImportError: attempted relative import with no known parent package
To view the sourcecode in github repo:
https://github.com/Brownred/Python-and-SQL
I tried to import a module but got an error after numerous attempts. I was expecting no error when it comes to importing a module.

PyDev: Python: Unresolved Import or ImportError: attempted relative import with no known parent package

tl;dr How should I correctly use relative modules in PyDev?
If I use:
from . import myModule
I get the following error:
ImportError: attempted relative import with no known parent package
However...
If I just use:
import myModule
the project will run, but PyDev flags the line and file with
Unresolved Import: myModule
The package has an __init__.py although its contents are empty
Edit: screenshot of PyDev Explorer structure added as requested by #Fabio Zadrozny
PyDev Explorer
The problem is that it depends on how you run the script... by default PyDev will run the file directly, but in this case you want to run the file with -m <module-name>.
PyDev can do that automatically if you go to PyDev > Run and enable the related setting:
p.s.: either you have to use the relative import and launch with -m or you can just use the full import (which in this particular case would be from pkg_BootMeUPY import in_constrains_ini as const -- in which case it'd work regardless of how you launch the file).

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?

PYTHONPATH prevents import from current directory

I a Python project, I import code from a module that resides in the 'current' directory, in ./DIR/module.py:
import DIR.module
This worked perfectly until I had the idea to place common modules in a central location. To do this, I defined PYTHONPATH and moved these modules there.
export PYTHONPATH=/path/to/repository
I now am able to import from this location, but the import from the 'current' directory no longer works. It therefore seems that PYTHONPATH prevents this type of 'local' import.
How can I add this extra directory without breaking import relative from where the python code file resides? In other words, I'd like to still be able to write import DIR.module alongside with imports from the new repository.
You could have overwritten the default pythonpath.
You could try the solution from here:
https://askubuntu.com/questions/250929/pythonpath-environment-variable
export PYTHONPATH=$PYTHONPATH:/home/dev/python-files

Typescript import ts file from node module

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".

Resources