ilearnio / module-alias

Register aliases of directories and custom module paths in Node
MIT License
1.76k stars 69 forks source link

Advanced usage shouldn't depend on package.json #16

Closed hardfist closed 7 years ago

hardfist commented 7 years ago

I prefer to set it all up programmatically and don't have package.json in my project,but it throws an error

throw new Error('Unable to read ' + base + '/package.json')

I think since set up programatically doesn't depend on package.json it shouldn't throw an error

ilearnio commented 7 years ago

I think that you are running the init function like require('module-alias')() that is trying to get the config from package.json. Have you tried something like this? Does it work for you?

const moduleAlias = require('module-alias')

moduleAlias.addAlias('@client', __dirname + '/src/client')
hardfist commented 7 years ago

this following is my init function, and I don't read config from package.json so I think it shouldn't throw an error

const moduleAlias = require('module-alias');

const dev = process.env.NODE_ENV === 'development';
if(dev) {
  moduleAlias.addPath(path.resolve(__dirname, '../public/js'));
  moduleAlias.addAliases({
    img: path.resolve(__dirname, '../public/img'),
    scss: path.resolve(__dirname, '../public/scss')
  });
} else {
  moduleAlias.addPath(path.resolve(__dirname, './public/js'));
  moduleAlias.addAliases({
    img: path.resolve(__dirname, './public/img'),
    scss: path.resolve(__dirname, './public/scss')
  });
}
moduleAlias();
ilearnio commented 7 years ago

Just remove the moduleAlias(); line in the end, it shouldn't be there

hardfist commented 7 years ago

thank you,fixed my problem