简体   繁体   English

npm 包从导入中删除文件扩展名

[英]npm pack removing file extensions from import

I'm trying to a create an internal library for work.我正在尝试为工作创建一个内部库。

All seems to be fine until I attempt to use in another project.在我尝试在另一个项目中使用之前,一切似乎都很好。 The file extension in all of the import statements seems to have been removed during the npm pack phase.所有import语句中的文件扩展名似乎已在npm pack阶段被删除。

In other words, the statement:换句话说,声明:

import * as Account from './modules/account.js'

becomes:变成:

import * as Account from './modules/account'

This causes the import to fail.这会导致导入失败。

I originally thought this may have been because I used the .js extension instead of .mjs , but switching to .mjs yields the same results.我最初认为这可能是因为我使用了.js扩展名而不是.mjs ,但切换到.mjs产生相同的结果。

main.js主程序

import * as Account from './modules/account.js'

Account.secretSquirrel().then( data => console.log( 'inspector gadget', data ) );

node version节点版本

v16.15.0

package.json (sensitive info redacted) package.json (已编辑敏感信息)

{
  "name": "@Nunya",
  "version": "0.0.0",
  "description": "Nunya",
  "private": true,
  "main": "./lib/main.js",
  "scripts": {
    "build": "npm run pack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "Nunya.git"
  },
  "author": "Nunya",
  "license": "ISC",
  "type": "module",
  "exports": {
    ".": {
      "require": "./lib/main.js",
      "default": "./lib/main.js"
    },
    "./Account": "./lib/modules/account.js"
  }
}

As far as I can tell, this shouldn't be happening.据我所知,这不应该发生。 Not sure how to resolve不确定如何解决

Evidently, adding an import object defining the resolutions for the relative paths to package.json resolves this issue.显然,添加定义 package.json 的相对路径分辨率的import package.json可以解决此问题。 Although, I'm certain there's a non-manual way to accomplish this, it is unbeknownst to me at this time.虽然,我确定有一种非手动的方法可以完成此操作,但目前我还不知道。

If anyone knows, please let me know如果有人知道,请告诉我

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

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