简体   繁体   English

如何使用package.json中的npm脚本配置'require'.js文件?

[英]How to 'require' .js files using npm's scripts config in package.json?

I'm trying out a node.js library called pkg ( https://www.npmjs.com/package/pkg ), and received the following error: 我正在尝试一个名为pkghttps://www.npmjs.com/package/pkg )的node.js库,并收到以下错误:

Warning Cannot resolve 'path.join(__dirname, './some/file.js')' /path/to/index.js Dynamic require may fail at run time, because the requested file is unknown at compilation time and not included into executable. 警告无法解析'path.join(__ dirname,'./some/file.js')'/path/to/index.js动态需求在运行时可能会失败,因为所请求的文件在编译时是未知的并且未包含在其中可执行文件。 Use a string literal as an argument for 'require', or leave it as is and specify the resolved file name in 'scripts' option. 使用字符串文字作为'require'的参数,或将其保留不变,并在'scripts'选项中指定解析的文件名。

How exactly do you specify a 'resolved file name' in the package.json file? 您如何在package.json文件中指定“解析文件名”? And more generally, is there a way I can specify a path to a file in the package.json file and then just use require('key in the scripts object') to use that file? 更一般地说,有没有一种方法可以在package.json文件中指定文件的路径,然后仅使用require('key in the scripts object')使用该文件?

I found the solution to this was to package my code as npm modules and then require them with npm install xxx --save . 我发现解决方案是将代码打包为npm模块,然后通过npm install xxx --save要求它们。

Then pkg works very well in creating executables. 然后pkg在创建可执行文件时效果很好。

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

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