简体   繁体   English

ES 模块和 jsconfig.json:错误 [ERR_MODULE_NOT_FOUND]

[英]ES modules and jsconfig.json: Error [ERR_MODULE_NOT_FOUND]

I got a node web app project that uses regular JS.我有一个使用常规 JS 的节点 Web 应用程序项目。

I am trying to set some paths using a jsconfig.json .我正在尝试使用jsconfig.json设置一些路径。 I tried using node default's Subpath imports and even thou it does work, the vscode intellisense stopped working.我尝试使用节点默认的Subpath imports ,即使它确实有效,vscode 智能感知也停止工作。

I found out you could use jsconfig.json so I set this file我发现你可以使用jsconfig.json所以我设置了这个文件

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "baseUrl": "./",
    "paths": {
      "@controllers/*": ["node/controllers/*"],
    },
  },
  "exclude": ["node_modules"]
}

And on my package.json I added在我的package.json我添加了

"type": "module",

My folder structure is我的文件夹结构是

├── node
│    └──controllers
│         └── foo.js
├── server.js
├── jsconfig.json
└── package.json

But when I try to import from server.js但是当我尝试从server.js导入时

import { foo } from '@controllers/foo.js'
// or import { foo } from '@controllers/foo'

foo.js

export const foo = 'Hello server'

I get我明白了

node:internal/process/esm_loader:74
     internalBinding('errors').triggerUncaughtException(

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@controllers/foo.js' imported from /Users/Alvaro/Sites/test/server.js

 at new NodeError (node:internal/errors:363:5)
 at packageResolve (node:internal/modules/esm/resolve:698:9)
 at moduleResolve (node:internal/modules/esm/resolve:739:18)
 at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:853:11)
 at Loader.resolve (node:internal/modules/esm/loader:89:40)
 at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
 at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:73:40)
at link (node:internal/modules/esm/module_job:72:36) {
code: 'ERR_MODULE_NOT_FOUND'

After hours of research, I have found that it is very difficult to have a custom alias path while using esm and very easily configured for commonjs ,经过数小时的研究,我发现在使用esm时很难拥有自定义别名路径,并且很容易为commonjs配置,

but it can be achieved, in a slightly different manner when you use esm , I will write the solution for esm implementation in method 1 and then for commonjs in method 2但它可以实现,当你使用esm时,方式略有不同,我将在方法 1中编写esm实现的解决方案,然后在方法 2commonjs

Method 1方法一

Step 0:步骤 0:

remove jsconfig.json删除jsconfig.json

Step 1:步骤1:

in your package.json add an exports property,在你的package.json添加一个exports属性,

"name": "package-name"

"exports":{
   "./@controllers/" : "./node/controllers/"
}

Note: Don't forget the '/' after the @controllers and /node/controllers/注意:不要忘记@controllers/node/controllers/后面的 '/'

Step 2第2步

you will have to import the file你将不得不导入文件

  import {foo} from "package-name/controllers/index.js" 
/*
package-name is the same value as the 'name' property in
the package.json
*/

to learn more about exports property in package.json refer here要了解有关package.json中的exports属性的更多信息,请参阅此处

------------------------------XXXXXXXX---------------------------------- ------------------------------XXXXXXXX------ ---------------

Method 2方法二

you can't use es modules in this method您不能在此方法中使用es modules

Step 0步骤 0

install module-alias package.安装module-alias包。

npm i module-alias

remove type:module if it exists in package.json or change the value to commonjs ;如果package.json中存在type:module ,则删除它或将值更改为commonjs

Step 1步骤1

Add the below code at the starting line of the file which is specified as the entry point in package.jsonpackage.json中指定为entry point的文件的起始行添加以下代码

require(module-alias/register)

check the main property in package.json in the author's case it should be server.js .作者的情况下检查package.json中的main属性,它应该是server.js

in package.json add thispackage.json添加这个

"_moduleAliases": {
  "@root" : ".", // Don't forget to mnetion this
  "@controllers" : "node/controllers/*"
}

Now you can require modules from directries like below现在您可以从以下目录中要求模块

const {foo} = require("@controllers/foo.js") // if foo is named export 

//or

const foo = require("@controllers/foo.js");

you can refer more about module-alias library here您可以在此处参考有关module-alias更多信息

What you have tried is almost correct.你所尝试的几乎是正确的。

you can get aliases working but it has to start with # .你可以让别名工作,但它必须以#开头。

for example: in package.json例如:在package.json

"imports": {
    "#app": "./app.js"
}

and in jsconfig.json并在jsconfig.json

{
  "compilerOptions": {
    "paths": {
      "#app": ["./app.js"]
    }
  }
}

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

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