简体   繁体   English

如何在 koa 项目中设置模块解析器?

[英]how to setup module resolver in koa project?

I use "koa": "2.13.0", in my project and I have this .babelrc file:我在我的项目中使用"koa": "2.13.0",并且有这个 .babelrc 文件:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ],
  "plugins": [
    ["@babel/plugin-transform-async-to-generator"],
    ["module-resolver", {
      "root": ["./"],
      "alias": {
        "app": "./app",
        "routes": "./routes",
        "utils": "./utils"
      }
    }]
  ]
}

installed "babel-plugin-module-resolver": "4.0.0", on devDependencies package.json.在 devDependencies package.json 上安装了"babel-plugin-module-resolver": "4.0.0", But it does not work when I tried to require with 'utils' for example.但是当我尝试使用“utils”来要求时它不起作用。

const utils = require('utils')

got this error:得到这个错误:

internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'utils'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (D:\work\Smartwalkie\presence-server\routes\health\index.js:3:34)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

I tried to make import export ES6 work together with module resolver on koajs project, but could not get it so far.我试图让 import export ES6 与 koajs 项目中的模块解析器一起工作,但到目前为止无法实现。 Please help.请帮忙。

turns out, I should be using babel-node on the npm scripts, instead of nodemon or node.事实证明,我应该在 npm 脚本上使用 babel-node,而不是 nodemon 或 node。 For using babel-node for my case, need to install:为了在我的情况下使用 babel-node,需要安装:

"@babel/core": "^7.12.3",
"@babel/node": "^7.12.6",

npm scripts should look like this: npm 脚本应如下所示:

  "scripts": {
    "start": "babel-node app.js",
    "dev": "nodemon --exec babel-node app.js"
  }

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

相关问题 如何在 babel-plugin-module-resolver 中使用 root 选项? - How to use root option in babel-plugin-module-resolver? 如何使用 babel-plugin-module-resolver 为根路径设置别名并禁用其他根导入? - How to use babel-plugin-module-resolver to set an alias for the root path and disable other root imports? 如何使用koa2运行简单的应用程序? - How to run simple app with koa2? Koa中间件与快速中间件有何不同? - How is koa middleware different from express middleware? 由于Webpack模块解析器,ReactJS测试文件发出``找不到模块&#39;&#39; - ReactJS test files issue 'cannot find module' because of webpack module resolver 无需Webpack项目设置即可进行反应 - React without webpack project setup 如何在ES6项目中导入我的npm模块? - How can I import my npm module in an ES6 project? 如何使用webpack将模块用作项目中的外部库? - How can use my module as an external library in my project with webpack? react-router,使用Koa / Express进行服务器端渲染。 如何导入/导出已编译的路线 - react-router, server sided rendering with Koa/Express. How to import/export compiled routes 如何在Apollo GraphQL解析器的一个函数中解析2种不同的方法? - How to resolve 2 different methods in one function on Apollo GraphQL resolver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM