Normally in Node one can useNODE_PATH=./src"
to make it so that instead of:
import { foo } from '../../../bar'
You can just do:
import { foo } from 'src/bar'
However, that only works if you use the esm
package (ie. node -r esm
): NODE_PATH
doesn't work with native ES Modules (ie. adding "type": "module"
to package.json
) ... so what is the modern replacement?
I've tried all of the following, and none of them seem to work (though I may not be using them correctly, and would welcome any clarification):
- local files (ie. `"dependencies": { "src": "file:./src",) - couldn't get this to even work
- symlinks (ie. adding a symlink from
node_modules/src
toproject-root/src
) - that imports the file as a CommonJS package, not an ES one, which means that named imports don't work - workspaces (ie.
"workspaces": ["src"],
inpackage.json
) - same issue: no named imports - imports (ie.
"imports": {"#src": "./src"}
) - ignores the--experimental-specifier-resolution=node
flag (so it only works IF I want to go through and manually add.js
to every import in my project) - custom loaders (ie. making a
loader.js
file and usingnode --loader loader.js
) - I couldn't figure out how to make this work, as there is almost no documentation on custom loaders
Ideally, I'd prefer not to have to implement all of Babel/Webpack/Typescript/etc. on my project, just to replace NODE_PATH=./src
, but it seems like adding some such tool is the only way now?