npm pack removing file extensions from import - javascript

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

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

Cannot Import My NPM Package With ES6 Import/Export Syntax

I am creating an NPM package called notifman. Here's what I did:
Created the package.json:
{
"name": "notifman",
"version": "1.0.5",
"description": "Advanced, Lightweight and Powerful Notification Library For Plain JS",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "npx nodemon index.js"
},
"keywords": [
"notification",
"frontend",
"js",
"plain"
],
"author": "...",
"license": "MIT",
"dependencies": {
"notifman": "^1.0.3"
},
"devDependencies": {
"nodemon": "^2.0.20"
}
}
I wrote the index.js:
console.log("hello world");
Wrote a README.md
Tested the package in Node.js and it worked fine.
Originally, I wanted the package to work in the browser, so I tried changing the code in index.js to:
export default function getRoot() {
return document.getElementById("root");
}
Then, I wanted to test using import/export syntax, I suppose it is:
import getRoot from "notifman";
getRoot.textContent = "hello world";
The error message is:
Uncaught TypeError: Failed to resolve module specifier "notifman". Relative references must start with either "/", "./", or "../".
Partially Solved:
I just used Webpack, and it worked fine. But I am sure Webpack is not required. I want it to work WITHOUT webpack. This is why this is partially solved.
"type": "module"
**add this to your package.json just below main **

cant get ES modules to work with import or require

I'm building a command line based application and was using require to link various files and node modules. I have previously used Require with no issues and now I get nothing but errors.
Error with Require:
Error [ERR_REQUIRE_ESM]: require() of ES Module ./lib/departments.cjs not supported,
Instead change the require of inquirer.js in ./lib/departments.cjs to a dynamic import() which is available in all CommonJS modules.
After this I tried multiple methods to get it to work again, but had no luck so I tried to switch to import. When I switched to import I got this message:
SyntaxError: Cannot use import statement outside a module
Ive tried to add type: "module" to my package.json file, but I don't know if I am putting it in the wrong place or what.
My JSON file looks like this
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "server.js",
"directories": {
"lib": "lib"
},
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git+https://.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://issues"
},
"homepage": "https://#readme",
"dependencies": {
"console.table": "^0.10.0",
"dotenv": "^16.0.2",
"express": "^4.18.1",
"inquirer": "^9.1.1",
"mysql2": "^2.3.3",
"node-fetch": "^2.6.7"
}
}
Seems like you are using .cjs extension for your js file, cjs is a commonJS file which doesn't support import. You need to use a dynamic import.
It's worth mentioning that ES modules cannot be imported using the require() function of cjs.

Getting references error when importing tone.js

I have a basic express.js server setup on my Mac. To this I'm trying to import Tone.js, https://tonejs.github.io, by following the instructions.
npm install tone
To import Tone.js:
import * as Tone from 'tone'
But I'm getting this error
Uncaught TypeError: Failed to resolve module specifier "tone". Relative references must start with either "/", "./", or "../".
My Package file looks like this
{
"name": "XXXXX",
"version": "1.0.0",
"description": "Run npm start",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon start"
},
"repository": {
"type": "git",
"url": "git+https://github.com/xxxxx"
},
"author": "xxxxx xxxx",
"license": "ISC",
"bugs": {
"url": "https://github.com/xxxxxxx"
},
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.12",
"tone": "^14.7.77"
}
}
As #lars-flieger pointed out, it's not possible to run it one a node.js server.
I ended up downloading Tones.js from here and includes it as a regular script tag.
https://github.com/Tonejs/Tone.js/wiki/Installation

Lite-server not working

I had a project that was previously using lite-server. I can no longer get it to run using
npm run
I see the following from the console
PS C:\Users\XXX\Documents\Work\Repos\Homepage_Slider> npm run
Lifecycle scripts included in homepage_slider: test
echo "Error: no test specified" && exit 1 start
lite-server
I don't really understand why this isn't working anymore.
I've tried:
Updating NPM
Running lite-server globably
Make sure package.json is set up correctly
Here is my package.json file
{
"name": "homepage_slider",
"version": "1.0.0",
"description": "A simple slider for homepage",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "lite-server"
},
"repository": {
"type": "git",
"url": "XXX"
},
"keywords": [
"Slider"
],
"author": "XXX",
"license": "ISC",
"devDependencies": {
"lite-server": "^2.3.0"
},
"dependencies": {
"npm": "^6.0.1"
}
}
npm run is a reserved word for npm itself. Check the documentation
What you really need is npm start which is a shortcut for npm run start which is a shortcut for npm run-script start!

Node.js nwjs package compilation error: Invalid file descriptor to ICU data

I'm trying to create my first package with nwjs, so I created a simple app with only nwjs module installed locally with npm. My app main file has only a console.log("Hello World!"). I zipped the app files - keeping them in the root - and renamed to app.nw. I basically followed this doc. The package tree is the following:
app.nw
- node_modules
- nw (and all its files inside)
- package.json
- app.js
And my package.json is:
{
"name": "app3",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"window": {
// Some properties here
}
}
Then I created an empty folder and copied app.nw to it. From the module folder /nw/nwjs I copied the files: nw.exe, nw.dll, nw_elf.dll and ffmpeg.dll to it, and dragged app.nw over nw.exe. The debug.log file shows me the following error:
[0524/110408:ERROR:icu_util.cc(157)] Invalid file descriptor to ICU data received.
[0524/110408:FATAL:icu_util.cc(260)] Check failed: result.
I can't find anything related to this error and this tool over the web. I'm running it in a Win7 x64. I also tryied downloading nwjs manually from github and tryied with x64 and x86 binaries, but they all give me the same error.
Below, the backtrace if needed:
v8::OutputStream::WriteHeapStatsChunk [0x000007FEDA1B7AB1+225089]
std::vector<v8::CpuProfileDeoptFrame,std::allocator<v8::CpuProfileDeoptFrame> >::operator= [0x000007FEDA1640BC+259612]
v8::OutputStream::WriteHeapStatsChunk [0x000007FEDA1F2E58+467688]
v8::Extension::auto_enable [0x000007FEDA1203D1+368897]
v8::Extension::auto_enable [0x000007FEDA11ED19+363081]
ChromeMain [0x000007FED9CE0EB5+133]
GetUploadedReportsImpl [0x000000013F201251+3297]
GetUploadedReportsImpl [0x000000013F20097F+1039]
IsSandboxedProcess [0x000000013F264628+231640]
BaseThreadInitThunk [0x0000000076C359BD+13]
RtlUserThreadStart [0x0000000076E6A2E1+33]
What am I missing here?
I got it to work with the following package.json:
{
"name": "app4",
"version": "1.0.0",
"description": "",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"single-instance": true,
"package": ".json",
"window": {
"title": "app4",
"toolbar": false
}
}
I'm actually not sure what specific property which I removed/added to made it to work. Then I zipped it to package.nw and put in the same folder as nw.exe to run it.

Categories

Resources