Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import react components with absolute path

Here is my test file

// /imports/components/main.test.js
import React from 'react'
import { shallow, mount } from 'enzyme'
import Main from './main'
import TextInput from "/imports/ui/textInput"
...

and the main.js has

// /imports/components/main.js
import { action1 } from "/imports/actions/myAction"

but it throws an error when I run the test, saying

Cannot find module '/imports/actions/myAction' from 'main.js'

If I comment the import './main', same thing happen with importing TextInput. I have no issue with importing modules in node_modules.

How can I tell Jest or webpack to import the component using absolute path from project directory (i.e import Foo from /imports/...)?

like image 560
spondbob Avatar asked Sep 02 '25 14:09

spondbob


1 Answers

Better way to solve relative path import issue, is by creating jsconfig.json file adjacent to package.json file.

{
  "compilerOptions": {
    "baseUrl": "src"
  }
}

then import { action1 } from "actions/myAction"; will work

like image 104
nishit chittora Avatar answered Sep 05 '25 03:09

nishit chittora