开玩笑无法使用主入口点找到模块

时间:2019-09-18 12:44:48

标签: javascript jestjs

我有一个问题,我的编译代码在浏览器中可以正常工作,但开玩笑会引发错误。 像这样导入时出现错误

import { Button } from '@pipe-dream/core'

但是,使用主条目的显式路径可以解决该问题:

import { Button } from '@pipe-dream/core/dist/pipe-dream.js'

这很奇怪,因为package.json中的@pipe-dream/core已经指定了密钥:"main": "/dist/pipe-dream.js"-因此它应该产生相同的结果?

错误:

  

●测试套件无法运行

Configuration error:

Could not locate module @pipe-dream/core mapped as:
/Users/anders/Code/my-project/node_modules/@pipe-dream/core.

Please check your configuration for these entries:
{
  "moduleNameMapper": {
    "/^@pipe-dream\/(.*)$/": "/Users/anders/Code/my-project/node_modules/@pipe-dream/$1"
  },
  "resolver": null
}

但是通过我的node_modules来看。以及为什么在使用显式路径导入dist时它能起作用。

要查看我的语法是否有问题,我在node_modules中创建了一个虚拟程序包,该程序包似乎有效,至少没有产生相同的错误。 dummy package.json

main: "/dist/index.js"

/dist/index.js:     module.exports.dummy = function(){         console.log(“我可以控制台日志!”);     }

usage

import { dummy } from '@anders/dummy'
dummy()

这似乎可行。知道我在这里缺少什么吗?

1 个答案:

答案 0 :(得分:0)

测试以将main中的package.json键编辑为不以'/'开头。这可能会引起开玩笑的麻烦。

示例package.json

{
  "name": "@pipe-dream/core",
  "main": "dist/pipe-dream.js",
  ...
}