I need to set up global aliases in my project without using Webpack or Babel. Currently, I am testing with AVA.
The npm package module-alias allows me to define aliases in my package.json file. However, when I try to create a basic example following the documentation and setting a simple alias, it doesn't recognize the file:
Error: Cannot find module '@root/my-module.js'
You can replicate this issue with these 3 files:
package.json
{
"name": "ava-alias",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "ava"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ava": "^2.4.0",
"esm": "^3.2.25",
"module-alias": "^2.2.2"
},
"ava": {
"require": [
"esm",
"module-alias"
]
},
"_moduleAliases": {
"@root": "."
}
}
test.js
require('module-alias/register')
import test from 'ava'
//import cube from './my-module.js' // working
import cube from '@root/my-module.js' // not working
test('cube of 3 is 27', t => {
t.is(cube(3), 27)
})
my-module.js
export default function cube(x) {
return x * x * x
}
Create an empty folder, save these files there, and then run npm i && npm run test
in that directory. Thank you for your assistance!