I'm trying to set alias globally in my project without using Webpack, Babel, etc. Right now I have a simple test with AVA .
I'm using module-alias npm package that let me set in package.json my aliases. However, when I try to just create a simple example following the documentation with a basic alias it doesn't find the file:
Error: Cannot find module '@root/my-module.js'
You can reproduce this example 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
}
Save these files to an empty folder and then in that path in your terminal: npm i && npm run test
Maybe I'm missing same basics, thank you so much for time and help!
I ran into this same issue. You can solve this by using module-alias/register
inside your Ava config.
Your package.json should look like:
{
"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/register"
]
},
"_moduleAliases": {
"@root": "."
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.