使用 package.json 子路径导出时获取 MODULE_NOT_FOUND

发布于 2025-01-17 02:41:11 字数 4768 浏览 4 评论 0原文

我试图了解如何使用 子路径导出 但我不知道知道我做错了什么。

使用节点 v17.6.0(64 位)+ windows10,我创建了一个简单的项目来玩,如下所示:

│   package.json                          
│                                         
├───packages                              
│   └───myorg-common                      
│       │   index.js                      
│       │   package.json                  
│       │                                 
│       └───src                           
│           │   a.js                      
│           │   index.js                  
│           │                             
│           └───b                         
│                   b.js                  
│                                         
└───src                                   
    │   main.js                           
    │                                     
    └───c                                 
            c.js                          
            index.js                      

ROOT

.\package.json

{
  "name": "test_exports",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "workspaces": [
    "packages/*"
  ],
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "esm": "^3.2.25"
  }
}

.\src\main。 js

import * as modroot from "@myorg/common/module_root";
// import * as moda from "@myorg/common/module_a";
// import * as modb from "@myorg/common/module_b";

console.log(modroot);
// console.log(moda);
// console.log(modb);

.\src\c\c.js

export const C="src/c/c.js";

.\src\c\index.js

export * from "./c";

工作空间

.\packages\myorg-common\index.js

export const ROOT="ROOT";

.\packages\myorg-common\package.json

{
  "name": "@myorg/common",
  "version": "1.0.0",
  "description": "",
  "exports": {
    "./module_root": "./src/index.js",
    "./module_a": "./src/a.js",
    "./module_b": "./src/b/b.js"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

.\packages\myorg-common\src\a .js

export const A="src/a.js";

.\packages\myorg-common\src\index.js

export * from "./a";
export * from "./b/b";

.\packages\myorg-common\src\b\b.js

export const B="src/b/b.js";

问题

之后正在做npm install 在根路径中并运行 node -r esm src\main.js 我会收到此错误:

D:\test_exports\node_modules\esm\esm.js:1
const __global__ = this;(funct ...TRUNCATED_CONTENT_OR_I_WON'T_BE_ABLE_TO_SUBMIT_TO_SO... default=Jh})]).default;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

D:\test_exports\src\main.js:1
Error: Cannot find module '@myorg/common/module_root'
Require stack:
- D:\test_exports\src\main.js
    at Object.<anonymous> (D:\test_exports\src\main.js:1) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'D:\\test_exports\\src\\main.js' ]
}

Node.js v17.6.0

几个问题:

  • 为什么我会收到这个错误MODULE_NOT_FOUND 错误以及如何修复它?我的主要目标是能够学习如何从我的工作区导出所有子包,而不必捆绑它们(即:直接从我的 CRA 应用程序使用 src 内容,这些应用程序通过反应脚本使用 webpack,这是很棒的)。

  • 来自 esm 的巨大转储,有什么方法可以避免所有这些噪音/日志?在旧版本的节点中,我不记得输出如此冗长。

提前致谢。

I'm trying to understand how to use Subpath exports but I don't know what I'm doing wrong.

Using node v17.6.0 (64-bit) + windows10 and I've created a simple project to play around that looks like this:

│   package.json                          
│                                         
├───packages                              
│   └───myorg-common                      
│       │   index.js                      
│       │   package.json                  
│       │                                 
│       └───src                           
│           │   a.js                      
│           │   index.js                  
│           │                             
│           └───b                         
│                   b.js                  
│                                         
└───src                                   
    │   main.js                           
    │                                     
    └───c                                 
            c.js                          
            index.js                      

ROOT

.\package.json

{
  "name": "test_exports",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "workspaces": [
    "packages/*"
  ],
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "esm": "^3.2.25"
  }
}

.\src\main.js

import * as modroot from "@myorg/common/module_root";
// import * as moda from "@myorg/common/module_a";
// import * as modb from "@myorg/common/module_b";

console.log(modroot);
// console.log(moda);
// console.log(modb);

.\src\c\c.js

export const C="src/c/c.js";

.\src\c\index.js

export * from "./c";

WORKSPACE

.\packages\myorg-common\index.js

export const ROOT="ROOT";

.\packages\myorg-common\package.json

{
  "name": "@myorg/common",
  "version": "1.0.0",
  "description": "",
  "exports": {
    "./module_root": "./src/index.js",
    "./module_a": "./src/a.js",
    "./module_b": "./src/b/b.js"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

.\packages\myorg-common\src\a.js

export const A="src/a.js";

.\packages\myorg-common\src\index.js

export * from "./a";
export * from "./b/b";

.\packages\myorg-common\src\b\b.js

export const B="src/b/b.js";

QUESTION

After doing npm install in the root path and running node -r esm src\main.js I'll get this error:

D:\test_exports\node_modules\esm\esm.js:1
const __global__ = this;(funct ...TRUNCATED_CONTENT_OR_I_WON'T_BE_ABLE_TO_SUBMIT_TO_SO... default=Jh})]).default;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

D:\test_exports\src\main.js:1
Error: Cannot find module '@myorg/common/module_root'
Require stack:
- D:\test_exports\src\main.js
    at Object.<anonymous> (D:\test_exports\src\main.js:1) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'D:\\test_exports\\src\\main.js' ]
}

Node.js v17.6.0

Couple of questions:

  • Why am I getting that MODULE_NOT_FOUND error and how to fix it? My main goal is being able to learn how to export all subpackages from my workspaces without having to bundle them (ie: it'd be great to use src content directly from my CRA apps that are using webpack through react-scripts).

  • That huge dump coming from esm, is there any way to avoid all that noise/log? In older versions of node I don't remember the output was so verbose.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-01-24 02:41:11

似乎 esm 是这里的罪魁祸首,我不确定如何“修复”它。尽管如此,一切都会与 webpack/rollup/vite 等一起正常工作(经过测试)。

要使其与节点一起使用,您必须使用 commonjs 以避免使用 esm 或通过将文件扩展名更改为 .mjs 或在 包中添加 "type": "module" 来切换到 ES 模块.json 然后运行:

node src/main.mjs

node src/main.js

取决于你决定做什么。

注意:如果您选择将 ES 模块与节点一起使用,则在这两种情况下都必须将文件扩展名添加到导入/导出语句中。例如:export * from './a.js'export * from '.a.mjs'

It seems that esm is the culprit here and I'm not sure about how to "fix" it. Nonetheless, everything will work fine with webpack/rollup/vite etc. (tested).

To make it work with node, you'd have to either use commonjs to avoid using esm or just switch to ES modules by changing your file extensions to .mjs or by adding "type": "module" in your package.json and then run:

node src/main.mjs

or

node src/main.js

depending on what you decided to go with.

NOTE: if you choose to use ES modules with node, you'd have to add the file extensions to your import/export statements in both cases. e.g.: export * from './a.js' or export * from '.a.mjs'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文