我可以使用原生 es6 模块 (mjs) 为我的本地模块使用裸导入吗?
Can I use bare imports for my local modules using native es6 modules (mjs)?
背景:
假设我的文件夹结构是这样的:
project/
-- package.json
-- index.mjs
-- lib/
-- config/
-- index.mjs
当我在节点中本地使用 require()
时,我可以使用裸导入来引用本地模块,例如:
const x = require('config')
因为我将库文件夹的根目录添加到了 NODE_PATH
环境变量中。 (当然假设当我使用 cjs/require 时,扩展名是 .js
)
问题:
当我尝试使用原生 es6 模块 (mjs) 执行此操作时,例如:
import x from 'config'
我收到错误:
Error [ERR_MODULE_RESOLUTION_LEGACY]: config not found by import in [SOME_ABS_PATH]/index.mjs. Legacy behavior in require() would have found it at [SOME_ABS_PATH]/lib/config/index.mjs
问题:
有人知道怎么解决吗?或者在节点原生 es6 模块系统中处理本地模块解析的相对路径的未来是什么?
目前的研究:
到目前为止我在这方面找到的唯一资源来自这里 http://2ality.com/2017/09/native-esm-node.html 说明:
Path resolution works slightly differently: ESM does not support NODE_PATH and require.extensions. And its specifiers always being URLs also leads to a few minor differences.
下面的错误消息 ERR_MODULE_RESOLUTION_LEGACY
- google 几乎什么都没显示。
好吧,一位同事(感谢@robin-balmforth)给了我答案:
来自 https://nodejs.org/api/esm.html#esm_no_node_path 它说:
Notable differences between import and require No NODE_PATH NODE_PATH
is not part of resolving import specifiers. Please use symlinks if
this behavior is desired.
来自更规范的东西:
All of the following will not be supported by the import statement:
$NODE_PATH $HOME/.node_modules $HOME/.node_libraries $PREFIX/lib/node
Use local dependencies, and symbolic links as needed.
所以我必须设置一个符号链接,而不是我设置 NODE_PATH 环境变量:
ln -s ../lib node_modules/lib
似乎工作正常耶。
我们假设原因是为了兼容 es6 模块的浏览器实现?
一些 讨论了 https://github.com/bmeck in this node-eps issue https://github.com/nodejs/node-eps/issues/11 这一变化背后的原因,例如:
The solution is not a workaround, it is how path resolution works.
Having a single flat namespace for "bare" path resolution is
important.
背景:
假设我的文件夹结构是这样的:
project/
-- package.json
-- index.mjs
-- lib/
-- config/
-- index.mjs
当我在节点中本地使用 require()
时,我可以使用裸导入来引用本地模块,例如:
const x = require('config')
因为我将库文件夹的根目录添加到了 NODE_PATH
环境变量中。 (当然假设当我使用 cjs/require 时,扩展名是 .js
)
问题:
当我尝试使用原生 es6 模块 (mjs) 执行此操作时,例如:
import x from 'config'
我收到错误:
Error [ERR_MODULE_RESOLUTION_LEGACY]: config not found by import in [SOME_ABS_PATH]/index.mjs. Legacy behavior in require() would have found it at [SOME_ABS_PATH]/lib/config/index.mjs
问题:
有人知道怎么解决吗?或者在节点原生 es6 模块系统中处理本地模块解析的相对路径的未来是什么?
目前的研究:
到目前为止我在这方面找到的唯一资源来自这里 http://2ality.com/2017/09/native-esm-node.html 说明:
Path resolution works slightly differently: ESM does not support NODE_PATH and require.extensions. And its specifiers always being URLs also leads to a few minor differences.
下面的错误消息 ERR_MODULE_RESOLUTION_LEGACY
- google 几乎什么都没显示。
好吧,一位同事(感谢@robin-balmforth)给了我答案:
来自 https://nodejs.org/api/esm.html#esm_no_node_path 它说:
Notable differences between import and require No NODE_PATH NODE_PATH is not part of resolving import specifiers. Please use symlinks if this behavior is desired.
来自更规范的东西:
All of the following will not be supported by the import statement:
$NODE_PATH $HOME/.node_modules $HOME/.node_libraries $PREFIX/lib/node Use local dependencies, and symbolic links as needed.
所以我必须设置一个符号链接,而不是我设置 NODE_PATH 环境变量:
ln -s ../lib node_modules/lib
似乎工作正常耶。
我们假设原因是为了兼容 es6 模块的浏览器实现?
一些 讨论了 https://github.com/bmeck in this node-eps issue https://github.com/nodejs/node-eps/issues/11 这一变化背后的原因,例如:
The solution is not a workaround, it is how path resolution works. Having a single flat namespace for "bare" path resolution is important.