如何从全局安装的节点应用程序中获取本地文件夹中没有node_modules的响应组件?

所以我得到了一个需要全局安装的软件包,它接受用户定义的反应组件并呈现它们。 我使用babel-register并定义:

 require('babel-register')({ presets: [ 'es2015', 'stage-0', 'react', ], }); 

我的package.json文件如下所示:

 "dependencies": { "babel-preset-es2015": "^6.24.1", "babel-preset-react": "^6.24.1", "babel-preset-stage-0": "^6.24.1", "babel-register": "^6.24.1", "babel-runtime": "^6.23.0", "chokidar": "^1.7.0", "del": "^2.2.2", "marked": "^0.3.6", "react": "^15.5.4", "react-dom": "^15.5.4", "window-size": "^1.0.0", "yamljs": "^0.2.10" }, 

现在,因为这些用户定义的反应组件有时住在没有安装.babelrc地方或者任何包中,所以我真的希望使用那些与包依赖关系一起全局安装的组件。 我试图避免在全球安装我的软件包时必须安装这些依赖项。 虽然不知道如何。

我检查了node_module文件夹,并安装了所有的依赖关系:

 . [...] ├── babel-code-frame ├── babel-core ├── babel-generator ├── babel-helper-bindify-decorators ├── babel-helper-builder-binary-assignment-operator-visitor ├── babel-helper-builder-react-jsx ├── babel-helper-call-delegate ├── babel-helper-define-map ├── babel-helper-explode-assignable-expression ├── babel-helper-explode-class ├── babel-helper-function-name ├── babel-helper-get-function-arity ├── babel-helper-hoist-variables ├── babel-helper-optimise-call-expression ├── babel-helper-regex ├── babel-helper-remap-async-to-generator ├── babel-helper-replace-supers ├── babel-helpers ├── babel-messages ├── babel-plugin-check-es2015-constants ├── babel-plugin-syntax-async-functions ├── babel-plugin-syntax-async-generators ├── babel-plugin-syntax-class-constructor-call ├── babel-plugin-syntax-class-properties ├── babel-plugin-syntax-decorators ├── babel-plugin-syntax-do-expressions ├── babel-plugin-syntax-dynamic-import ├── babel-plugin-syntax-exponentiation-operator ├── babel-plugin-syntax-export-extensions ├── babel-plugin-syntax-flow ├── babel-plugin-syntax-function-bind ├── babel-plugin-syntax-jsx ├── babel-plugin-syntax-object-rest-spread ├── babel-plugin-syntax-trailing-function-commas ├── babel-plugin-transform-async-generator-functions ├── babel-plugin-transform-async-to-generator ├── babel-plugin-transform-class-constructor-call ├── babel-plugin-transform-class-properties ├── babel-plugin-transform-decorators ├── babel-plugin-transform-do-expressions ├── babel-plugin-transform-es2015-arrow-functions ├── babel-plugin-transform-es2015-block-scoped-functions ├── babel-plugin-transform-es2015-block-scoping ├── babel-plugin-transform-es2015-classes ├── babel-plugin-transform-es2015-computed-properties ├── babel-plugin-transform-es2015-destructuring ├── babel-plugin-transform-es2015-duplicate-keys ├── babel-plugin-transform-es2015-for-of ├── babel-plugin-transform-es2015-function-name ├── babel-plugin-transform-es2015-literals ├── babel-plugin-transform-es2015-modules-amd ├── babel-plugin-transform-es2015-modules-commonjs ├── babel-plugin-transform-es2015-modules-systemjs ├── babel-plugin-transform-es2015-modules-umd ├── babel-plugin-transform-es2015-object-super ├── babel-plugin-transform-es2015-parameters ├── babel-plugin-transform-es2015-shorthand-properties ├── babel-plugin-transform-es2015-spread ├── babel-plugin-transform-es2015-sticky-regex ├── babel-plugin-transform-es2015-template-literals ├── babel-plugin-transform-es2015-typeof-symbol ├── babel-plugin-transform-es2015-unicode-regex ├── babel-plugin-transform-exponentiation-operator ├── babel-plugin-transform-export-extensions ├── babel-plugin-transform-flow-strip-types ├── babel-plugin-transform-function-bind ├── babel-plugin-transform-object-rest-spread ├── babel-plugin-transform-react-display-name ├── babel-plugin-transform-react-jsx ├── babel-plugin-transform-react-jsx-self ├── babel-plugin-transform-react-jsx-source ├── babel-plugin-transform-regenerator ├── babel-plugin-transform-strict-mode ├── babel-preset-es2015 ├── babel-preset-flow ├── babel-preset-react ├── babel-preset-stage-0 ├── babel-preset-stage-1 ├── babel-preset-stage-2 ├── babel-preset-stage-3 ├── babel-register ├── babel-runtime ├── babel-template ├── babel-traverse ├── babel-types [...] ├── react [...] └── yamljs 220 directories, 0 files 

我得到这个错误: Couldn't find preset "es2015" relative to directory

本地安装工程,但我不能使用bin绑定和我想添加的其他function。

我试图玩ignoreonlyselect,但没有成功。

所以我猜:

这个问题

我怎样才能从远离我自己的node_modules文件夹,可能没有任何错误安装没有任何依赖关系的文件夹中导入反应组件和babelfy?

更新

原来,你可以通过绝对path的babel-register电话:

 require('babel-register')({ presets: [ Path.normalize(`${ __dirname }/../node_modules/babel-preset-es2015`), Path.normalize(`${ __dirname }/../node_modules/babel-preset-stage-0`), Path.normalize(`${ __dirname }/../node_modules/babel-preset-react`), ] }); 

这似乎工作,但现在的应用程序抱怨无法findreactError: Cannot find module 'react'即使它在依赖关系。

更新#2

所以无法find反应的错误与显然导入反应的组件有关。 我想知道我是否可以将导入语句redirect到我的全局文件夹? 是的,你可以用这个漂亮的插件: https : //github.com/Velenir/babel-plugin-import-redirect 🙂

所以答案就是我的babel-register调用中的绝对path。 这解决了最初的问题。 :)希望它可以帮助路过的人。

 require('babel-register')({ presets: [ Path.normalize(`${ __dirname }/../node_modules/babel-preset-es2015`), Path.normalize(`${ __dirname }/../node_modules/babel-preset-stage-0`), Path.normalize(`${ __dirname }/../node_modules/babel-preset-react`), ] });