简体   繁体   English

如何从node_modules获取节点程序包使用者目录?

[英]How to get node package consumer directory from node_modules?

I am trying to create a simple node module that creates a set of folders in the app that consumes it. 我正在尝试创建一个简单的节点模块,该模块在使用它的应用程序中创建一组文件夹。 I exported a simple createLayout function that creates the folders. 我导出了一个简单的createLayout函数来创建文件夹。 I pushed my changes to git and did an npm i from another folder. 我将更改推送到git并从另一个文件夹执行了npm i Lets call the modules creator and consumer for the sake of explanation. 让我们为了解释而致电模块creatorconsumer When I try to call createLayout in consumer I am running in to several issues. 当我尝试在消费者中调用createLayout时,我遇到了几个问题。 I am in E:\\ drive. 我在E:\\驱动器中。

Below is the index.js in creator: 以下是创建者中的index.js:

import {sync} from 'mkdirp';

export function createLayout(config) {
  sync('./folder1');
}

And index.js in consumer: 和消费者中的index.js:

var createLayout = require('creator').createLayout;
createLayout();
// with config createLayout({path: __dirname})

This results in creating a folder in E:\\ not relative to consumer. 这导致在E:\\中创建一个文件夹,而不是相对于使用者。 So I tried including __dirname : 所以我尝试包括__dirname

sync(__dirname + '/folder1');

Once again, this also creates a folder in E:\\ not relative to consumer. 同样,这还会在E:\\中创建一个文件夹,而不是相对于使用者。 I searched for bit like in various modules to see how they are doing when they are reading the config file, for instance webpack uses process.cwd . 我在各种模块中搜索了一下,看看他们在读取配置文件时是怎么做的,例如webpack使用process.cwd So I tried that too. 所以我也尝试过。

sync(process.cwd() + '/folder1');

Same, results in creating a folder in E:\\ not relative to consumer. 同样,导致在E:\\中创建文件夹,而不是相对于使用者。 Then I tried to pass the __dirname or cwd through a config object. 然后我尝试通过config对象传递__dirname或cwd。

// get __dirname from the `consumer` in config.path
sync(config.path + '/folder1');

But it ends up in following error: 但它最终会出现以下错误:

Error: EPERM: operation not permitted, mkdir 'E:\'

I tried logging all the values in both creator and consumer: 我尝试记录创建者和使用者中的所有值:

console.log(__dirname, process.cwd(), config.path)
// creator: / / E:\projects\consumer
// consumer: E:\projects\consumer E:\projects\consumer E:\projects\consumer

I am using webpack with babel to pack the creator, plain js in consumer. 我正在将webpack与babel结合使用,以将创建者普通js打包到消费者中。 I do not know what am I doing wrong. 我不知道我在做什么错。 I am pretty new to nodejs ways of working. 我对nodejs的工作方式很陌生。

Update 更新

I am noticing that this is occurring only when I use webpack to build the creator. 我注意到只有当我使用webpack构建创建者时才会发生这种情况。 A simple module.exports works normally as anyone would expect. 一个简单的module.exports可以正常工作。 So I am including my webpack config file: 所以我包含了我的webpack配置文件:

module.exports = {
  entry: [
    './index.js'
  ],
  output: {
    filename: 'creator.js',
    path: __dirname + '/dist',
    library: 'creator',
    libraryTarget: 'umd'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel'
      }
    ]
  },
  externals: {
    fs: 'fs'
  }
};

Correct solution is adding this line in config : 正确的解决方案是在config中添加此行:

target: 'node'

this will make webpack to ignore modules like fs and mkdirp and some other. 这将使webpack忽略像fsmkdirp等模块。

Now no longer need to specify externals . 现在不再需要指定externals


Incorrect solution given before: 之前给出的解决方案不正确

Just add mkdirp to externals and it will resolve you problem: 只需将mkdirp添加到外部,它将解决您的问题:

  externals: {
    fs: 'fs',
    mkdirp: 'mkdirp'
  }

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

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