Home > Back-end >  npm pack removing file extensions from import
npm pack removing file extensions from import

Time:01-16

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.

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.

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)

{
  "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

CodePudding user response:

Evidently, adding an import object defining the resolutions for the relative paths to package.json resolves this issue. 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

  • Related