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

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.

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 change path in Node for modules?

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'

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.

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.

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