Absolute imports does not resolve with NODE_PATH in 5+
See original GitHub issueCurrent behavior:
When starting Cypress with
NODE_PATH=ui/src cypress open
it is unable to resolve absolute imports
Webpack Compilation Error
./cypress/integration/cart/utils.js
Module not found: Error: Can't resolve 'ducks/cart' in '/home/pong/e2e/cypress/integration/cart'
...
My code is
import { cartOperations } from "ducks/cart";
...
Desired behavior:
Imports resolve correctly. This worked in 4.12.1.
Test code to reproduce
Will create a minimal working example if needed and deemed to be an issue.
Versions
Cypress 5.1.0 Node 14.5.0 OS: Ubuntu
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:11 (3 by maintainers)
Top Results From Across the Web
NODE_PATH and absolute imports with Laravel Mix and React?
My problem is that all of my React app files are dependent on a .env file that contains NODE_PATH=js/ to enable absolute imports....
Read more >CommonJS modules | Node.js v19.3.0 Documentation
Using package subpath exports or subpath imports can provide the same containment organization benefits as folders as modules, and work for both require...
Read more >Working with file system paths and file URLs on Node.js - 2ality
Module 'node:path' is often imported as follows: ... Resolving an absolute path without a drive letter against a fully qualified path full ...
Read more >Documentation - Module Resolution - TypeScript
A relative import is resolved relative to the importing file and cannot resolve to an ambient module declaration. You should use relative imports...
Read more >Module Resolution - webpack
Since we already have the absolute path to the file, no further resolution is required. Relative paths. import '../src/file1'; import ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
NODE_PATH is not supported in webpack by default as it was in browserify. Webpack’s reasoning is that use of NODE_PATH is discouraged: https://github.com/webpack/webpack/issues/4479
See the workaround below for this.
Reproducible example
cypress/integration/spec.js
ui/src/duck/index.js
4.12.1
6.1.0
Resolution / Workaround
Extend you
webpack.config
optionsresolve.modules
to look for theNODE_PATH
:webpack.config.js
cypress/plugins/index.js
Pass in the webpack options to cypress. This requires use of
@cypress/webpack-preprocessor
: https://github.com/cypress-io/cypress/tree/master/npm/webpack-preprocessor#optionsWanted to leave a comment for anyone that might run into this. I also ran into this issue while using jsconfig as recommended by create-react-app. (https://create-react-app.dev/docs/importing-a-component/). This error doesn’t occur in Cypress V4, but is present in V5 and V6.
This is the working fix for me since I was using create-react-app.
No need to create the webpack.config
Also note for some reason cypress does not like this type of import style after you use this workaround (I think it has to do something with
findWebpack.cleanForCypress
:export { default as SomeCommands } from "./SomeCommands"
Just change it to regular
import SomeCommands from "./SomeCommands"
The workaround for create-react-app/react-scripts Copy and paste from https://github.com/cypress-io/cypress/blob/master/npm/webpack-preprocessor/examples/react-app/cypress/plugins/index.js, but changed
const webpackPreprocessor = require("@cypress/webpack-preprocessor")