ES6 导入、节点库和我的模块
ES6 import, node libraries and my modules
我正在学习node,但以前从未使用过ES6/javascript/TypeScript,所以请多多包涵...
我正在尝试用纯 ES6 编写 nodejs 应用程序。从下面的例子(取自),
import fs from 'fs';
export default class Animal {
constructor(name){
this.name = name ;
}
print(){
console.log('Name is :'+ this.name);
}
}
import Animal from 'path/to/Animal.js';
我们可以看到有两种种import
,require
的ES6方式。一个是没有路径的(import fs from 'fs';
),一个是有路径的。
所以我的第一个困惑是,here 它说,
For compatibility with CommonJS and in preparation for future features, relative paths that don’t start with ./ or ../ are not allowed (in ES6):
// Not allowed:
import * as foo from 'foo.mjs';
import * as foo from 'lib/foo.mjs';
那么import fs from 'fs'
是对还是错?
我尝试编写的纯 ES6 nodejs 应用程序基于 npm
模块,但因为它几乎每天更新,所以我从它的 git 中提取而不是
npm install mydepmod
这又使其以
开头的示例代码
import { mydepmod } from 'mydepmod'
不适合我。我得到的错误是,
module.js:557
throw err;
^
Error: Cannot find module 'mydepmod'
at Function.Module._resolveFilename (module.js:555:15)
at Function.Module._load (module.js:482:25)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/path/to/example/the-test.js:19:21)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
所以我的最后一个问题是如何让它发挥作用?
顺便说一句,我尝试了它的 docker 安装和 docker 中的 运行(以 import { mydepmod } from 'mydepmod'
开头)工作没有任何问题,所以我假设如果我这样做 npm install mydepmod
,它应该也能工作。
总而言之,我怎样才能使我的 git 拉取依赖模块像 npm install
ed 模块一样工作。谢谢。
当您使用前缀为“./”的相对路径导入时,它会从该文件加载模块,您可以提供完整路径,例如“/path/to/module”。
当您导入任何没有任何路径的模块时,节点会查找该模块
- 在
node_modules
文件夹中,在该目录中,或在 package.json
存在的任何父目录中
- 如果无法从该目录加载模块,则节点会尝试从用户目录(例如,
/usr/lib/node_modules/
)中的全局 node_modules
文件夹加载该模块。
- 如果节点在此阶段无法加载模块,则会抛出错误提示
Can not find module
。
Here 详细描述了 require 是如何工作的。
require 和 Import/Export 类似,this 文章给出了 require 和 import/export 的区别。
局部node_modules和全局node_modules的区别,here待讨论
全局node_modules的位置在不同的os环境中是不同的。根据this回答:
在 Unix 系统上,它们在全局安装时通常位于 /usr/local/lib/node
或 /usr/local/lib/node_modules
中。如果将NODE_PATH
环境变量设置为该路径,则可以通过node找到模块。
Windows 经验值 - %USERPROFILE%\Application Data\npm\node_modules
Windows 7、8 和 10 - %AppData%\npm\node_modules
我正在学习node,但以前从未使用过ES6/javascript/TypeScript,所以请多多包涵...
我正在尝试用纯 ES6 编写 nodejs 应用程序。从下面的例子(取自
import fs from 'fs';
export default class Animal {
constructor(name){
this.name = name ;
}
print(){
console.log('Name is :'+ this.name);
}
}
import Animal from 'path/to/Animal.js';
我们可以看到有两种种import
,require
的ES6方式。一个是没有路径的(import fs from 'fs';
),一个是有路径的。
所以我的第一个困惑是,here 它说,
For compatibility with CommonJS and in preparation for future features, relative paths that don’t start with ./ or ../ are not allowed (in ES6):
// Not allowed:
import * as foo from 'foo.mjs';
import * as foo from 'lib/foo.mjs';
那么import fs from 'fs'
是对还是错?
我尝试编写的纯 ES6 nodejs 应用程序基于 npm
模块,但因为它几乎每天更新,所以我从它的 git 中提取而不是
npm install mydepmod
这又使其以
开头的示例代码import { mydepmod } from 'mydepmod'
不适合我。我得到的错误是,
module.js:557
throw err;
^
Error: Cannot find module 'mydepmod'
at Function.Module._resolveFilename (module.js:555:15)
at Function.Module._load (module.js:482:25)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/path/to/example/the-test.js:19:21)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
所以我的最后一个问题是如何让它发挥作用?
顺便说一句,我尝试了它的 docker 安装和 docker 中的 运行(以 import { mydepmod } from 'mydepmod'
开头)工作没有任何问题,所以我假设如果我这样做 npm install mydepmod
,它应该也能工作。
总而言之,我怎样才能使我的 git 拉取依赖模块像 npm install
ed 模块一样工作。谢谢。
当您使用前缀为“./”的相对路径导入时,它会从该文件加载模块,您可以提供完整路径,例如“/path/to/module”。
当您导入任何没有任何路径的模块时,节点会查找该模块
- 在
node_modules
文件夹中,在该目录中,或在package.json
存在的任何父目录中 - 如果无法从该目录加载模块,则节点会尝试从用户目录(例如,
/usr/lib/node_modules/
)中的全局node_modules
文件夹加载该模块。 - 如果节点在此阶段无法加载模块,则会抛出错误提示
Can not find module
。
Here 详细描述了 require 是如何工作的。 require 和 Import/Export 类似,this 文章给出了 require 和 import/export 的区别。
局部node_modules和全局node_modules的区别,here待讨论
全局node_modules的位置在不同的os环境中是不同的。根据this回答:
在 Unix 系统上,它们在全局安装时通常位于 /usr/local/lib/node
或 /usr/local/lib/node_modules
中。如果将NODE_PATH
环境变量设置为该路径,则可以通过node找到模块。
Windows 经验值 - %USERPROFILE%\Application Data\npm\node_modules
Windows 7、8 和 10 - %AppData%\npm\node_modules