How change path in Node for modules? - node.js

i have node js project.
/home/sergey/Desktop/core/app.mjs
In app.mjs i am import file
import test from '/static/test.mjs'
But when i run node node app.mjs import not found because code search file in /static
how to tell node to search all paths starting from /home/sergey/Desktop/core ?

You should import a relative path, instead of an absolute one:
import test from './static/test.mjs'

Related

"Cannot use import statement outside a module" without "type: module"

I've NodeJS app whit many file like this:
import '#babel/polyfill'
import app from './app'
./app is a js file: app.js and #babel/polyfill is a npm package
When I try to start my app with npm run dev i got this error:
SyntaxError: Cannot use import statement outside a module
I've seen that you can use "type": "module" on package.json to solve the problem
But this causes another problem:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module './app' imported from /boot.js
I have many file that import modules and other files like that so i can't change them all
How do I keep the two type fo import?
If you only import babel like that you can setup babel for global import
This is .babelrc file on root folder:
{
"presets": [
"#babel/preset-env"
]
}

Cannot import functions in from one directory's subfolder to other pyhton files in same parent directories sub folders. How do I use init.py files

parent_folder /
subfolder1 /
subsubfolder1/
a.py
b.py
subsubfolder2/
c.py
d.py
e.py
subfolder2 /
subsubfolder2/
f.py
g.py
subfolder3 /
h.py
i.py
g.py
I want to import the functions from files h,i,g to all the subsubfolders py files. Can any one help me how do I do that.
I have tried using sys.path.insert(0 , 'path')
from h import funct1 , from g import funct1. It works
II get this error ImportError: attempted relative import with no known parent package when I do from .h import funct1 and I also want to Implement this in docker files. Is there any other way?
Thanks in advance !!
For your case it is best to use sys:-
for example to import in a.py use:
import sys
sys.path.insert(0,'../../subfolder3')
import h,i,g
Here we can use relative path in sys, '..' indicates moving one folder back
For Docker it is best to use relative path as:
import sys,os
sys.path.append(os.path.join(sys.path[0],'../../subfolder3'))
import h,i,g
This would work for docker. Please try and let me know if it works.

React - Can't import modules if entry points have timestamp in file name

I have module-a which has the following directory structure
module-a
public/index.html
src/index.js
index.js exports component App.
module-a built through webpack, for caching related issues the build output file names are appended with timestamp, for example the dist for this module would be
module-a
index.[timestamp].html
index.[timestamp].js
module-b has a dependency of module-a, when I try to import App component of module-a, I'm getting module not found error.
import App from module-a (not working)
import App from module-a/index.[timestamp] (working)
If I remove the timestamp in the filename the modules, I could import components with just module name.
The timestamp will change for every build, so I can't hardcode the timestamp in the import statements.
My question is how can I import App component without specifying the index.[timestamp] in module-b.
Thanks in advance.

Meteor Shell, can't import files from any folder: Error: Cannot find module ‘/imports/api/donuts/collection.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.

A test running with nosetests fails with ImportError, but works with python command

When run a test with python mycore/tests4extractor.py it works. If run the test with nosetests ./mycore/tests4extractor.py it fails with ImportError: No module named extractor. I am in the helpers folder.
The project structure is:
helpers/
mycore/
__init__.py
extractor.py
tests4extractor.py
Setting PYTHONPATH to the absolute path to helpers and/or helpers/mycore doesn't help.
Answer
tests4extractor.py:
import mycore
from extractor import extract
should be changed to:
import mycore
from mycore.extractor import extract
And python should be run with python -mmycore.tests4_strings
Does tests4extractor.py contain import extractor?
Because mycore is a package, you need to use absolute imports:
from mycore import extractor
or relative imports:
from . import extractor

Resources