简体   繁体   English

如何从node_modules(自定义模块)读取文件,例如babel读取.babelrc文件

[英]How to read a file from node_modules (custom module) like babel reading .babelrc files

I have a custom package. 我有一个自定义包。 I tried to use 'fs' as dependancy. 我试图使用“ fs”作为依赖项。 but I am getting following error. 但我收到以下错误。 (but I can see installed module in node_modules folder) Module not found: Error: Can't resolve 'fs' (但我可以在node_modules文件夹中看到已安装的模块) Module not found: Error: Can't resolve 'fs'

then I make changes to webpack config as 然后我更改为webpack配置

node: { fs: empty }

then I could able to build. 这样我就可以建造。 Later I have installed this package in other project and I could n't able to read file. 后来,我在其他项目中安装了此软件包,但无法读取文件。 'error: fs is not installed.' 错误:未安装fs。

My goal: 我的目标:

I would like to read a read a file which is under 'src/' folder (like babel reading .babelrc file) from my custom npm package which is under node_module. 我想从我的自定义npm包中位于node_module下的'src /'文件夹下读取一个文件(例如babel读取.babelrc文件)。

Please advice! 请指教! thankyou! 谢谢!

[Just adding my work around. [只添加我的工作。 It could help someone.] 它可以帮助某人。]

After spending enough time I find my mistake. 花了足够的时间后,我发现了自己的错误。 Built-in node module 'fs' will not available in frond-end when while compile and load in browser. 当在浏览器中编译和加载时,内置的节点模块“ fs”在前端不可用。

To get access for fs module, I have to execute script (like node ../abc.js this script will load config file from 'src/' folder) through pre-install/post-install in my custom package. 要访问fs模块,我必须通过自定义程序包中的预安装/后安装来执行脚本(例如node ../abc.js此脚本将从“ src /”文件夹加载配置文件)。 This comes with a problem, any changes to config file need package installation. 这带来了一个问题,对配置文件的任何更改都需要安装软件包。

I took another approach with webpack. 我对webpack采取了另一种方法。 Created a webpack-loader which will be called when my package code is getting parsed and then custom package will get access to 'fs' module to read a file in 'src/' folder. 创建了一个webpack-loader,在解析我的程序包代码时将调用该程序,然后自定义程序包将访问“ fs”模块以读取“ src /”文件夹中的文件。 Changes to config will be re-compiled with webpack. 对配置的更改将使用webpack重新编译。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Babel 不处理 node_modules - 没有排除,没有.babelrc - Babel doesn't process node_modules - no excludes, no .babelrc 为什么 Babel 7 不编译 node_modules 文件? - Why is Babel 7 not compiling node_modules files? 模块构建失败(来自./node_modules/@nuxt/webpack/node_modules/babel-loader/lib/index.js): - Module build failed (from ./node_modules/@nuxt/webpack/node_modules/babel-loader/lib/index.js): 如何解决未捕获的错误:模块构建失败(来自./node_modules/babel-loader/lib/index.js): - How to Solve Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js): 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js) - Module build failed (from ./node_modules/babel-loader/lib/index.js) 错误:模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):语法错误: - Error: Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: 模块构建失败(来自./node_modules/babel-loader/lib/index.js): - Module build failed (from ./node_modules/babel-loader/lib/index.js): 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js)Vue Js - Module build failed (from ./node_modules/babel-loader/lib/index.js) Vue Js 未捕获的错误:模块构建失败(来自./node_modules/babel-loader/lib/index.js) - Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js) Webpack SyntaxError - 模块构建失败(来自./node_modules/babel-loader/lib/index.js) - Webpack SyntaxError - Module build failed (from ./node_modules/babel-loader/lib/index.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM