简体   繁体   中英

Copy file from node_modules in a dependency

I've module A with the following postinstall script:

"postinstall": "cp node_modules/dep-module/file.txt lib/file.txt"

After running npm install the file can be found and copied:

module-A
|- node_modules
   |- dep-module
      |- file.txt

This worked fine until I imported the module A from the module B :

"module-a": "0.0.1"

Now since I do the npm install from module B the dir structure is:

module-B
|- node_modules
   |- module-A
   |- dep-module
      |- file.txt

The file.txt cannot be find in the same route, it should be instead:

"postinstall": "cp ../dep-module/file.txt lib/file.txt"

What are the best options here to solve this issue?

Currently I've hacked a script that simple does a path check to see if there is a node_modules dir, but I would prefer a solution where I can avoid the node script...

const getModulePath = module =>
  fs.existsSync(path.resolve('./node_modules')) ? `./node_modules/${module}` : `../${module}`;

fs.copy(`${getModulePath('dep-module')}/file.txt`, 'dist/file.txt');

您可以使用require.resolve()将模块样式的字符串解析为文件路径。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM