Parcel: Using node "util.promisify" leads to error

Created on 16 Apr 2019  路  1Comment  路  Source: parcel-bundler/parcel

馃悰 bug report

I'm using node "promisify" on "fs" methods, like:
const writeFile = util.promisify(fs.writeFile);

Parcel builds the package successfully, but when importing it in another project, I receive the following error:
TypeError: The "original" argument must be of type Function

When using the --target node flag:

TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type Function. Received type undefined

馃帥 Configuration (.babelrc, package.json, cli command)

I tried both cases:

parcel build index.js
parcel build index.js --target node

Both fails.

馃 Expected Behavior

Be able to import the final distribution code without errors.

馃槸 Current Behavior

Build is successful, but importing the code as a dependency throws an error.

Stacktrace when running with --target node option:

/Users/ldecastro/workspace/temp/dist/index.js:1
(function (exports, require, module, __filename, __dirname) { parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"

TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type Function. Received type undefined
    at Object.promisify (internal/util.js:256:11)
    at Object.parcelRequire.Focm (/Users/ldecastro/workspace/temp/dist/index.js:2:135)
    at f (/Users/ldecastro/workspace/temp/dist/index.js:1:530)
    at parcelRequire.Focm (/Users/ldecastro/workspace/temp/dist/index.js:1:833)
    at Object.<anonymous> (/Users/ldecastro/workspace/temp/dist/index.js:1:1085)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)

Stacktrace when running without --target node option:

/Users/ldecastro/workspace/temp/dist/index.js:1
(function (exports, require, module, __filename, __dirname) { parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"

TypeError: The "original" argument must be of type Function
    at Object.exports.promisify (/Users/ldecastro/workspace/temp/dist/index.js:10:7135)
    at Object.parcelRequire.Focm.util (/Users/ldecastro/workspace/temp/dist/index.js:14:125)
    at f (/Users/ldecastro/workspace/temp/dist/index.js:1:530)
    at parcelRequire.jSD1 (/Users/ldecastro/workspace/temp/dist/index.js:1:833)
    at Object.<anonymous> (/Users/ldecastro/workspace/temp/dist/index.js:1:1085)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node ./index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/ldecastro/.npm/_logs/2019-04-16T12_53_39_263Z-debug.log

馃敠 Context

In my codebase, I'm exporting a idempotent utility function to create directories:

import util from 'util';
import fs from 'fs';

const mkdir = util.promisify(fs.mkdir);

export const createDirectory = async (folderPath) => {
  try {
    await mkdir(folderPath);
  } catch (err) {
    const doesDirectoryAlreadyExists = (err.code === 'EEXIST');
    if (doesDirectoryAlreadyExists) {
      return null; // Idempotent: Folder already exists, no need to create it.
    }

    return err;
  }
};

I searched parcel codebase and found these two files:

Which leads me to think that it might have a reason for my code to not work as expected.

Regardless, I can use the callback version of this code, no worries.
However, I'm curious if there is a solution for such case and why it is not working on the current state.

馃捇 Code Sample

All the instructions to reproduce the problem can be found on README.md file:
https://gitlab.com/leonardo.sarmentocastro/parcel

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.12.3
| Node | v10.15.3 (LTS)
| npm/Yarn | 6.4.1
| Operating System | macOS Mojave, Version 10.14.1 (18875)

Bug

>All comments

Was this page helpful?
0 / 5 - 0 ratings