I am getting an error when I import a file for testing.
This is the only line in the file:
import Cart from '../../components/cart/Cart';
This is the error:
TypeError: Cannot read properties of undefined (reading 'default')
27 |
28 | // Form Blocks
> 29 | export { default as CustomerInfo } from './formBlocks/CustomerInfo';
| ^
30 | export { default as Address } from './formBlocks/Address';
Note: CustomerInfo is not used in the file I am trying to import. If I comment out that line we get the same error on a different line that is also not related to the component I am trying to import.
The error points to an index file for all of the components.
This also happens if I change the import to any of these variations:
import Cart from '../../components/cart/Cart';
import { Cart } from '../../components';
import { Cart } from '../../components/index';
etc
I also get the same error if I try to import a file that doesn't exist
import ThisFileDoesntExist from '../../components/index';
Why is it looking at the index file if I import it directly from the component file?
Is there some extra config I can add to tell Jest not to look at the index file?
Related
hi I want to import a graphql file. For this i have installed babel-plugin-import-graphql package.but for some reason i am getting error
\node_modules\ts-node\src\index.ts:750
return new TSError(diagnosticText, diagnosticCodes);
^
TSError: тип Unable to compile TypeScript:
src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations.
12 import typeDefs from "./schema.graphql";
~~~~~~~~~~~~~~~~~~
how do i do the import
import typeDefs from "./schema.graphql";
graphql.d.ts
declare module "*.graphql" {
import { DocumentNode } from "graphql";
const value: DocumentNode;
export = value;
}
.babelrc
{
"plugins": ["import-graphql"]
}
full code can be viewed here
Rename the file from src/#types/graphql.d.ts to src/#types/index.d.ts, leave the contents the same, and restart the TS server (or just reload the IDE).
I'd like to import a text file:
import text from "./text.txt";
I have a file with:
declare module "*.txt" {
const content: string;
export default content;
}
However, ts-node complains with:
error TS2307: Cannot find module './text.txt' or its corresponding type declarations.
Please note that I cannot use the fs module to read the file since I am also using the same code for the web frontend (where the import is resolved by the bundler).
EDIT
Trying to use the proposal in the first answer:
import text from "./text.txt!text";
with
declare module "*!text" {
const content: string;
export default content;
}
I get this error:
Cannot find module './text.txt!text' from '...'
you have to tell typescript that your module will be a string type like that:
import text from "./text.txt!text";
I have installed AnimeJS with the following commands in my Ionic 4 project:
npm i animejs --save
npm i #types/animejs --save
I then attempted to reference it by using:
import * as anime from 'animejs'
Doing the above gives me the following error when calling anything from animejs:
Error: Uncaught (in promise): TypeError: Object is not a function
(near '...animejs__WEBPACK_IMPORTED_MODULE_1__...')
However, if I import by referencing the anime.js within the node_modules directory, everything will work just as expected. I thought by installing #types/animejs this would allow me to use just a simple import * as anime from 'animejs' without having to directly reference the file within the node_modules directory.
Why can I import using the node_modules folder but not import * as anime from 'animejs'
After importing I call it like so:
openModal(modalPage) {
// Define modal to open
switch (modalPage) {
case 'login' : {
modalPage = LoginPage;
break;
}
case 'register' : {
modalPage = RegisterPage;
break;
}
}
// Open modal
this.modalCtrl.create({
component: modalPage,
cssClass: 'intro-modal',
showBackdrop: true,
enterAnimation: this.animations.modalEnter
}).then(modal => {
// Hide intro buttons
anime({
targets: ['.intro-buttons'],
translateX: '-100%',
duration: 150,
easing: 'linear'
});
// Animate slide back
modal.onWillDismiss().then(() => {
anime({
targets: ['.intro-buttons'],
translateX: '0%',
duration: 150,
easing: 'linear'
});
});
// Present the modal
modal.present();
});
}
Update your import to the following:
import anime from 'animejs'
This will import the default export from animejs which is actually a function that take the params/object that you are attempting to pass.
Here is an example in action showing the import and passing the expected object to anime() without the error triggering.
With your existing import * as anime, if you log anime, you'll see a property default of that object that is the actual function you are needing. Also you will see that import is bringing in various other properties/functions including penner, stagger, and timeline. You were simply targeting the wrong property with your previous import.
Hopefully that helps!
I am trying to create a Jenkins Pipeline Script using groovy. However, the import statement is giving me a compilation error - Unknown Type : Import. Not sure why.
You should define import jxl.* at the top of the pipeline script, e.g.
import jxl.*
node {
stage('Execute Tests') {
try {
dir('.') {
sh '......' // etc.
}
}
}
}
When you added it inside node {} block Jenkins was looking for a method instead of import class statement. The good convention is to put all import statements at the top of the Groovy file.
This code works fine:
import modal = require('./modal');//modal path
class index{
constructor(){
var_modal = new modal();
}
}
export = index
If I change the require path to "../../widgets/personInfo/viewmodel", after I rebuild the solution Visual Studio gives the error:
Unable to resolve external module "../../widgets/personInfo/viewmodel", Build: Module cannot be aliased to a non-module type, Invalid 'new' expression
The problem was found. Folder name was starting with uppercase, while require path was all-lowercase.
Valid code:
import modal = require('../../Widgets/PersonInfo/viewmodel')