码迷,mamicode.com
首页 > Web开发 > 详细

[Node.js] Resolve path problem in all envs (require.resolve)

时间:2021-03-30 13:42:21      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:and   for   sync   code   nod   read   files   res   path   

When dealing with `fs` library, the common problem you will meet is the path to file.

Different envs may have different way to handle the path.

The best way which works for all is `require.resolve`:

  const script = fs.readFileSync(require.resolve("./image.js"), "utf-8");

 

[Node.js] Resolve path problem in all envs (require.resolve)

标签:and   for   sync   code   nod   read   files   res   path   

原文地址:https://www.cnblogs.com/Answer1215/p/14592332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!